[英]Sending data to server with NetworkStream
My assignment is to make a client that sends a ToString-override method to the server. 我的任务是创建一个向服务器发送ToString-override方法的客户端。 My teacher sent me a Centralcomputer.exe which is the serverprogram, this program can recieve single strings and uses my local IP.
我的老师给我发了一个Centralcomputer.exe,它是服务器程序,这个程序可以接收单个字符串并使用我的本地IP。
I connect without a problem, the problem comes when trying to execute this line Int32 bytesRead = stream.Read(bytesToRead, 0, klient.ReceiveBufferSize);
我连接没有问题,尝试执行此行时出现问题
Int32 bytesRead = stream.Read(bytesToRead, 0, klient.ReceiveBufferSize);
my client freezes and the value of bytesRead is 0. 我的客户端冻结,bytesRead的值为0。
EDIT: Forgot to mention that the serverprogram responds with some "oriental" letters before client freezes. 编辑:忘了提到serverprogram在客户端冻结之前用一些“东方”字母回复。
TcpClient client = new TcpClient();
public async Task Connecting()
{
try
{
await client.ConnectAsync("127.0.0.1", 12345);
}
catch (ArgumentNullException e)
{
MessageBox.Show("ArgumentNullException: {0}" + e);
}
catch (SocketException e)
{
MessageBox.Show("SocketException: {0}" + e);
}
public void Sending()
{
string sendToServer = "Testing testing";
byte[] data = System.Text.Encoding.ASCII.GetBytes(sendToServer);
NetworkStream stream = client.GetStream();
stream.Write(data, 0, data.Length);
byte[] bytesToRead = new byte[client.ReceiveBufferSize];
Int32 bytesRead = stream.Read(bytesToRead, 0, client.ReceiveBufferSize);
//This is where the client freezes
MessageBox.Show("Received : " + Encoding.ASCII.GetString(bytesToRead, 0, bytesRead));
}
I've never done this type of coding before so I really would appreciate some help just to get started! 我之前从未做过这种编码,所以我真的很感激一些帮助才能开始!
There are only two scenarios when you will receive zero here: 只有两种情况,您将在此处收到零:
Assuming you didn't request a zero-length read, we can therefore assume the second option, and the socket closed. 假设您没有请求零长度读取,我们可以假设第二个选项,并且套接字关闭。 So: why did it close?
那么:它为什么关闭? Most likely, because the server gave up waiting for a message to reply to.
最有可能的,因为服务器放弃了等待回复的消息。 Which suggests that it doesn't think you sent a complete message.
这表明它并不认为您发送了完整的消息。
That in turn means one of: 这反过来意味着以下之一:
To rule out the second, make sure you call Flush()
after the Write
. 要排除第二个,请确保在
Write
后调用Flush()
。 If that still doesn't fix it: check the protocol very carefully to see what you were meant to send. 如果仍不能解决问题:请仔细检查协议,看你是什么意思发送。 Maybe you missed a trailing linefeed or similar.
也许你错过了拖尾换行或类似的。
As a side note: any time you're doing a single Read
on a socket, you've probably made a mistake. 作为旁注:每当你在套接字上进行单次
Read
时,你可能犯了一个错误。 TCP isn't message oriented - it is just a stream. TCP不是面向消息的 - 它只是一个流。 This means you always need a read-loop that handles whatever framing protocol is in use.
这意味着您始终需要一个读取循环来处理正在使用的任何成帧协议。
It is possible that your server expects a carriage return or/and a line feed before writing something back to the client. 在将内容写回客户端之前,您的服务器可能需要回车或/和换行。
Try to change the following line 尝试更改以下行
string sendToServer = "Testing testing";
into 成
string sendToServer = "Testing testing \r\n";
Since we do not have access to the server's code, there is pretty hard to give you an exact solution. 由于我们无法访问服务器的代码,因此很难为您提供准确的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.