i want to create a client server chat application in c using linux.. i want to create two threads in client and server programms. one for send and other for recv.. iam new to threading .. please let me know how i should create it? here is my server code..
#include <stdlib.h>
#include <stdio.h>
#include <string.h> //strlen
#include <sys/socket.h>
#include <arpa/inet.h> //inet_addr
#include <unistd.h> //write
#define port 8877
int main(int argc , char *argv[])
{
int socket_desc , client_sock , c , client_reply;
struct sockaddr_in server , client;
char client_message[5000];
char repltocli[6000];
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0);
//Prepare the sockaddr_in structure
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons( port);
//Bind
if( bind(socket_desc,(struct sockaddr *)&server , sizeof(server)) < 0)
{
//print the error message
perror("bind failed. Error");
return 1;
}
puts("bind done");
//Listen
listen(socket_desc , 9);
//Accept and incoming connection
puts("Waiting for incoming connections...");
c = sizeof(struct sockaddr_in);
//accept connection from an incoming client
client_sock = accept(socket_desc, (struct sockaddr *)&client, (socklen_t*)&c);
if (client_sock < 0)
{
perror("accept failed");
return 1;
}
puts("Connection accepted");
puts("press ctrl+c to terminate the application");
while(1)
{
//Receive a message from client
recv(client_sock , client_message , 5000 , 0);
puts("messege recived from client :");
puts(client_message);
memset(client_message, 0, sizeof(client_message));
printf("enter your message : ");
fgets(repltocli, 6000,stdin);
//Send some data
send(client_sock, repltocli , strlen(repltocli) , 0) ;
memset(repltocli, 0, sizeof(repltocli));
}
return 0;
}