1

编辑:老实说,我不知道错误发生在哪里,所以我将添加大部分相关代码,不确定这是否有帮助

所以基本上我有两个基本的服务器和客户端进程。用户指定一个命令,客户端进程然后将消息的长度发送给服务器,服务器执行命令并返回响应的消息长度。

这在第一个命令上效果很好(我连续尝试了两个 ls -la),第一个命令的值为 1028(正确的值),第二个命令的值为 1685223288(在客户端)不正确

使用以下代码完成写入:

server_handler() {
char str[5000];
char * token = NULL;
unsigned long totalSize = 0;
str[0] = "\0";
totalSize = htonl(totalSize); // Correct size already calculate
if((result = write(clientFD, (char *) &totalSize, sizeof(totalSize)) < 0))
{
     printf("Failed sending size to client\n");
     exit(-1);
}
//Here the server starts sending the results to the client
token = strtok(str, "\n"); // str array contains the results
while(token != NULL)
{
     token[strlen(token)] = '\n';
     write(clientFD, token, strlen(token));
     token = strtok(NULL, "\n);
}

读取在客户端进程中通过以下方式完成:

static void handle_reply() 
{
char * revBuf = 0;
unsigned long int bytesRead = 0;
unsigned long bufferlen = 0;
while(1) 
{
    result = read(socketFD, (char *) &bufferlen, sizeof(bufferlen));
    bufferlen = ntohl(bufferlen);
    recvBuf = malloc(bufferlen);
   //Here client starts reading results
    while(bytesread < bufferlen)
    {
        result = read(socketFD, recvBuf, bufferlen);
        printf("recvBuf: %s\n", recvBuf);
        bytesRead = strlen(recvBuf) + bytesRead;
    }
    free(recvBuf);
    bufferlen = 0;
   client_handler(); //calls Function that asks for new command
}
}

我的问题:为什么我在第一个命令之后收到错误的值?, 我已经验证了在两种情况下,totalSize 在服务器端都具有正确的值并打印。写/读一定有问题?

我还在服务器上打印了 htonl(totalSize),它是 67371008。但是在客户端收到的值是 ntohl 之前的 2021093988。

4

2 回答 2

1

以下代码将无法正常工作,因为您strlen在修改字符串后再次调用。

token = strtok(str, "\n");
while(token != NULL)
{
    token[strlen(token)] = '\n';
    write(clientFD, token, strlen(token));
    token = strtok(NULL, "\n");
}

为了说明,假设str最初是"hello\nworld\n"。在十六进制中

 68 65 6c 6c 6f 0A 77 6f 72 6c 64 0A 00
                ^---- the first newline

之后strtok

 68 65 6c 6c 6f 00 77 6f 72 6c 64 0A 00
                ^---- strtok changed it to a NUL

行后token[strlen(token)] = '\n'它将是

 68 65 6c 6c 6f 0A 77 6f 72 6c 64 0A 00
                ^---- you changed it back to a newline

所以现在strleninwrite将返回 12,因此write将发送 12 个字节,而不是您期望的 6 个字节。这可以通过调用strlen一次来修复,就像这样

token = strtok(str, "\n");
while(token != NULL)
{
    size_t length = strlen(token);
    token[length] = '\n';
    write(clientFD, token, length);
    token = strtok(NULL, "\n");
}
于 2015-03-05T22:16:43.873 回答
0

htonl 和 ntohl 是 32 位...

  uint32_t htonl(uint32_t hostlong);
  uint32_t ntohl(uint32_t netlong);

看看使用 64 位系统的程序处理大整数会发生什么。

#include <stdio.h>

int main(int argc, char** arg)
{
  unsigned long bufferlen = 0;
  unsigned long totalSize = 1024;
  sscanf(arg[1],"%lld",&totalSize);
  printf("%lld  %llx\n", totalSize,totalSize);

  totalSize = htonl(totalSize); // 32 <-> 64bits .
  bufferlen = ntohl(totalSize);

  printf("%lld %llx %lld %llx\n", totalSize,totalSize,bufferlen,bufferlen);

}

测试它:

./test 111111111111111
111111111111111  650e124ef1c7
-940487150 ffffffffc7f14e12 307163591 124ef1c7
于 2015-03-05T19:57:37.580 回答