Socket: how to get a unique port number ?

P

Polaris

Hi:

I'm writing a server program on WinXP using Win Socket. I need to assign a
listening port for my server. Just wonder if there is any API I can use to
find an currently un-used port for the system? Seems the answer is "no", but
just like to make sure, or is there any other way to find a port which is
not in use?

I'm not using MFC's socket class.

Thanks in Advance!
Polaris
 
N

Nathan Evans

Just use the port 0 and Winsock will select an unused port number for
you. This is the preferred method, too.
 
P

Phillip Windell

If you need a specific number that you will always know what it is,..
then,...it is "open season" on anything above port 5000. There is always a
chance you will use on that some other Application uses, that is just the
way it is.
 
S

Sinna

Polaris said:
Hi:

I'm writing a server program on WinXP using Win Socket. I need to assign a
listening port for my server. Just wonder if there is any API I can use to
find an currently un-used port for the system? Seems the answer is "no", but
just like to make sure, or is there any other way to find a port which is
not in use?

I'm not using MFC's socket class.

Thanks in Advance!
Polaris
Hi,

I had the same problem about two weeks ago.
I solved it by using GetTcpTable (the API equivalent of NETSTAT).
By sorting the returned list I'm able to assign a port to my process
that is not in use currently.
I don't think it's completely fail-safe, but it's up to the other
programs to not launch their process on a port that I have in use.


Sinna
 
D

Dave

how do you plan on telling your client app how to find the port the server
is listening on? normally the server side port is a static assignment so
the clients know how to connect to it. It could be changed like you can
change the http port in iis, but then of course you have to have a way to
tell your clients to also change the port they try to connect to.
 
P

Polaris

Hi everyone:

In my case, the client and server are running on the same machine. The
server can write the listening port in registry and the client can read it
there. I think I will try GetUdpTable ...

Thanks all for your help !

Polaris
 
S

Sinna

Dave said:
how do you plan on telling your client app how to find the port the server
is listening on? normally the server side port is a static assignment so
the clients know how to connect to it. It could be changed like you can
change the http port in iis, but then of course you have to have a way to
tell your clients to also change the port they try to connect to.

Well, sorry to mention, but client and server are running on the same PC
and when launching the client, the server is launched on a given port
(determined by the client). When the client shuts down, the server is
also shut down.

Sinna
 
S

Sinna

Polaris said:
Hi everyone:

In my case, the client and server are running on the same machine. The
server can write the listening port in registry and the client can read it
there. I think I will try GetUdpTable ...

Thanks all for your help !

Polaris
Don't use GetUdpTable, but GetTcpTable!
I can't imagine you're using the UDP-protocol instead of the TCP-protocol.

Sinna
 
D

Dave

so its not really a client-server connection just two processes on the same
machine that need to talk. and its not even really a 'server' if it is only
started when the client wants to use it, usually servers are sitting there
listening all the time just waiting for connections from clients. you might
want to look at other ipc mechanisms, you may find that some other method is
better suited for what you want to do than tcp/ip.
 
S

Sinna

Dave said:
so its not really a client-server connection just two processes on the same
machine that need to talk. and its not even really a 'server' if it is only
started when the client wants to use it, usually servers are sitting there
listening all the time just waiting for connections from clients. you might
want to look at other ipc mechanisms, you may find that some other method is
better suited for what you want to do than tcp/ip.
I know, but in fact the server acts as a proxy for a Remote Device. We
use the TCP/IP-mechanism so the client doesn't has to know if it's
connecting to a Local IP or a Remote IP.
This reduces a lot of reprogramming.


Sinna
 
P

Polaris

Thanks Sinna. I'm using UDP socket, I know for best security, TCP should be
used, but I think both server and the client are running on the same
machine, it should be ok and it is much simpler to code. But I might change
it later.

Polaris
 
A

Arkady Frenkel

Not security , but reliability , but that not so relevant for you ( the same
host )
Arkady
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top