这是Android与PC之间的通信问题。智能手机是等待连接的主机,当pc连接时,智能手机会继续向pc发送信息。
即使智能手机重启,我也要重新连接tcp。即使在重新启动后,智能手机仍在等待再次连接。如果智能手机被关闭,连接的值仍然是“true”。并接收到'null‘值。这个时候我应该怎么做才能再次连接?
TcpClient MyTcpClient;
int myPort = 4000;
StreamWriter MyWriter;
StreamReader MyReader;
int port = GetFreePort();
MyTcpClient = new TcpClient(IPAddress.Loopback.Tostring(), port);
MyTcpClient.ReceiveTimeout = 30000;
MyReader = new StreamReader(MyTcpClient.GetStream());
MyWriter = new StreamWriter(MyTcpClient.GetStream());
while(MyTcpClient.Connected) //if device is off, it still true
{
string line = MyReader.ReadLine();// when device is off, it is null
}如果设备重新启动,我想重新连接Tcp连接。
发布于 2019-04-20 19:03:19
据我所知,您必须同时为客户端和服务器配置ReceiveTimeout。
使用套接字将为您提供更多的选择,例如
SetSocketOption( SocketOptionLevel.Socket,SocketOptionName.KeepAlive,true )。
https://stackoverflow.com/questions/55759760
复制相似问题