Explicitly assigning port number to client in Socket
Prerequisite: Socket programming in C/C++.
In socket programming, when server and client are connected then the client is provided any random port number by an 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 the client by the operating system may have been blocked by the 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 the client program to run on only a certain port number and so in this case, the client needs to forcefully assign that port number only as it runs on port number either on 111 or on 2049. This can be done using a bind() system call specifying a particular port number in a client-side socket.
Below is the implementation Server and Client program where a 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 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 write.geeksforgeeks.org or mail your article to email@example.com. 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.