亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java Socket Server 和 C# Client Localhost 工作

Java Socket Server 和 C# Client Localhost 工作

HUH函數 2021-10-27 10:16:51
我有一個 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());            }
查看完整描述

1 回答

?
長風秋雁

TA貢獻1757條經驗 獲得超7個贊

那么你只綁定到localhost. 您應該使用要綁定到的實際 IP 地址。


您還可以嘗試綁定到所有 IPv4 接口:


InetSocketAddress sAddr = new InetSocketAddress("0.0.0.0", port);

server.bind(sAddr);


查看完整回答
反對 回復 2021-10-27
  • 1 回答
  • 0 關注
  • 167 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號