Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

something about ports in Windows2000

How to open a new port and assign specific services(HTTP) to that on my Win2k server.

 awaiting of clarification.
1 Solution
The best way to do that is the same way as in any unix style program:

Step 1. Open a socket.

int sock = socket(AF_INET, SOCK_DGRAM, 0);

step 2. define a sockaddr_in struct:

sockaddr_in sin;

sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons(PORT);

bind(sock, & sin, sizeof(sin));

Now, if you specify a service such as HTTP it is possible that there already is a program that is listening on that port. In that case the bind above will fail. In this case you have to make up your mind if you should let that other program continue to listen on that port or if you should grab the port from it.

If you decide to let that other service use the port then you can either use a differnt port or you can close the socket and exit.

If you decide to grab it you must stop that HTTP service already running. Use task manager or some API to kill that process. Then you can reattempt the bind.

step 3:

When bind is successful you have made an association with the port number and the socket. Next step is to create a listening queue to the socket, it won't accept any incoming connects to it otherwise:


It is common to use a length of 5 on such a socket. This means that there can be at most 5 connect attempts in the queue at any one time. However, you should normally process that queue very fast and so it doesn't have to be large. The queue is also in system shared memory and is in a part of memory shared among all processes on that machine and you should therefore not be greedy and create a big queue. In no case is a queue of more than 10 reasonable anyway, if you can't accept connects fast enough to handle 10 you won't be able to accept them faster by increasing the queue size to 20 anyway. so 5 or even less is the common, in rare cases can one see code using 10 - a value higher than 10 is just stupid.

step 4:

Now that you have made a queue the queue will start to fill up by other programs possibly on other machines trying to connect() to the port. It is time to accept them, you do this typically in a loop:

int csock;
int csin_len = sizeof(sockaddr_in);
struct sockaddr_in csin;

while ((csock = accept(sock, & csin, & csin_len)) >= 0) {
   // csock is a connection.
   // handle this connection.
   csin_len = sizeof(csin);

Now this is very simplified code. handle_connection will typically fork() to a child process to handle the connection or create a thread to handle the connection. In this case the handle_connection() function will probably return when that thread or process has been created. In other cases the handling is supposed to be a small and simple thing (such as sending a short reply to whoever tried to connect) and in that case the handle_connection() can simply send the reply to csock using send(csock, ...).

In the case of HTTP you most likely will have a lengthy conversation with the client and so you will fork() or create a thread. Note that using threads can be a security risk since the thread will then run in the same memory space as the server itself. In this case you will create a new process/fork() or create a new thread and then have the main server return from handle_connection as soon as the creation is done. The child will of course continue in that child process by its thread main function and will never return from handle_connection. Either the child process will exit() when it is done or incase of a thread it will simply exit_thread() or exit from the thread main function. In neither circumstance will it return from handle_connection().

Are you attempting to write your own HTTP server? Be aware that that is a rather complicated server and the very fact that you ask here indicates to me that you are not up to the task. Find yourself something simpler at first - a simple client/server program that can communicate with each other.

hey, you should award him points since he gave you a correct answer and stuff
No comment has been added lately (185 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to Salte http:#8056761

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --


EE Cleanup Volunteer

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now