Thursday, January 16, 2014

TCP in C (one server and multiple clients)

tcpserver.c
 
#include"stdio.h"
#include"stdlib.h"
#include"sys/types.h"
#include"sys/socket.h"
#include"string.h"
#include"netinet/in.h"

#define PORT 4444
#define BUF_SIZE 2000
#define CLADDR_LEN 100

void main() {
 struct sockaddr_in addr, cl_addr;
 int sockfd, len, ret, newsockfd;
 char buffer[BUF_SIZE];
 pid_t childpid;
 char clientAddr[CLADDR_LEN];
 
 sockfd = socket(AF_INET, SOCK_STREAM, 0);
 if (sockfd < 0) {
  printf("Error creating socket!\n");
  exit(1);
 }
 printf("Socket created...\n");
 
 memset(&addr, 0, sizeof(addr));
 addr.sin_family = AF_INET;
 addr.sin_addr.s_addr = INADDR_ANY;
 addr.sin_port = PORT;
 
 ret = bind(sockfd, (struct sockaddr *) &addr, sizeof(addr));
 if (ret < 0) {
  printf("Error binding!\n");
  exit(1);
 }
 printf("Binding done...\n");

 printf("Waiting for a connection...\n");
 listen(sockfd, 5);

 for (;;) { //infinite loop
  len = sizeof(cl_addr);
  newsockfd = accept(sockfd, (struct sockaddr *) &cl_addr, &len);
  if (newsockfd < 0) {
   printf("Error accepting connection!\n");
   exit(1);
  }
  printf("Connection accepted...\n");

  inet_ntop(AF_INET, &(cl_addr.sin_addr), clientAddr, CLADDR_LEN);
  if ((childpid = fork()) == 0) { //creating a child process

   close(sockfd); 
//stop listening for new connections by the main process. 
//the child will continue to listen. 
//the main process now handles the connected client.

   for (;;) {
    memset(buffer, 0, BUF_SIZE);
    ret = recvfrom(newsockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr, &len);
    if(ret < 0) {
     printf("Error receiving data!\n");  
     exit(1);
    }
    printf("Received data from %s: %s\n", clientAddr, buffer); 

    ret = sendto(newsockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr, len);   
    if (ret < 0) {  
     printf("Error sending data!\n");  
     exit(1);  
    }  
    printf("Sent data to %s: %s\n", clientAddr, buffer);
   }
  }
  close(newsockfd);
 }
}

tcpclient.c
 
#include"stdio.h"  
#include"stdlib.h"  
#include"sys/types.h"  
#include"sys/socket.h"  
#include"string.h"  
#include"netinet/in.h"  
#include"netdb.h"
  
#define PORT 4444 
#define BUF_SIZE 2000 
  
int main(int argc, char**argv) {  
 struct sockaddr_in addr, cl_addr;  
 int sockfd, ret;  
 char buffer[BUF_SIZE];  
 struct hostent * server;
 char * serverAddr;

 if (argc < 2) {
  printf("usage: client < ip address >\n");
  exit(1);  
 }

 serverAddr = argv[1]; 
 
 sockfd = socket(AF_INET, SOCK_STREAM, 0);  
 if (sockfd < 0) {  
  printf("Error creating socket!\n");  
  exit(1);  
 }  
 printf("Socket created...\n");   

 memset(&addr, 0, sizeof(addr));  
 addr.sin_family = AF_INET;  
 addr.sin_addr.s_addr = inet_addr(serverAddr);
 addr.sin_port = PORT;     

 ret = connect(sockfd, (struct sockaddr *) &addr, sizeof(addr));  
 if (ret < 0) {  
  printf("Error connecting to the server!\n");  
  exit(1);  
 }  
 printf("Connected to the server...\n");  

 memset(buffer, 0, BUF_SIZE);
 printf("Enter your message(s): ");

 while (fgets(buffer, BUF_SIZE, stdin) != NULL) {
  ret = sendto(sockfd, buffer, BUF_SIZE, 0, (struct sockaddr *) &addr, sizeof(addr));  
  if (ret < 0) {  
   printf("Error sending data!\n\t-%s", buffer);  
  }
  ret = recvfrom(sockfd, buffer, BUF_SIZE, 0, NULL, NULL);  
  if (ret < 0) {  
   printf("Error receiving data!\n");    
  } else {
   printf("Received: ");
   fputs(buffer, stdout);
   printf("\n");
  }  
 }
 
 return 0;    
}  

18 comments:

  1. how to execute this program

    ReplyDelete
    Replies
    1. --terminal 1--
      gcc tcpserver.c -o server
      ./server

      --terminal 2--
      gcc tcpclient.c -o client
      ./client 192.168.0.4

      --terminal 3--
      ./client 192.168.0.4

      Delete
    2. Note that 192.168.0.4 is the IP address of the machine in which the server is running.

      Delete
  2. this is not tcp.this is udp.u have used "sendto()" and "recvfrom()"

    ReplyDelete
    Replies
    1. Hi,
      Sorry for late reply!
      Functions sendto() and recvfrom() are MUST while using UDP. However you can use it in TCP also.
      What distinguishes TCP from UDP is the following line of statement-
      sockfd = socket(AF_INET, SOCK_STREAM, 0); //for TCP
      sockfd = socket(AF_INET, SOCK_DGRAM, 0); //for UDP

      Delete
  3. we executed both client & server programs but, while we executing after giving corresponding ip address "Error connecting to the server" message is displayed.

    ReplyDelete
    Replies
    1. --terminal 1--
      gcc tcpserver.c -o server
      ./server

      --terminal 2--
      gcc tcpclient.c -o client
      ./client 192.168.0.4

      --terminal 3--
      ./client 192.168.0.4


      Replace 192.168.0.4 with the IP address of the machine in which the server program is running.

      Delete
  4. Thank you.. It works for us.

    ReplyDelete
  5. i want to run it in ubuntu.. how to connect the server to multiple clients.
    thank you in advance

    ReplyDelete
    Replies
    1. --terminal 1--
      gcc tcpserver.c -o server
      ./server

      --terminal 2--
      gcc tcpclient.c -o client
      ./client 192.168.0.4

      --terminal 3--
      ./client 192.168.0.4


      Replace 192.168.0.4 with the IP address of the machine in which the server program is running.

      Delete
    2. in the client terminal after writing ./client
      it is showing that
      no such file or directory is found

      Delete
    3. gcc tcpclient.c -o client

      This command compiles "tcpclient.c" and saves the executable with file name "client"

      For execution "./client" can be used only if the name of the executable file generated on compiling the program is "client"

      Delete
  6. yes its working good
    thank you sir

    ReplyDelete
  7. how does the server diffrentiates between clienta if the port no and ip address are same?

    ReplyDelete
    Replies
    1. In TCP, you can only have one application listening to the same port at one time (in the same machine/IP address).

      Delete
  8. what happen if we use ip address 122.0.0.1

    ReplyDelete
    Replies
    1. You can use 122.0.0.1 only if the IP address of the machine in which the server program is running is 122.0.0.1

      Delete

Contact Form

Name

Email *

Message *

The Insane Techie - Android App

Launched an android app for the blog on 07th June 2016. Get it from google play store... Tips for using the app Use in landscape mo...