Home > Mobile >  C client doesn't receive from server
C client doesn't receive from server

Time:12-04

I made a simple server client program with select() but the problem is that the client doesn't receive the welcome message. I did this several times earlier without select() and it worked, but all of a sudden it doesn't want to work when working with select(). What am I doing wrong?

Server:

#define MAX_CLIENTS 10

int main()
{
    system("clear");
    printf("***SERVER STARTED***\n");

    int master_socket = socket(AF_INET, SOCK_STREAM, 0);
    fd_set fdsForReading;

    struct sockaddr_in address;
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = INADDR_ANY;
    address.sin_port = htons(8888);
    bind(master_socket, (struct sockaddr *)&address, sizeof(address));
    listen(master_socket, 3);

    while (1)
    {
        FD_ZERO(&fdsForReading);               // Clearing the file descriptor set
        FD_SET(master_socket, &fdsForReading); // Add master_socket to file descriptor set

        printf("Monitoring for connections...\n");
        select(master_socket   1, &fdsForReading, NULL, NULL, NULL);

        int accepted_socket = accept(master_socket, (struct sockaddr *)&address, sizeof(address));
        printf("[ ] New connection accepted\n");

        char buffer[125] = "Welcome to my server";
        send(accepted_socket, buffer, sizeof(buffer), 0);

        printf("End of while loop\n");
    }
}

Client:

int main()
{
    system("clear");
    int clientSocket = socket(AF_INET, SOCK_STREAM, 0), x;

    struct sockaddr_in serverAddress;
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(8888);
    serverAddress.sin_addr.s_addr = INADDR_ANY;

    connect(clientSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress));

    char serverResponse[125];
    recv(clientSocket, &serverResponse, sizeof(serverResponse), 0);
    printf("test: %s\n", serverResponse);

    scanf("%d", &x);

    close(clientSocket);
}

CodePudding user response:

  • You don't check for errors. Always do!
  • &serverResponse should be serverResponse
  • recv may receive fewer characters than you sent so you should check how many characters you received and possibly concatenate what you receive until you have the full message.
  • What you receive will usually not be null terminated so you need to add \0 if what you receive is to be used with functions like printf("%s", ... that require null terminated strings. In your case you actually send 125 chars even though the string is much shorter. You should probably not do that. This will send the string the null terminating character:
    send(accepted_socket, buffer, strlen(buffer)   1, 0);
    
  • The third argument to accept should be a socklen_t * - not sizeof(address). Example:
    socklen_t addrlen = sizeof address;
    int accepted_socket = accept(master_socket, (struct sockaddr *)&address, &addrlen);
    

With those changes, the client will receive the greeting fine given that no errors occurs - but when you've added error checking you will see where the problem is in that case.

  • Related