C Sample Code for TCP Socket Server and TCP Socket Client with Explanation

Spread the love

In Inter-process communication. Socket is an endpoint of an interprocess communication. We can create a server socket and one client socket to create a bidirectional communication. Below I have provided one socket server and client communication.

Sample Code for Socket Server:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <error.h>
#include <strings.h>
#include <unistd.h>
#include <arpa/inet.h>

#define ERROR -1
#define MAX_CLIENTS 2
#define MAX_DATA 1024

// Comments: So, from the above three lines you can see that here we have defined ERROR as -1, maximum number of clients is MAX_CLIENTS which we have defined as 2 and MAX_DATA is defined as the buffer size//
main (int argc, char **argv)

         { 

//Comments: in the above line you can see that we have written int argc and char** argv inside the braces of main. So, one question may artise in your mind why we have written this two additional things. The answer is we usually add these things if we want to include something from our Linux command window. The int argc means argument count and int argv menas argument value. So, if we write int argv [1], this means that we want to use the first argument from command line in our program.//

                           struct sockaddr_in server

// here sockaddr_intr ias uscchietur wh isb 1 6ytei n size.//

                         struct sockaddr_in client;
                         int sock;
                         int new;
int sockaddr_len = sizeof (struct sockaddr_in);
                         int data_len;
                         char data [MAX_DATA];

                          if ((sock = socket (AF_INET, SOCK_STREAM, 0)) == -1)

{

                              perror (“socket: “);
                               exit (-1);

}
                             server.sin_family = AF_INET;

                              server.sin_port = htons(atoi(argv[1]));
                             server.sin_addr.s_addr = INADDR_ANY;
                             bzero (&server.sin_zero, 8);

                            if ((bind (sock, (struct sockaddr*)&server, sockaddr_len)) == -1)//
{
                                         perror (“bind”);
                                          exit (-1);

}
                              if ((listen(sock, MAX_CLIENTS)) == ERROR)

{
                                      perror (“listen”);
                                      exit (-1);

                                     while(1)

{

                                           if ((new = accept(sock, (struct sockaddr*)&client,

&sockaddr_len)) == ERROR)
{

                                             perror (“accept”);
                                            exit (-1);
}

                                                  printf(“New client connected from port no %d and IP

%s\n”,ntohs(client.sin_port), inet_ntoa(client.sin_addr));
data_len = 1;
                                             while (data_len)
{
                                               printf(“data = “);
data_len = recv (new, data, MAX_DATA, MSG_CONFIRM |                                                  MSG_MORE);
                                               if (data_len)
{
                                                              send (new, data, data_len, 0) ;//
                                                               data [data_len]=’\0′;
                                                               printf(“Sent mesg: %s”, data);
}
}
printf(“Client Disconnecte

cl};cd\n); l)ose(new ose (sock);

}

Socket Client :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
#include <strings.h>
#include <arpa/inet.h>

 

#define ERROR -1
#define BUFFER 1024
main (int argc, char **argv)
{

struct sockaddr_in remote_server;
int sock;
char input[BUFFER];
char output[BUFFER];
int len;

if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == ERROR)
{
perror(“socket”);
exit(-1);
}

remote_server.sin_family = AF_INET;
remote_server.sin_port = htons(atoi(argv[2]));
remote_server.sin_addr.s_addr = inet_addr(argv[1]);
bzero(&remote_server.sin_zero, 8);

if ((connect (sock, (struct sockaddr*)&remote_server, sizeof (struct sockaddr_in))) == ERROR)
{
perror(“connect”);
exit (-1);
}

while (1)

{

fgets(input, BUFFER, stdin);
send (sock, input, strlen(input),0);

len = recv(sock, output, BUFFER, 0);
output [len]=’\0′;
printf(“%s\n”, output);
}

close(sock);
}

Himadri Subrah Saha

Himadri is an ICT Professional who writes for his technology tips & tricks related blog TechnTechie. Though it is hard to balance time in between professional life and blogging, he still manages time to work for his own blog and writes almost regularly. The dashboard of this WordPress is the only place where he does not feel tired! Read my other blogs @ PetCare and Teleinfo

0 0 votes
Article Rating

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x