something about ports in Windows2000

Posted on 2003-03-03
Medium Priority
Last Modified: 2010-04-01
How to open a new port and assign specific services(HTTP) to that on my Win2k server.

 awaiting of clarification.
Question by:psnraju
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 12

Accepted Solution

Salte earned 80 total points
ID: 8056761
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.


Expert Comment

ID: 8142792
hey, you should award him points since he gave you a correct answer and stuff
LVL 30

Expert Comment

ID: 9374592
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question