Skip to content
Advertisement

TCP echo server doesn’t accept client

I’m trying to make an echo client server in C. The problem is, when I start the server and run the client code, in client shell it says “Connected” but in server shell it still says “Waiting for connection”. (Server doesn’t say “accept failed”. It says “Waiting for connection” forever). I think server is not accepting client and therefore if I write anything from client shell it doesn’t come back.

I would appreciate if I can get any help.

Here is server code:

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
    int socket_desc, client_sock, c, read_size;
    struct sockaddr_in server, client;
    char client_message[2000];

    //create socket
    socket_desc = socket(AF_INET, SOCK_STREAM, 0);
    if(socket_desc == -1)
    {
        printf("Socket couldn't createdn");
    }

    puts("Socket created");

    //prepare the sockaddr_in structure
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons( 8888 );

    //bind
    if( bind(socket_desc,(struct sockaddr *)&server, sizeof(server)) < 0)
    {
        //print the error message
        perror("bind failed");
        return 1;
    }
    puts("bind done");

    //Listen
    listen(socket_desc , 3)


    //Accept and incoming connection
    puts("Waiting for incoming connections...");
    c = sizeof(struct sockaddr_in);

    while(1)
    {
        //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");

    //Recieve message from client
    while( (read_size = recv(client_sock , client_message , 2000 , 0))>0)
    {
        //Send the message back to client
        write(client_sock , client_message , strlen(client_message));

    }
    if(read_size == 0)
    {
        puts("Client disconnected");
    }
    else if(read_size == -1)
    {
        perror("recv failed");
    }

    return 0;
}

Here is the client code:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/socket.h>
#include <arpa/inet.h>

int main(int argc , char *argv[])
{
    int sock;
    struct sockaddr_in server;
    char message[1000] , server_reply[1000];

    //Create socket
    sock = socket(AF_INET , SOCK_STREAM , 0);
    if(sock == -1)
    {
        printf("Could not create socket");
    }
    puts("Socket created");

    server.sin_addr.s_addr = inet_addr("127.0.0.1");
    server.sin_family = AF_INET;
    server.sin_port = htons( 8888 );

    //Connect to remote server
    if(connect(sock, (struct sockaddr *)&server, sizeof(server))<0)
    {
        perror("connect failed");
        return 1;
    }
    puts("Connectedn");

    //Keep communicating with server
    while(1)
    {
        printf("Enter message: ");
        scanf("%s", message);

        //Send some data
        if(send(sock, message, strlen(message), 0)<0)
        {
            puts("Send failed");
            return 1;
        }
        //Recieve a reply from server
        if( recv(sock, server_reply, 1000, 0)<0)
        {
            puts("recv failed");
        }
        puts("Server reply: ");
        puts(server_reply);
        bzero(message, 1000);
        bzero(server_reply, 1000);
    }
    close(sock);
    return 0;

}

Advertisement

Answer

You accept new clients in a infinite loop. So even if a client is detected the code will wait for an other client to connect.

If you want to make a single client server you can just remove the loop around the accept(). Example here

If you want to make a multiple client server you can thread your accept and store all your opened file descriptor. Example here

Advertisement