Networking Forums

Networking Forums > Computer Networking > Windows Networking > Socket: how to get a unique port number ?

Reply
Thread Tools Display Modes

Socket: how to get a unique port number ?

 
 
Polaris
Guest
Posts: n/a

 
      04-04-2005, 06:02 PM
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




 
Reply With Quote
 
 
 
 
Nathan Evans
Guest
Posts: n/a

 
      04-04-2005, 08:33 PM
Just use the port 0 and Winsock will select an unused port number for
you. This is the preferred method, too.

 
Reply With Quote
 
Phillip Windell
Guest
Posts: n/a

 
      04-04-2005, 09:15 PM
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.


--

Phillip Windell [MCP, MVP, CCNA]
www.wandtv.com


"Polaris" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>
>



 
Reply With Quote
 
Sinna
Guest
Posts: n/a

 
      04-05-2005, 02:24 PM
Polaris wrote:
> 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
 
Reply With Quote
 
Dave
Guest
Posts: n/a

 
      04-05-2005, 03:12 PM
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.

"Sinna" <(E-Mail Removed)> wrote in message
news:eYRR$(E-Mail Removed)...
> Polaris wrote:
> > 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



 
Reply With Quote
 
Polaris
Guest
Posts: n/a

 
      04-05-2005, 08:59 PM
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

"Polaris" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>
>



 
Reply With Quote
 
Sinna
Guest
Posts: n/a

 
      04-06-2005, 08:36 AM
Dave wrote:
> 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
 
Reply With Quote
 
Sinna
Guest
Posts: n/a

 
      04-06-2005, 08:37 AM
Polaris wrote:
> 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
 
Reply With Quote
 
Dave
Guest
Posts: n/a

 
      04-06-2005, 12:12 PM
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.

"Sinna" <(E-Mail Removed)> wrote in message
news:e%23Z%(E-Mail Removed)...
> Dave wrote:
> > 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



 
Reply With Quote
 
Sinna
Guest
Posts: n/a

 
      04-06-2005, 01:37 PM
Dave wrote:
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Socket connection to port fails despite port open / listening ! Jack Linux Networking 2 12-19-2007 03:46 PM
simple q on IP address and port number dew Linux Networking 3 03-24-2007 12:16 PM
increase the number of port ranges? Kevin Blount Wireless Internet 5 01-11-2006 05:16 PM
UDP source port number when using RAW socket?? Andreas Linux Networking 0 06-28-2004 10:34 AM
Routing by port number Bill Davidsen Linux Networking 3 12-21-2003 12:53 AM



1 2 3 4 5 6 7 8 9 10 11