2 つのアプリケーションがあり、1 つは TCP ソケット経由で別のアプリケーションに接続します。私は問題を抱えていましたが、長いトラブルシューティングの後、根本的な原因はソケットの切断によるものであると考え始めました。別名、Socket.state がDisconnectedに変更されました。
上記の結論に達した理由は、純粋にコードを読んで分析しただけです。それが事実であることを証明する必要があるため、私の質問は、ソケットに接続しようとした後でもソケットが実際に切断され続けるというこの種の問題に遭遇したことがありますか?
以下は私のConnectコードです。ソケットの状態自体を常にチェックするループがあり、状態が「切断」であることを検出した場合は、このConnect()関数を再度呼び出します。Connect() を呼び出すたびに、ソケットの状態がConnectedに戻っていることに気付きました。
だから私の質問は:
1. Have you seen this behavior yourself before?
2. Do you see any problem in me calling multiple Connect() again and again?
3. Is there a way to simulate this type of socket disconnections? I tried but I can't set the Socket.Connected flag.
public override void Connect()
{
try
{
sState = Defs.STATE_CONNECTING;
// send message to UI
string sMsg = "<Msg SocketStatus=\"" + sState + "\" />";
HandleMessage(sMsg);
// Create the socket object
sSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
string sIP = "";
// Define the Server address and port
if (Validate.IsIPAddress(sServer.ToString()))
{
sIP = sServer.ToString();
}
else
{
IPHostEntry iphost = Dns.GetHostEntry(sServer.ToString());
sIP = iphost.AddressList[0].ToString();
}
IPEndPoint epServer = new IPEndPoint(IPAddress.Parse(sIP), 1234);
// Connect to Server non-Blocking method
sSock.Blocking = false;
AsyncCallback onconnect = new AsyncCallback(OnConnect);
sSock.BeginConnect(epServer, onconnect, sSock);
}
catch (Exception ex)
{
LogException(new Object[] { ex });
}
}