您好我正在编写一个代码,将Windows客户端连接到Linux服务器套接字。 我已经可以建立一个连接,但似乎linux服务器总是在几秒钟之后切断我的连接,而不是发回我需要的响应。 此外,我已经尝试使用telnet,但几秒后,linux服务器再次切断了我的连接。
使用Windows套接字连接到Linux服务器套接字是否有问题?
IPEndPoint remoteEP = new IPEndPoint(IPAddress.Parse("IPADDRESS"), 6004);
// Create a TCP/IP socket.
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// Connect the socket to the remote endpoint. Catch any errors.
try
{
if (!sender.Connected)
sender.Connect(remoteEP);
string data = new Client().Test();
DE_ISO8583 de = new ISO8583().Parse(data);
data = data.Length.ToString().PadLeft(4, '0') + data;
byte[] msg = Encoding.ASCII.GetBytes(data);
int bytesSent = sender.Send(msg);
//// Receive the response from the remote device.
int bytesRec = sender.Receive(bytes);
Console.WriteLine("Received = {0}",
Encoding.ASCII.GetString(bytes, 0, bytesRec));
//sender.Disconnect(true);
//sender.Shutdown(SocketShutdown.Both);
//sender.Close();
}
catch (ArgumentNullException ane)
{
Console.WriteLine("ArgumentNullException : {0}", ane.ToString());
}
catch (SocketException se)
{
Console.WriteLine("SocketException : {0}", se.ToString());
}
catch (Exception e)
{
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
答案 0 :(得分:0)
由于没有活动,服务器可能正在关闭连接。启用keepalive选项将定期向服务器发送空数据报以保持连接活动。见下面的代码
TcpClient client = new TcpClient();
client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, true);