猿问

Java Socket Server 和 C# Client Localhost 工作

我有一个 C# 客户端,他将数据发送到 Java 异步服务器并将数据写入 GUI,在localhost 中一切正常,但是,当我更改为其他 ip 时,客户端说我:


System.Net.Sockets.SocketException (0x80004005): 无法建立连接,因为目标设备明确拒绝此类连接 192.168.1.172:11000 zh System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) en System.Net .Sockets.Socket.Connect(EndPoint remoteEP)


客户端 C#:


public void StartClient(string precio)

    {

        // Data buffer for incoming data.  

        byte[] bytes = new byte[1024];


        // Connect to a remote device.  

        try

        {


            IPAddress ipAddress = IPAddress.Parse("192.168.1.172");

            IPEndPoint remoteEP = new IPEndPoint(ipAddress, 11000);


            // Create a TCP/IP  socket.  

            Socket sender = new Socket(ipAddress.AddressFamily,

                SocketType.Stream, ProtocolType.Tcp);


            // Connect the socket to the remote endpoint. Catch any errors.  

            try

            {

                sender.Connect(remoteEP);


                Console.WriteLine("Socket connected to {0}",

                    sender.RemoteEndPoint.ToString());


                // Encode the data string into a byte array.  

                byte[] msg = Encoding.ASCII.GetBytes(precio + "<EOF>");

                Console.WriteLine(msg);

                // Send the data through the socket.  

                int bytesSent = sender.Send(msg);


                // Release the socket.  

                sender.Shutdown(SocketShutdown.Both);

                sender.Close();


            }

            catch (ArgumentNullException ane)

            {

                Console.WriteLine("ArgumentNullException : {0}", ane.ToString());

            }

            catch (SocketException se)

            {

                Console.WriteLine("SocketException : {0}", se.ToString());

            }

HUH函数
浏览 149回答 1
1回答

长风秋雁

那么你只绑定到localhost. 您应该使用要绑定到的实际 IP 地址。您还可以尝试绑定到所有 IPv4 接口:InetSocketAddress sAddr = new InetSocketAddress("0.0.0.0", port);server.bind(sAddr);
随时随地看视频慕课网APP

相关分类

Java
我要回答