Reputation: 37
there something about this tutorial i do not understand. I hope someone can explain to me. http://www.tidytutorials.com/2009/12/c-winsock-example-using-client-server.html
In the server program, it's port was initialised to 1101 (line 14)
13. //The port you want the server to listen on
14. int host_port= 1101;
and in the client program it's port is also 1101
12. //The port and address you want to connect to
13. int host_port= 1101;
14. char* host_name="127.0.0.1";
Now here's the question.
Is the int host_port in client program the same as int host_port inside the server program?
Why is the port number specifically 1101? (i get error 111 when i change the port number to 80 or other number like 1234)
isnt char* host_name="127.0.0.1"; supposed to refer to the client ip address? Why must i specifically use the loopback address and not 192.x.x.x.
Thank you
I realise my mistake, i run my client before the server program......... and in the client program i did not put
serv_addr.sin_addr.s_addr = INADDR_ANY; <<<<<<
Upvotes: 1
Views: 12945
Reputation: 121599
Your choice of port# is just that - a choice.
"Well known port#'s" (such as port "80" for http, "25" for SMTP, "443" for HTTPS, etc etc) should not be used. But everything else is up for grabs.
The port number the server binds to must match the port# the client tries to connect to - or the "connect" will fail.
Finally, you can use any available "interface" for either your client or your server. "Loopback" (for example, "127.0.0.1") is one such interface. "192.x.x.x." (for your particular LAN) might be another.
The key thing is that the client must have network connectivity to the server. This implies both physical connectivity as well as addressibility (the two IPs are either on the same LAN, or there's a reliable "gateway" between the two hosts.
I'd strongly encourage you to check out Beej's Guide to Network Programming, if you're not familiar with it. It's a short, but excellent, introduction to TCP/IP and sockets programming. Satisfaction guaranteed!
Upvotes: 0
Reputation: 4035
The host port is the port where you have bind your server program, so you have to use the same port in client as well to connect to the server.
The port number and IP address together forms the transport address. If you want that any IP address on the server should work, then you use INADDR_ANY which would bind the server to all the valid IP's on the machine.
sockfd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY; <<<<<<
serv_addr.sin_port = htons(2000);
bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));
Upvotes: 1
Reputation: 1885
The server and client port numbers MUST match. The port number is part of the address of the server. If the client applies wrong address, i.e. wrong port number OR wrong server address, the message would be delivered to wrong recipient. Much like postal addresses. If you are writing to your friend, you have to mention street number AND city. If you put wrong street number, the letter would reach somewhere else.
AND then you MUST use a port number that is not in use. 80 is a well known port number; reserved for HTTP. If a port number is used, you cannot allocate it to someone else. On your machine port 80 may be in use. You would not want your home street address to be allocated to someone else OR someone else allocated the same house as yours. That would be inconvenient. You can imagine what would happen if computers allowed two programs to use same port number.
You can always evict someone from their well known port number. You can shut off the web server, if one is running on your machine, and then write client/server app to use port 80.
You can use 192.xxx addresses. Provided that the firewalls on your computers are not blocking it. Because while loopback loops back within the computer, the 192.xx addressing scheme would cause the packets to go out on LAN and come back. I may be wrong on this one, on some operating systems. Experiment and let us know if this is right or wrong.
Upvotes: 1
Reputation: 310840
The IP address and port used in the connect()
method by the client are the server's. Not the client's. The client is connecting to the server, at that IP:port.
The client's own IP address and port are allocated automatically, unless for some reason you call bind()
prior to connect().
Upvotes: 0