当我使用这段代码时,它会向我的接收器发送一个空字节数组。为什么,我不向流中写入任何内容,我只获得它。我能做些什么来防止它?
public void Send()
{
TcpClient client = new TcpClient();
IPEndPoint serverEndPoint = new IPEndPoint( IPAddress.Parse( ip ), port );
client.Connect( serverEndPoint );
NetworkStream clientStream = client.GetStream();
clientStream.Close();
client.Close();
}
private TcpListener tcpListener;
private Thread listenThread;
public void Receiver()
{
this.tcpListener = new TcpListener( IPAddress.Any, port );
this.listenThread = new Thread( new ThreadStart( ListenForClients ) );
this.listenThread.Start();
}
private void ListenForClients()
{
this.tcpListener.Start();
while ( true )
{
//blocks until a client has connected to the server
TcpClient client = this.tcpListener.AcceptTcpClient();
Thread clientThread = new Thread( new ParameterizedThreadStart( HandleClientComm ));
clientThread.Start( client );
}
}
private void HandleClientComm( object client )
{
TcpClient tcpClient = (TcpClient)client;
NetworkStream clientStream = tcpClient.GetStream();
tcpClient.Close();
}