通过 TCP 客户端套接字接收数据的问题 [英] Problems receiving data over a TCP client socket

查看:30
本文介绍了通过 TCP 客户端套接字接收数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 C 编写一个 TCP 客户端程序,客户端将在其中启动,连接到服务器.然后它会发送一些信息,然后只听它收到的信息并做出相应的反应.

I'm trying to make a TCP Client program in C where the client will start up, connect to a server. Then it will send a little information and then just listen to what it receives and react accordingly.

我遇到麻烦的部分是持续聆听.这是我所拥有的

The part that I'm having trouble with is the continuous listening. Here is what I have

...

while (1) {
   numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0);
   buf[numbytes] = '\0';
   printf("Received: %s\n", buf);
   // more code to react goes here
}

...

连接到服务器后,发送两行数据后,服务器应该会收到一些信息,但是当我运行它时,它会打印:

Upon connecting to the server, after sending two lines of data, the server should receive a good bit of information, but when I run this, it prints:

收到:

然后继续坐在那里直到我强迫它关闭.

And then continues to just sit there until i force it to close.

** EDIT ** 当我按照乔纳森告诉我的去做时,我得到以下信息:

** EDIT ** when i do what Jonathan told me to do, I get the following:

计数:-1,错误:111,收到:

Count: -1, Error: 111, Received:

所以这意味着它的错误,但我该怎么办?

So that means its erroring, but what do i do about it?

推荐答案

打印接收到的字节数 - 它可能为零,但请确认.

Print out the number of bytes received - it is likely to be zero, but confirm that.

值得检查您没有收到错误 - 从而导致缓冲区下溢.

It would be worth checking that you aren't getting an error - and therefore underflowing your buffer.

[注意:从这里开始是 Pax 的作品 - 谢谢,我已将其转换为 Community Wiki,因此我不会不当地获得代表点数.]

[Note: from here onwards is the work of Pax - thank you, and I've converted it to Community Wiki so I don't get rep points undeservedly.]

以下代码将执行此操作.请尝试并报告结果.

The following code will do this. Try it and report back on the results, please.

while (1) {
    numbytes = recv(sockfd, buf, MAXDATASIZE-1, 0);
    buf[numbytes] = '\0';
    printf("Count: %d, Error: %d, Received: %s\n", numbytes, errno, buf);
    // more code to react goes here
}

问题编辑后:

错误号 111 是 ECONNREFUSED - 这不是 recv() 的常见错误代码,但更适合开放类型调用(open()、connect() 等).

Error number 111 is ECONNREFUSED - this is not a usual error code for recv(), but is more suited to the open-type call (open(), connect(), etc).

无论如何,ECONNREFUSED 是服务器端的问题,而不是客户端 - 服务器故意拒绝接受您的传入连接,因此您需要调查链接的那一端.

In any case, ECONNREFUSED is a problem at the server end, not the client - the server has purposefully refused to accept your incoming connection, so you will need to investigate that end of the link.

为了测试这一点,请更改您的代码,使其在端口 80 上连接到 www.microsoft.com,然后发送几行任何旧垃圾.您应该从他们的 Web 服务器返回一个错误,指示格式错误的 HTTP 请求.这将证明您的客户端没有问题.

In order to test this, change your code so that it's connecting to www.microsoft.com on port 80, then send a couple of lines of any old rubbish. You should get back an error from their web server indicating a malformed HTTP request. This will prove there's no problem on your client end.

这是当我 telnet www.microsoft.com 80 并输入 hello 后跟 ENTER 两次时得到的:>

This is what I get back when I telnet www.microsoft.com 80 and type in hello followed by ENTER twice:

HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 27 Nov 2008 01:45:09 GMT
Connection: close
Content-Length: 326

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Verb</h2>
<hr><p>HTTP Error 400. The request verb is invalid.</p>
</BODY></HTML>

您应该会看到类似的内容.

You should see something similar.

这篇关于通过 TCP 客户端套接字接收数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆