Linux Server Socket to Windows Client Socket



嗨,我正在编写一个将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());
            }

服务器可能由于没有活动而关闭连接。 启用保持连接选项将定期向服务器发送空数据报,以保持连接处于活动状态。 请参阅下面的代码

            TcpClient client = new TcpClient();
            client.Client.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.KeepAlive, true);​

最新更新