LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-24-2012, 01:14 AM   #1
fahad.anwar
LQ Newbie
 
Registered: May 2012
Posts: 18

Rep: Reputation: Disabled
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;
}
 
Old 07-24-2012, 04:07 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,772

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
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));
        }

Last edited by NevemTeve; 07-24-2012 at 04:10 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Client/Server chat program joyce092130 Programming 16 07-16-2012 08:51 AM
Client & server program ZuzooVn Programming 5 12-09-2009 05:39 AM
server client program swift2008 Programming 10 11-26-2008 12:12 PM
How to get IP address of client from server.c program nazsarwat Programming 1 04-22-2005 03:57 PM
Server Client program using UDP frostmagic Programming 9 11-14-2003 11:06 PM


All times are GMT -5. The time now is 03:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration