客户端和服务端代码都能正常通信
当对方关闭窗体时,对方下线都有提示 如下代码:
#region 接收客户端消息
bool isReceive = true;
void ReceiveMsg(object obj)
{
Socket sokMsg = obj as Socket;
byte[] arrMsg = new byte[1024];
try
{
while (isReceive)
{
......
}
catch (Exception ex)
{
this._dgCloseConn(sokMsg.RemoteEndPoint.ToString());
this._dgChangeLinkState("offLine");
this._dgShow("对方断开连接~!");
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#region 接收服务端消息
bool isReceive = true;
public void ReceiveMsg()
{
byte[] arrMsg = new byte[1024];//创建消息缓存区
try
{
while (isReceive)
{
int realLength = sockClient.Receive(arrMsg);
string strMsg = System.Text.Encoding.UTF8.GetString(arrMsg, 0, realLength);
...
}
}
catch (Exception ex)
{
isReceive = false;
sockClient.Close();
sockClient = null;
ChangLinkState("offLine");
ShowMsg("对方断开连接!");
}
}
#endregion
但都加了一个登陆窗体,
if (FormClient.loginUser != null)
{
FormClient fc = new FormClient();
fc.Show();
this.Dispose(false);
}
上面提示断开连接的 异常捕获就不执行了,郁闷中,请指定!!!!!!!!!!
我写的TCP通信案例,也有上线下线提示,可以参考一下https://blog.csdn.net/qq_62731133/article/details/124806834