user3809727
user3809727

Reputation: 3

C TCP Server doesn't send data before closing

I'm trying to set up a TCP connection between two Ubuntu computers. The server is supposed to send single char-values to the client. The client is supposed to print these chars. Establishing the connection between server and client seems to work just as expected but when I call send(), there is no output on the clientside.

The only way to achieve an output is by either sending the same char-value in an infinite loop, which leads to an infinite number of chars printed in the client's console, or by changing the if statement in the client's code (see code below) from if(len > 0) to if(len >= 0). In this case the sent chars will be printed correctly as soon as I close the server via CTRL + C, but it will also print the last transmitted char multiple times. So I'm not able to receive the values on clientside while the server is still running.

This is the server's code:

int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in adresse;
adresse.sin_family = AF_INET;
adresse.sin_addr.s_addr = INADDR_ANY;
adresse.sin_port = htons (2457);

bind ( serverSocket, (struct sockaddr *) &adresse, sizeof (adresse))
listen (serverSocket, 1);

while(1)
{
socklen_t adrlen = sizeof(struct sockaddr_in);
int conSocket= accept ( serverSocket, (struct sockaddr *) &adresse, &adrlen ); 
if(conSocket => 0)
{
    char charToSend;
    while(1)
    {
        charToSend = returnChar();
        if(send(conSocket= , &charToSend, 1, 0) == -1)
        {
            printf("Error\n"); 
        } 
    }
    close(conSocket);           
}
close(serverSocket);
}

This is the client's code:

int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in adresse;
adresse.sin_family = AF_INET;
adresse.sin_port = htons (2457);
inet_aton ("192.168.2.101", &adresse.sin_addr);

if (connect ( clientSocket, (struct sockaddr *) &adresse, sizeof (adresse)) == 0)
{               
while(1)
{
    char recvd[2];
    int len = recv(clientSocket, recvd, 1, 0);
    recvd[1] = '\0';
    if(len > 0)
    {
            printf("%s", recvd);
    }
}
}

The function returnChar() in the server's code is used to process information from a sensor and returns a char value. It won't terminate before a complete signal has been processed and is running a loop in the meantime.

My only idea is that send() "doesn't have the time" to send out the values before the programm continues in another loop. Could this be the problem? Or did I do something wrong in the client's code?

Upvotes: 0

Views: 667

Answers (2)

John Hascall
John Hascall

Reputation: 9416

Actually, the problem is probably on the client side. Make your client's output unbuffered:

setvbuf(stdout, NULL, _IONBF, (size_t)0);

or simply use unbuffered, rather than standard (buffered) I/O:

write(fileno(stdout), recvd, (size_t)1);

Although, since your traffic is 1-way, the suggestion to use TCP_NODELAY will help speed the sending a bit.

Upvotes: 0

dmi
dmi

Reputation: 1479

In my opinion the problem is on the client side. You print the char with no line termination. Therefore everything is buffered in the client stdout stream. Please add '\n' to the printf as: printf("%s\n", recvd);

Upvotes: 3

Related Questions