0

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;
}
Juan Cespedes
  • 1,299
  • 12
  • 27
Zeeshan Zeb
  • 3
  • 1
  • 3
  • It isn't clear whether you're asking how to write multi-threaded code, how to write the client side of this server/client conversation, or possibly both (or neither). What *exactly* is the problem you're having? – WhozCraig Aug 28 '14 at 07:22
  • 2
    Learn PThreads, or C11's Threads and learn how to handle TCP streams, especially if messages shall be transferred over it. – alk Aug 28 '14 at 07:23
  • Referring the code you show: If you got this from a tutorial, switch to a better one. The way receiving and sending is implemented, is totally unreliable, at least if doing this via a TCP stream, as it does. – alk Aug 28 '14 at 07:27
  • @WhozCraig i want to add two threads to this server program.. one for sending data and one for receiving data.. – Zeeshan Zeb Aug 28 '14 at 07:38
  • @alk i will make this code more reliable as my knowledge increase.. if you have better idea for implementing send and receive please do share with me.. i will be thankfull to you.. – Zeeshan Zeb Aug 28 '14 at 07:42
  • This answer http://stackoverflow.com/a/24536689/694576 discusses a major issue in your code, that is not handling the value returned by `send()` and `recv()`. – alk Aug 28 '14 at 07:48
  • @ZeeshanZeb You should take a look at some example c servers. There are many available online: http://www.martinbroadhurst.com/server-examples.html#simple-server – Malt Aug 28 '14 at 07:58
  • Read some good [Pthread tutorial](https://computing.llnl.gov/tutorials/pthreads/) – Basile Starynkevitch Aug 28 '14 at 08:12

2 Answers2

3

For dealing with threads you need to create thread functions, return type should be void * and with/with out an argument. But if you use arguments, arguments should be void *

Thread to receive message-

void *receive_message(){
            while(1){
                    //Receive a message from client
                    recv(client_sock , client_message , 5000 , 0);
                    //puts("messege recived from client :"); // Comment this line out. Else it will annoye the user by printing every time
                    puts(client_message);

                    memset(client_message, 0, sizeof(client_message));
            }
}

Thread to send message-

void *send_message(){
            while(1){       
                    // printf("enter your message : "); // Comment this line out. Else it will annoye the user by printing every time
                    fgets(repltocli, 6000,stdin);
                    //Send some data
                    send(client_sock, repltocli , strlen(repltocli) , 0) ;

                    memset(repltocli, 0, sizeof(repltocli));
            }
}

And declare repltocli array, client_sock and client_message array globally, because your threads also needs it!

In your main() declare two pthread_d variable for threads-

int main(){

pthread_d thread_send, thread_recv;

// Do your stuff like socket, bind, listen and accept!

// Create these two threads and make sure that your main program should be alive-

pthread_create(&thread_send, NULL, send_message, NULL);
pthread_create(&thread_recv, NULL, receive_message, NULL);

while(1); // Press ctrl + C to terminate!
return 0;
}

Do this same thing for another side also.

Sathish
  • 3,740
  • 1
  • 17
  • 28
1

Simple idea is to have each thread on each request.

Algorithm

  1. Loop for the request
  2. When ever you are getting a request ,create a thread to process the data.

Thread function will handle the request

something like bellow will work in server code

while(1)
{
 //Get the request
 //Create a thread to handle the request 
}

Please read about Pthread library for linux.

pradipta
  • 1,718
  • 2
  • 13
  • 24