【发布时间】:2017-11-08 04:41:03
【问题描述】:
我有一个问题,我没有从连接的 TcpClient 接收任何字节!
服务器:(我尝试将收到的消息添加到列表框中,但没有任何结果。
//tl is a TcpListener
//I set this up in a 500 milisecond loop. No worries about that.
if (tl.Pending())
{
TcpClient tcp = tl.AcceptTcpClient();
var s = tcp.GetStream();
int bytesRead = s.Read(new byte[tcp.ReceiveBufferSize], 0,
tcp.ReceiveBufferSize);
string dataReceived = Encoding.ASCII.GetString(new
byte[tcp.ReceiveBufferSize], 0, bytesRead);
listBox1.Items.Add(dataReceived);
tcp.Close();
oac++; //Overall connection count
label3.Text = "Overall Connections: " + oac; //logging connections
}
客户:
void Send(){
TcpClient c = new TcpClient(Im not including my ip., 4444);
System.IO.StreamWriter w = new System.IO.StreamWriter(c.GetStream());
byte[] bytesToSend = ASCIIEncoding.ASCII.GetBytes($"Username: \"
{textBox1.Text}\" | Password: \"{textBox2.Text}\"");
NetworkStream nwStream = c.GetStream();
nwStream.Write(bytesToSend, 0, bytesToSend.Length);
nwStream.Flush();
}
I 连接正常,但接收数据时出现问题。它只是空白
【问题讨论】:
-
您使用填充为 0 的字节数组(新字节 [...])创建 dataReceived 字符串,因此它始终为空。然后您将数据从套接字读取到另一个立即丢弃的字节数组
标签: c# byte tcplistener