Wednesday, 9 October 2013

Client Server Chat application with multithreading in C

Server.c


#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <pthread.h>
#include <string.h>
int sockfd, newsockfd, portno, clilen,newsockfd1;
char buffer[256],buff[255];
struct sockaddr_in serv_addr, cli_addr;
int n,f,b;
int main( int argc, char *argv[] )
{

// time_t   now;
  //   struct tm *ts;
// now = time(0);
// ts = localtime(&now);

// strftime(buff, sizeof(buff), "%a %Y-%m-%d %H:%M:%S %Z", ts);
// printf("%s\n", buff);
/* First call to socket() function */
sockfd = socket(AF_INET, SOCK_STREAM, 0);

/* Initialize socket structure */
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = 5001;
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);




/* Now bind the host address using bind() call.*/

bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr));

/* Now start listening for the clients, here process will * go in sleep mode and will wait for the incoming connection */
listen(sockfd,5);
clilen = sizeof(cli_addr);


/* Accept actual connection from the client */
newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
newsockfd1=newsockfd;


/* If connection is established then start communicating */

//printf("Reply For Client : Please enter the message: ");
//fgets(buffer,255,stdin);
pid_t p;
p=fork();
if(p>0)
{
while(1==1){
n = read(newsockfd,buffer,255 );
printf("%s",buffer);
bzero(buffer,256);
}
}
else
{
while(1==1){
fgets(buff,255,stdin);

//n = write(newsockfd,buffer,255);
n = write (newsockfd,buff,255);
bzero(buff,256);}
}


return 0;
}



Client.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
int sockfd, portno, n,b;
struct sockaddr_in serv_addr;
//struct hostent *server;
char buffer[256],buff[256];

int main(int argc, char *argv[])
{
if (argc < 3)
{
fprintf(stderr,"usage %s hostname port\n", argv[0]);
return 0;
}
portno = atoi(argv[2]);

/* Create a socket point */
sockfd = socket(AF_INET, SOCK_STREAM, 0);

bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(portno);
inet_pton(AF_INET,argv[1],&serv_addr.sin_addr);



/* Now connect to the server */
connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr));

/* Now ask for a message from the user, this message * will be read by server */
pid_t p;
p=fork();
if(p>0){
while(1==1)
{
// printf("");
bzero(buffer,256);
fgets(buffer,255,stdin);

/* Send message to the server */
n = write(sockfd,buffer,255);
}
}
else
{
while(1==1){
/* Now read server response */
n = read(sockfd,buff,255);
printf("%s",buff);
bzero(buff,256);
}}
return 0;
}


Input Search:
  • Client Server Multithreading
  • Multithreading Socket Programing in C
  • Client  Server Socket Programing
  • Client Server Multithreading Socket Programming.

3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. how should i run this code?
    should i need to attach thread libraries while running?

    ReplyDelete
  3. Keep up the good work; I read few posts on this website, including I consider that your blog is fascinating and has sets of the fantastic piece of information. Thanks for your valuable efforts. Allo Talk

    ReplyDelete