클라이언트 서버 응용 프로그램을 만들고 있습니다. 서버는 이미 클라이언트로부터의 연결을 기다리고있는 설계 및 장소에 있습니다. 이제 클라이언트 섹션에서 응용 프로그램의 수명 내내 연결을 유지하고 싶습니다. 연결은 주 클라이언트 응용 프로그램을 닫거나 종료하거나 서버를 닫을 때만 닫힙니다.Tcp 연결 유지
는 현재 10 초마다 서버는 TCP connection.I이
socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, true);
과 노력 닫지 만 나를 위해 작동하지 않습니다 .. 다음은 내 코드 블록
public TCPStreamDevice(string RemoteIPAddress, int RemotePort, string SourceIPAddress, int SourcePortNo)
{
mIpAddress = RemoteIPAddress;
mPort = RemotePort;
mClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
System.Net.IPEndPoint LocalEndPoint = new System.Net.IPEndPoint(System.Net.IPAddress.Parse(SourceIPAddress), SourcePortNo);
mClient.Bind(LocalEndPoint);
mDataReceivedCallback = new AsyncCallback(DataReceivedTCPCallback_Handler);
mBuffer = new byte[1024];
Description = new DeviceDescription();
}
과에 다루는 사람 :
private void DataReceivedTCPCallback_Handler(IAsyncResult ar)
{
try
{
Socket client = (Socket)ar.AsyncState;
int bytesReceived = client.EndReceive(ar);
if (bytesReceived > 0)
{
//to know transport level errors
//EngineInterface.reponseReceived(mBuffer, false);
ReceiveCallBackFunc(mBuffer, bytesReceived);
client.BeginReceive(mBuffer, 0, 1024, SocketFlags.None, DataReceivedTCPCallback_Handler, client);
}
else
{
//disconnect
/* when there is no datapacket means no TCP connection is alive now (how can i keep Tcp alive here) */
}
}
}
아마도 서버가 연결을 종료했을 것입니다. 시간 초과가 초과되면 10 초 이내에 데이터를 전송하는 것을 제외하고는 아무 것도 할 수 없습니다. 수신 된 0 바이트는 다른 쪽이 연결 해제되었음을 의미합니다. –
거기에 어떤 방법을 통해 나는 TCP 세션을 다시 시작할 수 있습니까? –
필요할 때 다시 연결해야합니다. 이것은로드가 많은 서버의 경우 일반적으로 발생합니다. –