简体   繁体   English

Linux服务器套接字到Windows客户端套接字

[英]Linux Server Socket to Windows Client Socket

Hi im writing a code that will connect a windows client to a linux server socket. 您好,我正在编写将Windows客户端连接到Linux服务器套接字的代码。 I can already established a connection but it seems like the linux server are always cutting my connection after a few seconds without sending me back the response i needed. 我已经可以建立连接了,但似乎Linux服务器总是在几秒钟后就切断了我的连接,而没有发回我所需的响应。 Also i already tried using telnet but again after a few seconds the linux server cut my connection. 我也已经尝试使用telnet,但是在几秒钟后linux服务器又切断了我的连接。

Is there a problem connecting to a linux server socket using windows socket? 使用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());
            }

The server may be closing the connection due to no activity. 由于没有活动,服务器可能正在关闭连接。 Enabling the keepalive option will send an empty datagram to the server periodically to keep the connection alive. 启用keepalive选项将定期向服务器发送一个空的数据报,以保持连接有效。 See code below 见下面的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM