majidarif
majidarif

Reputation: 20025

client makes multiple connections to server

I have updated my code to this based on research:

while (number_of_connections--) {
    client_sock = socket(AF_INET , SOCK_STREAM , 0);

    if (connect(client_sock , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        perror("connect failed. Error");
    }

    printf("socket %d created\n", client_sock);

    pthread_t sniffer_thread;
    new_sock = malloc(1);
    *new_sock = client_sock;

    if( pthread_create( &sniffer_thread , NULL ,  connection_handler , (void*) new_sock) < 0)
    {
        perror("could not create thread");
        return 1;
    }
}

Then I'm handling it with this function:

void *connection_handler(void *socket_desc)
{
    //Get the socket descriptor
    int sock = *(int*)socket_desc;
    int read_size, cursor;
    char *message , client_message[2000];

    //Send some messages to the client
    char handshakeBuf[sizeof(handshake)];
    memcpy(handshakeBuf, &handshake, sizeof(handshake));

    handshake.a++;
    handshake.c = 0xac;
    handshake.d = 0x0d;

    //Send some data
    if( send(sock , handshakeBuf , sizeof(handshakeBuf) , 0) < 0)
    {
        puts("Send failed");
    }

    //keep communicating with server
    while(1)
    {
        //Receive a reply from the server
        if( recv(sock , client_message , 2000 , 0) < 0)
        {
            puts("recv failed");
            break;
        }

        puts(client_message);
    }

    close(sock);

    return 0;
}

Now my problem is why does it suddenly stop when it reaches the 4th connection?

Original Question

I am trying to write my first C client. I needed to create 4 connections to the server from one client to simulate 4 clients connected where each connection of course has its own handler.

Here is what I have so far:

void connect_to_server(struct sockaddr_in server);

int main(int argc , char *argv[])
{
    int number_of_connections, x;
    struct sockaddr_in server;
    server.sin_addr.s_addr = inet_addr("ipaddress");
    server.sin_family = AF_INET;
    server.sin_port = htons( port );

    number_of_connections = 4;

    for ( x = 0; x < number_of_connections; x++ ) {
        connect_to_server(server);
    }

    return 0;
}

void connect_to_server(struct sockaddr_in server) {
    int sock;

    char message[1000] , server_reply[2000];

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

    if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0)
    {
        perror("connect failed. Error");
    }

    while(1)
    {
        scanf("%s" , message);

        if( send(sock , message , strlen(message) , 0) < 0)
        {
            puts("Send failed");
        }

        if( recv(sock , server_reply , 2000 , 0) < 0)
        {
            puts("recv failed");
            break;
        }

        puts(server_reply);
    }

    close(sock);
}

Well I'm obviously also new to C. So what is wrong? Should I declare more sockets like: int sockA, sockB, sockC, sockD or I guess its the while loop inside the connect_to_server?

Upvotes: 0

Views: 55

Answers (1)

It seems the new version of the implementation uses multithreading: pthread_create(...) function call. But is there an implementation of waiting after the threads are created?

For example, the waiting can be implemented:

  • by using pthread_join(...) function call;
  • by waiting for specific key press event using getchar() function call.

Notes

Please be careful with these statements:

if (send(sock, handshakeBuf, sizeof(handshakeBuf), 0) < 0)
if (recv(sock, client_message, 2000 , 0) < 0)

The send() and recv() functions do not guarantee that the entire buffer will be sent/received after one function call. The functions return the actual number of sent/received bytes.

Please introduce analysis of the returned value for send() and recv() function calls: continue sending if not all bytes are sent, continue receiving if "not enough" bytes are received. Also, there is an article related to the some basics of the network programming: TCP/IP client-server application: exchange with string messages.

Upvotes: 1

Related Questions