基本网络编程,为什么客户端接收不到服务器发送的信息,send函数返回值为0,求帮忙。多谢多谢

unique__1 2014-10-25 08:15:28
下面是源代 码截图:
服务器端:
客户端:

...全文
366 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
unique__1 2014-10-30
  • 打赏
  • 举报
回复
谢谢各位的指点。问题找到了,少了一对括号,由于优先级的问题,导致send的返回值为-1,当然客户端recv返回值就导致为0了,所以才出现我的这个解决。再次谢谢各位。
jiewinli 2014-10-29
  • 打赏
  • 举报
回复
你可以看下你服务器代码在循环接受,而你客户端代码都没有发送数据过去,当然服务端accept之后一至阻塞等待客户端的连接。
hnhujun518 2014-10-29
  • 打赏
  • 举报
回复
你close调用太快了。导致接收端会收到断开信息
轮子学长 2014-10-29
  • 打赏
  • 举报
回复
try to sleep 10s after send();
  • 打赏
  • 举报
回复
引用 6 楼 unique__1 的回复:
引用 5 楼 micropentium6 的回复:
[quote=引用 4 楼 unique__1 的回复:] [quote=引用 3 楼 micropentium6 的回复:] send(connectfd,"Your connect the server. \n",29,0);
哦哦,犯了个低级错误,嘿嘿。可是我改完了 而且recv也用了循环了,结果还是不对:,客户端不输出任何消息,如果recv不用循环,也是这样的结果:客户端不输出任何消息。求指导。
Post ur code then[/quote]这是客户端代码 这是服务器代码 请帮忙看一下我哪不对了,小女子不胜感激。。多谢多谢 [/quote] I don't see any loop around recv, is this the latest code you have for the client? Could you please copy paste your codes to the web page instead of screenshot? If I ever want to try your codes, I would have to literally type every character if it's a screenshot. Be prepared and considerate when you post a question!
ben4058512 2014-10-28
  • 打赏
  • 举报
回复
你可以尝试检查send()函数的返回值,确认数据成功发送。 你的send()函数发送的数据貌似越界了(实际的那个字符长度不足29个字节?), cli的接受函数这里也越界了(接受缓存只有十个字节,服务器端一次发送29个字节,所以如果这里rcv正常返回的话,numbytes的值很有可能是10,那buf[10]在操作的话,就越界了,cli进程竟然没dump)。
unique__1 2014-10-27
  • 打赏
  • 举报
回复
引用 5 楼 micropentium6 的回复:
引用 4 楼 unique__1 的回复:
[quote=引用 3 楼 micropentium6 的回复:]
send(connectfd,"Your connect the server. \n",29,0);
哦哦,犯了个低级错误,嘿嘿。可是我改完了 而且recv也用了循环了,结果还是不对:,客户端不输出任何消息,如果recv不用循环,也是这样的结果:客户端不输出任何消息。求指导。


Post ur code then[/quote]这是客户端代码


这是服务器代码


请帮忙看一下我哪不对了,小女子不胜感激。。多谢多谢
unique__1 2014-10-26
  • 打赏
  • 举报
回复
引用 3 楼 micropentium6 的回复:
send(connectfd,"Your connect the server. \n",29,0);
哦哦,犯了个低级错误,嘿嘿。可是我改完了 而且recv也用了循环了,结果还是不对:,客户端不输出任何消息,如果recv不用循环,也是这样的结果:客户端不输出任何消息。求指导。
  • 打赏
  • 举报
回复
引用 4 楼 unique__1 的回复:
引用 3 楼 micropentium6 的回复:
send(connectfd,"Your connect the server. \n",29,0);
哦哦,犯了个低级错误,嘿嘿。可是我改完了 而且recv也用了循环了,结果还是不对:,客户端不输出任何消息,如果recv不用循环,也是这样的结果:客户端不输出任何消息。求指导。
Post ur code then
  • 打赏
  • 举报
回复
send(connectfd,"Your connect the server. \n",29,0);
  • 打赏
  • 举报
回复
in your serve side C codes, the first argument for function send should be the socket file descriptor of client. In other words, it's the return value from accept, connectfd instead of listenfd. You may also want to know that your client recv buffer is smaller than the incoming data from the server. So, without a loop around recv, you probably can't print all 29 characters on your console...
unique__1 2014-10-25
  • 打赏
  • 举报
回复
引用 楼主 unique__1 的回复:
下面是源代 码截图:
服务器端:
客户端:


运行结果:

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧