C Linux Code for multi threaded socket chat

//server_threaded_chat_concurrent
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>//for sockaddr_in
#include <string.h>
#include <unistd.h>

#define MAXLINE 4096 /*max text line length*/
#define SERV_PORT 3000 /*port*/
#define LISTENQ 8 /*maximum number of client connections*/


char buf[MAXLINE];
int n;
void do_service(int sd)
{
while ( (n = recv(sd, buf, MAXLINE,0)) > 0)  {
   printf("%s","String received from and resent to the client:");
   puts(buf);
   send(sd, buf, n, 0);
  }

  if (n < 0){
   printf("%s\n", "Read error");
  exit(0);
}
}


//do service function executed by child
int  curr_sd, n;
void* body(int *arg)
{
struct sockaddr_in c_add;
int addrlen;
int i,l;
int base_sd = (int) arg;
int sd;
while (1) {
//pthread_mutex_lock(&m_acc);
sd = accept(base_sd, &c_add, &addrlen);
//pthread_mutex_unlock(&m_acc);
printf("\n\nsd=%d\n\n",sd);
do_service(sd);
close(sd);
/*curr_sd=sd;
while ( (n = recv(curr_sd, buf, MAXLINE,0)) > 0)  {
   printf("%s","String received from and resent to the client:");
   puts(buf);
   send(curr_sd, buf, n, 0);
  }

  if (n < 0){
   printf("%s\n", "Read error");
  exit(0);
 }*/
}
}

//creating socket and listening
int connectbody()
{
int base_sd2;
struct sockaddr_in c_add, servaddr;

 if ((base_sd2 = socket (AF_INET, SOCK_STREAM, 0)) <0) {
  perror("Problem in creating the socket");
  exit(2);
 }

 //preparation of the socket address
 servaddr.sin_family = AF_INET;
 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
 servaddr.sin_port = htons(SERV_PORT);

 //bind the socket
 bind (base_sd2, (struct sockaddr *) &servaddr, sizeof(servaddr));

 //listen to the socket by creating a connection queue, then wait for clients
 listen (base_sd2, LISTENQ);

 printf("%s\n","Server running...waiting for connections.");

return base_sd2;
}

int main (int argc, char **argv)
{

 //Create a socket and listen

int base_sd;
base_sd=connectbody();

//accepting the maximum queud connections
pthread_t t[10];
struct sockaddr_in c_add;
socklen_t addrlen;
addrlen = sizeof(c_add);
int i;
for (i=0 ;i<10 ;i++ )
{
  printf("\n\nthread is t[%d]\n\n",i);
 // curr_sd = accept (base_sd, (struct sockaddr *) &c_add, &addrlen );
  //body(curr_sd);
pthread_create(&t[i],0,body,(void *)base_sd);
}//end of infinite for loop
pause();
//close(base_sd);
}
////////////---------------------CLIENT--------------------///////////////////
//client_threaded_chat_concurrent
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>

#define MAXLINE 4096 /*max text line length*/
#define SERV_PORT 3000 /*port*/

int
main(int argc, char **argv)
{
 int sockfd;
 struct sockaddr_in servaddr;
 char sendline[MAXLINE], recvline[MAXLINE];

 //basic check of the arguments
 //additional checks can be inserted
 if (argc !=2) {
  perror("Usage: TCPClient <IP address of the server");
  exit(1);
 }

 //Create a socket for the client
 //If sockfd<0 there was an error in the creation of the socket
 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) <0) {
  perror("Problem in creating the socket");
  exit(2);
 }

 //Creation of the socket
 memset(&servaddr, 0, sizeof(servaddr));
 servaddr.sin_family = AF_INET;
 servaddr.sin_addr.s_addr= inet_addr(argv[1]);
 servaddr.sin_port =  htons(SERV_PORT); //convert to big-endian order

 //Connection of the client to the socket
 if (connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr))<0) {
  perror("Problem in connecting to the server");
  exit(3);
 }

 while (fgets(sendline, MAXLINE, stdin) != NULL) {

  send(sockfd, sendline, strlen(sendline), 0);

  if (recv(sockfd, recvline, MAXLINE,0) == 0){
   //error: server terminated prematurely
   perror("The server terminated prematurely");
   exit(4);
  }
  printf("%s", "String received from the server: ");
  fputs(recvline, stdout);
 }
//close(sockfd);
 exit(0);
}

                                               

No comments:

Post a Comment