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
// C program to demonstrate // socket programming in finding ip address // and port number of connected client // on Server Side #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<sys/un.h> #include<string.h> #include<netdb.h> #include<netinet/in.h> #include<arpa/inet.h> #include<string.h> int main() { // Two buffers for message communication char buffer1[256], buffer2[256]; int server = socket(AF_INET, SOCK_STREAM, 0); if (server < 0) printf("Error in server creating\n"); else printf("Server Created\n"); struct sockaddr_in my_addr, peer_addr; my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = INADDR_ANY; // This ip address will change according to the machine my_addr.sin_addr.s_addr = inet_addr("10.32.40.213"); my_addr.sin_port = htons(12000); if (bind(server, (struct sockaddr*) &my;_addr, sizeof(my_addr)) == 0) printf("Binded Correctly\n"); else printf("Unable to bind\n"); if (listen(server, 3) == 0) printf("Listening ...\n"); else printf("Unable to listen\n"); socklen_t addr_size; addr_size = sizeof(struct sockaddr_in); // Ip character array will store the ip address of client char *ip; // while loop is iterated infinitely to // accept infinite connection one by one while (1) { int acc = accept(server, (struct sockaddr*) &peer;_addr, &addr;_size); printf("Connection Established\n"); char ip[INET_ADDRSTRLEN]; inet_ntop(AF_INET, &(peer_addr.sin_addr), ip, INET_ADDRSTRLEN); // "ntohs(peer_addr.sin_port)" function is // for finding port number of client printf("connection established with IP : %s and PORT : %d\n", ip, ntohs(peer_addr.sin_port)); recv(acc, buffer2, 256, 0); printf("Client : %s\n", buffer2); strcpy(buffer1, "Hello"); send(acc, buffer1, 256, 0); } return 0; } |
Output:
Server Created Binded Correctly Listening ... Connection Established connection established with IP : 10.32.40.213 and PORT : 12010 Client : Hello
Client Side Program
// C program to demonstrate socket programming // as well as explicitly assigning a port number // on Client Side #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<sys/un.h> #include<string.h> #include<netdb.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdlib.h> int main() { // Two buffer are for message communication char buffer1[256], buffer2[256]; struct sockaddr_in my_addr, my_addr1; int client = socket(AF_INET, SOCK_STREAM, 0); if (client < 0) printf("Error in client creating\n"); else printf("Client Created\n"); my_addr.sin_family = AF_INET; my_addr.sin_addr.s_addr = INADDR_ANY; my_addr.sin_port = htons(12000); // This ip address will change according to the machine my_addr.sin_addr.s_addr = inet_addr("10.32.40.213"); // Explicitly assigning port number 12010 by // binding client with that port my_addr1.sin_family = AF_INET; my_addr1.sin_addr.s_addr = INADDR_ANY; my_addr1.sin_port = htons(12010); // This ip address will change according to the machine my_addr1.sin_addr.s_addr = inet_addr("10.32.40.213"); if (bind(client, (struct sockaddr*) &my;_addr1, sizeof(struct sockaddr_in)) == 0) printf("Binded Correctly\n"); else printf("Unable to bind\n"); socklen_t addr_size = sizeof my_addr; int con = connect(client, (struct sockaddr*) &my;_addr, sizeof my_addr); if (con == 0) printf("Client Connected\n"); else printf("Error in Connection\n"); strcpy(buffer2, "Hello"); send(client, buffer2, 256, 0); recv(client, buffer1, 256, 0); printf("Server : %s\n", buffer1); return 0; } |
Output:
Client Created Binded Correctly Client Connected Server : Hello
Reference : https://stackoverflow.com/questions/4118241/what-client-side-situations-need-bind
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 contribute@geeksforgeeks.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.
Recommended Posts:
- Is it possible to call constructor and destructor explicitly?
- Assigning an integer to float and comparison in C/C++
- Assigning multiple characters in an int in C language
- How to bind to port number less than 1024 with non root access?
- Difference between IP address and Port Number
- Port Security in Computer Network
- Type-C Port in Computer Network
- Port Address Translation (PAT) on Adaptive Security Appliance (ASA)
- Difference Between Network Address Translation (NAT) and Port Address Translation (PAT)
- SSH Port Forwarding
- Port Address Translation (PAT) mapping to Private IPs
- Switch Port Analyzer (SPAN)
- TCP Server-Client implementation in C
- Reverse a string in C/C++ using Client Server model
- UDP Server-Client implementation in C
- Java Implementation of Deffi-Hellman Algorithm between Client and Server
- Difference between Client-Server and Peer-to-Peer Network
- TCP Client-Server Program to Check if a Given String is Palindrome
- Two way communication between Client and Server using Win32 Threads
- Distributed Objects Computing: The next generation of client-server computing

