LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Client Server program in C (https://www.linuxquestions.org/questions/programming-9/client-server-program-in-c-4175418341/)

fahad.anwar 07-24-2012 01:14 AM

Client Server program in C
 
Hi all

I am trying to run a client server program. I have one problem. When I am closing the client connection then the server connection should also close. Instead its going into an infinite loop. Help would be appreciated. Below is the server code :


Code:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>

#define PORT 9000
#define ARRAY_SIZE 5

struct client_handler
{
        unsigned int status;
        int client_sock;
        pthread_t thread_id;
};

void* send_recv(void* arg)
{
        struct client_handler *sock = (struct client_handler *)arg;
        char buffer[100];
        int bytes_read,bytes_sent;

        while(1)
        {
                printf ("TCP server waiting for receive the data from client\n");
                bytes_read = recv(sock->client_sock, buffer, sizeof(buffer), 0);
                if(bytes_read <=0)
                {
                        close(sock->client_sock);
                        sock->status = 0x1;
                        break;
                }

                buffer[bytes_read] = '\0';
                printf("Server Received Data:%s\n", buffer);

                bytes_sent = send(sock->client_sock, buffer, bytes_read, 0);
                if(bytes_sent < 0)
                {
                        close(sock->client_sock);
                        sock->status = 0x0;
                        break;
                }
                printf ("TCP server sent the received data to client\n");
        }
        pthread_kill();
}

int main(void)
{

        int server_sock, client_sock, t, len,remote_len;
        struct sockaddr_in local, remote;

        struct client_handler client_thread[ARRAY_SIZE];

        if((server_sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
                perror("server socket creation failed");
                exit(1);
        }

        memset(&local, 0, sizeof(local));
        local.sin_family = AF_INET;
        local.sin_addr.s_addr = INADDR_ANY;
        local.sin_port = htons(PORT);

        if(bind(server_sock, (struct sockaddr *)&local, sizeof(struct sockaddr)) == -1)
        {
                perror("bind Error");
                exit(1);
        }
        if(listen(server_sock,5) == -1)
        {
                perror("listen Error");
                exit(1);
        }

        while(1)
        {
                printf ("waiting for a new connection\n");
                remote_len = sizeof(remote);
                if((client_sock = accept(server_sock, (struct sockaddr*)&remote, &remote_len)) == -1)
                {
                        perror("accept");
                        exit(1);
                    printf("Client Connected\n");

                for(t=0; t < ARRAY_SIZE; t++)
                {
                        if(client_thread[t].status == 0x0)
                        {
                                client_thread[t].status = 0x1;
                                client_thread[t].client_sock = client_sock;
                                pthread_create (&client_thread[t].thread_id,NULL,&send_recv,(void*)&client_thread[t]);
                                printf ("New client thread created, Thread ID:%d\n",client_thread[t].thread_id);
                                break;
                        }
                }

        }

        return 0;
}

                  }



following is the client code :
Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>


int main(void)
{
        int s, t, len;
        struct sockaddr_in remote;
        char str[100];

        if((s = socket(AF_INET, SOCK_STREAM, 0)) == -1)
        {
                perror("socket");
                exit(1);
        }

        printf("Trying to Connect.....");

        memset(&remote, 0, sizeof(remote));
        remote.sin_family = AF_INET;
        remote.sin_addr.s_addr = INADDR_ANY;
        remote.sin_port = htons(9000);
        if(connect(s, (struct sockaddr *) &remote, sizeof(struct sockaddr)) == -1)
        {
                perror("connect");
                exit(1);
        }

        printf("connected");

        while(printf(">"), fgets(str, 100, stdin), !feof(stdin))
        {
                if(send(s, str, strlen(str), 0) == -1)
                {
                        perror("send");
                        exit(1);
                }
                if((t = recv(s, str, 100, 0)) > 0)
                {
                        str[t] = '\0';
                        printf("Echo > %s", str);
                }
                else
                {
                        if(t < 0)
                                perror("recv");
                        else
                                printf("Server Closed Connection");
                        exit(1);
                }
        }
        close(s);
        return 0;
}


NevemTeve 07-24-2012 04:07 AM

For start, compile with -W -Wall -Wextra -pedantic, and fix every warning

Then fix your code:
Code:

                if((client_sock = accept(server_sock, (struct sockaddr*)&remote, &remote_len)) == -1)
                {
                        perror("accept");
                        exit(1);
>>>> Missing line here <<<<
                    printf("Client Connected\n");

Finally insert these lines before "bind":

Code:

        {
        int opt = 1;
        setsockopt (server_sock, SOL_SOCKET, SO_REUSEADDR, (char *)&opt, sizeof (opt));
        }



All times are GMT -5. The time now is 12:17 AM.