Explicitly assigning port number to client in Socket - GeeksforGeeks

09 Nov, 2021
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 
C
// 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
// 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 write.geeksforgeeks.org or mail your article to review-team@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. 
marcosarcticseal
C-Library
C Language
Computer Networks
Computer Networks
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
TCP Server-Client implementation in C
Multithreading in C
Exception Handling in C++
'this' pointer in C++
Arrow operator -> in C/C++ with Examples
Layers of OSI Model
TCP/IP Model
RSA Algorithm in Cryptography
TCP Server-Client implementation in C
Differences between TCP and UDP