简体   繁体   English

C TCP Server在关闭前不发送数据

[英]C TCP Server doesn't send data before closing

I'm trying to set up a TCP connection between two Ubuntu computers. 我正在尝试在两台Ubuntu计算机之间建立TCP连接。 The server is supposed to send single char-values to the client. 服务器应该向客户端发送单个char值。 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. 在服务器和客户端之间建立连接似乎按预期工作,但是当我调用send()时,客户端没有输出。

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). 实现输出的唯一方法是在无限循环中发送相同的char值,这会导致在客户端控制台中打印出无限数量的char,或者通过更改客户端代码中的if语句(请参见下面的代码)从if(len> 0)到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. 在这种情况下,一旦我通过CTRL + C关闭服务器,发送的字符将被正确打印,但是它也会多次打印最后发送的字符。 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. 服务器代码中的函数returnChar()用于处理来自传感器的信息并返回char值。 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. 我唯一的想法是send()“没有时间”在程序继续在另一个循环中之前发送值。 Could this be the problem? 这可能是问题吗? Or did I do something wrong in the client's code? 还是我在客户代码中做错了什么?

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. 因此,所有内容都缓存在客户端stdout流中。 Please add '\\n' to the printf as: printf("%s\\n", recvd); 请将'\\n'添加到printf中,如下所示: printf("%s\\n", recvd);

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: 或简单地使用无缓冲的I / O,而不是标准的(缓冲的)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. 尽管由于您的流量是单向的,所以建议使用TCP_NODELAY将有助于加快发送速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM