[英]Connection refused error when using sockets between C# (server) and Java (Client) applications
[英]Error in simple C# client using sockets
我試圖在C#中創建一個非常簡單的客戶端/服務器,基本上我現在想做的就是讓客戶端連接到服務器,這就是我出錯的地方。 我的服務器啟動正常,但是當我嘗試啟動客戶端時,出現此錯誤:
The requested address is not valid in this context
at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x00000] in <filename unknown>:0
at Client.Client.Main (System.String[] args) [0x00000] in <filename unknown>:0
我現在擁有的基本客戶如下
try
{
IPAddress ipAddress = IPAddress.Parse("0.0.0.0");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 8001);
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
sender.Connect(remoteEP);
Console.WriteLine("Connected to: " + remoteEP);
byte[] msg = Encoding.ASCII.GetBytes("Testing");
sender.Send(msg);
sender.Shutdown(SocketShutdown.Both);
sender.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message + "\n" + e.StackTrace);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message + "\n" + e.StackTrace);
}
這不是有效的IP地址。 對於本地主機(您自己的計算機),這就是您想要的
IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.