ports with TCP/IP

Started by
3 comments, last by Ridcully 23 years, 11 months ago
i have just started with winsock programming and don''t get the concept of the ports with TCP/IP. as i understand it, there are only 65536 ports avaible (16-bit). does that mean you can only have that number of connections on ONE machine open? what if a server opened a connection on port 4567 and the client that wants to connect finds that port blocked by another application? are there any proven FREE ports that are guaranteed to work? as you can see a lot of questions.. thanks for any answers ridcully
Advertisement
Normally the server has a port open to listen on. Say 3000.
Then clients connect to that port and the server accepts connections on a different port (any free)

Then the connection is estableshed and communication can begin.

And you can only have 65536 connections to one computer but that computor would just die from it.
I am not the most knowledgable of people on this subject, but I think that ports are where you connect. I belive you can have up to 200 or so connections on a single port. If you have a server, usually you run your program on a set port so that everyone knows what port to connect to. No other program will use that port on your server.

You bind a socket to a port with the bind() function. It has some wierd syntax on windows machines, something like the port number times 256 (i.e. 13*256 for port 13).

--------------------


You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

why would you need 65536 ports anyway?

all ports < 1024 are reserved for standard services such as email, news, etc (ex: telnet runs on port 23). 1024 and above are all free for you to use.

in socket programming, you can set how many connections are available on one port at one time (i think this is done with the bind() function). a port will not accept a connection unless there is a server running on that port. (translation: you have a program running that has been bind()-ed to that port number.) incoming packets destined for that port are directed to the program by the operating system.

hope this helps.

----------------------------------------
Look behind you! A three-headed monkey!
----------------------------------------Look behind you! A three-headed monkey!
Here''s how the TCP/IP connection system works. The server opens a socket, and binds that socket to an interface/port combinations. Say port 23 on 13.14.15.16. The client on say 13.15.16.17 then creates a new socket and calls connect() on that socket. The OS then assigns that socket a random port on the client machine (say 2345). It''s important to note that the port address of the client machine is not necessarily the same as the port it''s trying to connect to. Which is why you can have multiple web pages down loading, for example. So the address of the client socket is port 2345 on 13.15.16.17. A request for a connection is sent fro 13.15.16.17:2345 to 13.14.15.16:23. The server then calls accept() on that socket it opened. This in turn creates a new socket on say port 3456. And the server uses that socket to communicate with the client. So the connection between them 13.14.15.16:3456 to 13.15.16.17:2345. Which leaves 13.14.15.16:23 still open to listen for connections. So every time a connect() is completed the server uses up the port. If you want the server to communicate with more than 65,000 clients you should use UDP, which isn''t connection oriented. i.e. doesn''t use up ports like that. Also any future attempt to bind ports 23 or 3456 on the server will fail. The OS should not assign either of those ports for any new sockets created by connections either.

Yanroy: The bind() function expects the port number to be in network byte order, which on the intel x86 is not the same as the host byte order. Add a htons() call around your port number in order to do the conversion in a portable manner.

guybrush: I think what you''re thinking of is that there is a maximum number of client connections that can be in the queue for a listening socket. You set this options with the listen() call. Whenever the accept() call is processed on that socket the number of connections in the queue goes down. So you can accept() an unlimited (where unlimited is defined to be around 65,000) number of connections on a single port.

This topic is closed to new replies.

Advertisement