Explicitly assigning port number to client in Socket
Prerequisite: Socket programming in C/C++.
In socket programming, when server and client are connected then client is provided any random port number by operating system to run and generally, we don’t care about it, But in some cases, there may be a firewall on the client side that only allows outgoing connections on certain port numbers. So, it is highly probable that the port number provided to client by operating system may have been blocked by client firewall. In that case, we need to explicitly or forcefully assign any port number to the client on which it can operate.
Some protocol like NFS protocol requires client program to run on only certain port number and so in this case, client need to forcefully assigned that port number only as it runs on port number either on 111 or on 2049. This can be done using bind() system call with specifying particular port number in client side socket.
Below is the implementation Server and Client program where client will be forcefully get assigned a port number.
Server Side Program
Server Created Binded Correctly Listening ... Connection Established connection established with IP : 10.32.40.213 and PORT : 12010 Client : Hello
Client Side Program
Client Created Binded Correctly Client Connected Server : Hello
This article is contributed by Aditya Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.