How to create a TCP server-client program that will send 200 tcp flows from the source to the destination? [closed]

StackOverflow https://stackoverflow.com/questions/23470489

Question

I want to create a simple TCP Server-client program for my Ubuntu 12.10 where the source will be able to send 200 (or, any n) no. of flows to the destination? I am looking for some sort of socket programming. Thanks in advance!

Was it helpful?

Solution

For next questions I recomend you to first try googleing it. There are thousands of examples on the internet.

Modify this one. You only have to modify the while at the bottom of the program. The port should go in the line commented.

Client.c

int main(int argc, char**argv)
{
   int sockfd,n,m=0;
   struct sockaddr_in servaddr,cliaddr;
   char sendline[1000];
   char recvline[1000];

   if (argc != 3)
   {
      printf("usage:  client <IP address> <Number of messagges>\n");
      exit(1);
   }

   sockfd=socket(AF_INET,SOCK_STREAM,0);

   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr=inet_addr(argv[1]);
   servaddr.sin_port=htons(32000); //Port for the TCP Client

   connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

   while (m<=argv[2]) //While that has to be modified
   {
      fgets(sendline, 10000,stdin;
      sendto(sockfd,sendline,strlen(sendline),0,
             (struct sockaddr *)&servaddr,sizeof(servaddr));
      n=recvfrom(sockfd,recvline,10000,0,NULL,NULL);
      recvline[n]=0;
      fputs(recvline,stdout);
      m++;
   }
}

Server.c

int main(int argc, char**argv)
{
   int listenfd,connfd,n;
   struct sockaddr_in servaddr,cliaddr;
   socklen_t clilen;
   pid_t     childpid;
   char mesg[1000];

   listenfd=socket(AF_INET,SOCK_STREAM,0);

   bzero(&servaddr,sizeof(servaddr));
   servaddr.sin_family = AF_INET;
   servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
   servaddr.sin_port=htons(32000);//Port that has to be modified
   bind(listenfd,(struct sockaddr *)&servaddr,sizeof(servaddr));

   listen(listenfd,1024);

   for(;;)
   {
      clilen=sizeof(cliaddr);
      connfd = accept(listenfd,(struct sockaddr *)&cliaddr,&clilen);

      if ((childpid = fork()) == 0)
      {
         close (listenfd);

         for(;;)
         {
            n = recvfrom(connfd,mesg,1000,0,(struct sockaddr *)&cliaddr,&clilen);
            sendto(connfd,mesg,n,0,(struct sockaddr *)&cliaddr,sizeof(cliaddr));
            printf("-------------------------------------------------------\n");
            mesg[n] = 0;
            printf("Received the following:\n");
            printf("%s",mesg);
            printf("-------------------------------------------------------\n");
         }

      }
      close(connfd);
   }
}

Source: http://www.cs.ucsb.edu/ I've also googled it.

Have a nice code!

OTHER TIPS

You can find a lot of examples of client/server programming in C on Internet. One of first Google answers gives this link where you have an example you can use to do what you want after adapting it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top