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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Is it possible to call constructor and destructor explicitly?
- How to bind to port number less than 1024 with non root access?
- Assigning multiple characters in an int in C language
- Assigning an integer to float and comparison in C/C++
- SSH Port Forwarding
- Type-C Port in Computer Network
- Port Security in Computer Network
- Socket Programming in Java
- Socket in Computer Network
- Secure Socket Layer (SSL)
- Socket Programming in Python
- Port Address Translation (PAT) mapping to Private IPs
- Sort an array using socket programming in C
- Port Address Translation (PAT) on Adaptive Security Appliance (ASA)
- Socket Programming with Multi-threading in Python
- Difference between Server OS and Client OS
- UDP Server-Client implementation in C
- TCP Server-Client implementation in C
- Socket Programming in C/C++: Handling multiple clients on server without multi threading
- Difference between Client /Server and Distributed DBMS