为什么我的客户没有收到任何字节?

外频

我正在编写一个简单的客户端-服务器程序。我的服务器可以和telnet一起正常工作,但是我的客户端只能发送字节,不能接收字节。有什么明显的我想念的东西吗?我认为可能是由于我recv在某种程度上使用了该函数,但对我而言并不明显。recv函数始终获得0字节。我应该补充一点,recv函数不会阻塞,但是接收到的字节数始终为零。

客户端主要功能中的代码片段如下。该功能changeIPOrPort设置服务器的IP地址和端口,并且由于服务器接收到客户端消息,因此可以正常工作。如果需要,我也可以发布服务器代码。

int main() {

int client_fd, numbytes = 0;
//int quit = 0;
struct sockaddr_in their_addr;
char response[MAXDATASIZE] = "";
char buf[MAXDATASIZE] = "";
size_t len = 0;

/* Server config*/
their_addr.sin_family = AF_INET;
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
printf("Configuring the server\n");
changeIPOrPort(&their_addr);


printf("Hello! please input letters to know things about me.\n");
printf("Each character will give you a piece of information about me\n");
printf("%s\n", serviceInfo); //info string "UI"...

/*create client socket*/
client_fd = socket(AF_INET, SOCK_STREAM, 0);

if(connect(client_fd, (struct sockaddr *)&their_addr, sizeof(their_addr)) < 0) {
    printf("\n Error : Connect Failed \n");
    return 1;
}

if(!fgets(response, sizeof (response), stdin)) {
    printf("Error reading line.\n");
}
else {
    len = strlen(response);
    if(response[len - 1] == '\n') {
        response[len - 1] = '\0';
    }
    printf("You entered the characters:%s\n", response);
    printf("sending...\n");
    send(client_fd, response, len, 0);

    numbytes = recv(client_fd, buf, strlen(buf), 0) ;
    if(numbytes < 0) {
        printf("Error receiving from server, quitting\n");
        exit(1);
    }
    else {
        printf("Number of bytes received: %d\n", numbytes);
        buf[numbytes] = '\0';
        printf("%s\n", buf);
        printf("This is errno\n", errno);
    }
}
close(client_fd);
return 0;
}
马丁·詹姆斯

将'strlen(buf)'替换为'sizeof(buf)-1'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么我没有从我的 TCP 服务器和客户端收到任何消息?

为什么我没有从JS客户端(作为Chrome扩展程序)收到任何信息?

为什么getchar()没有收到任何输入?

为什么我没有收到任何错误在下面的代码?

为什么即使调用了异常,我也没有收到任何错误消息?

为什么我的C ++ ZeroMQ订阅者没有收到任何数据?

为什么我的接收者的类实例方法没有收到任何数据?

为什么我没有得到任何输出?

为什么我没有得到任何输出

为什么我没有得到任何输出

Rails 5:提交表单时我没有收到任何错误,但是我的数据没有存储在数据库中。为什么?

我没有收到任何警报消息

我的片段在RecycleView中没有收到任何值

为什么我没有得到任何加速度计更新?

为什么我在Java FX中没有得到任何规模转换?

Wiremock 没有收到任何请求

TcpListener 没有收到任何数据

为什么我没有从我的网络请求中得到任何东西?我应该寻找什么?

我没有收到任何错误,但是数字没有添加到JTextField中

CAPL:为什么我没有在 CAPL 中接收到 CAN 消息的数据字节?

为什么我没有收到插槽?

当我的输入变量超过 1 个字符时,为什么我没有得到任何结果?

为什么我没有得到任何输出,对于我在链表中插入的代码?

为什么我没有得到任何输出?并且有一个 InputMismatchException

触发Webhook时,我没有从trello收到任何有效载荷

为什么我没有收到使用带 udp 套接字的 BPF 的任何数据包?

为什么我会收到任何 Java 代码的“错误:无法找到或加载主类”?

Google Maps API v3 JS:为什么我没有从该函数得到任何回报?

德语版 Google 交易会导致“抱歉,我没有收到任何回复”。