Home > Back-end >  How to receive data from client on server via sockets
How to receive data from client on server via sockets

Time:02-20

I am trying to send data from my client to server, but my server isn't receiving the message, I am not sure why. The client receives Connected message and it's displaying. How can I get the server to receive messages from client? Also, should I use close() function in the server for loop?

Client

int main(){

    char server_response[256];
    char send_msg[256] = "Sending to server";
    int server;

    server = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_port = htons(9002);
    address.sin_addr.s_addr=INADDR_ANY;

    int con = connect(server,(struct sockaddr*) &address, sizeof(address));
    if(con == -1) {
        printf("Error with connection");
        return -1;
    }
    recv(server, server_response, sizeof(server_response), 0);
    int toServer = send(server, send_msg, sizeof(send_msg), 0);
    if(toServer == -1) {
        perror();
    }
    printf("%s", server_response);
    return 0;
}

Server

int main(){

    char server_response[256] = "Connected";
    int server;
    char from_client[256];
    server = socket(AF_INET, SOCK_STREAM, 0);
    if(server == -1) {
        perror();
        return -1;
    }
    struct sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_port = htons(9002);
    address.sin_addr.s_addr=INADDR_ANY;

    int bindx = bind(server,(struct sockaddr*) &address, sizeof(address));
    if(bindx == -1) {
        perror();
        return -1;
    }
    listen(server, 5);
    for(;;) {
        int client_socket = accept(server, NULL, NULL);
        send(client_socket, server_response, sizeof(server_response), 0);
        recv(client_socket, from_client, sizeof(from_client), 0);
        printf("%s", from_client);
    }
    return 0;
}

CodePudding user response:

It seems you have some synchronization problem. That is either server and client both are in send mode or both in recv mode. It's better to follow a request-response pattern which means server be in listening mode after accept the client. So change the server code to recv/printf/send pattern and client to send/receive mode or just send mode.

Server:

int main(){

    char server_response[256] = "Connected";
    int server;
    char from_client[256];
    server = socket(AF_INET, SOCK_STREAM, 0);
    if(server == -1) {
        perror();
        return -1;
    }
    struct sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_port = htons(9002);
    address.sin_addr.s_addr=INADDR_ANY;

    int bindx = bind(server,(struct sockaddr*) &address, sizeof(address));
    if(bindx == -1) {
        perror();
        return -1;
    }
    listen(server, 5);
    for(;;) {
        int client_socket = accept(server, NULL, NULL);
        recv(client_socket, from_client, sizeof(from_client), 0);
        printf("%s", from_client);
        send(client_socket, server_response, sizeof(server_response), 0);
    }
    return 0;
}

Client:

int main(){

    char server_response[256];
    char send_msg[256] = "Sending to server";
    int server;

    server = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_port = htons(9002);
    address.sin_addr.s_addr=INADDR_ANY;

    int con = connect(server,(struct sockaddr*) &address, sizeof(address));
    if(con == -1) {
        printf("Error with connection");
        return -1;
    }
    int toServer = send(server, send_msg, sizeof(send_msg), 0);
    if(toServer == -1) {
        perror();
    }
    recv(server, server_response, sizeof(server_response), 0);
    printf("%s", server_response);
    return 0;
}

And stare server before client. See online demo provided.

  •  Tags:  
  • c
  • Related