【发布时间】:2020-03-16 01:21:59
【问题描述】:
您好,我正在尝试创建连接两台电脑的连接。在本地代码中,下面的工作没有任何问题,但是当我们在不同的电脑上启动客户端代码时,我们无法连接。代码如下。哪里错了?
客户端:
void Start()
{
Debug.Log(string.Format("Starting TCP and UDP clients on port {0}...", 90));
udpThread = new Thread(new ThreadStart(ClientThread));
udpThread.Start();
}
void ClientThread()
{
try
{
// Establish the remote endpoint
// for the socket. This example
// uses port 11111 on the local
// computer.
IPHostEntry ipHost = Dns.GetHostEntry(Dns.GetHostName());
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress addr = ipEntry.AddressList[0];
IPAddress addr2 = ipEntry.AddressList[1];
IPEndPoint localEndPoint = new IPEndPoint(addr, 90);
// Creation TCP/IP Socket using
// Socket Class Costructor
Socket sender = new Socket(addr.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
try
{
sender.Connect(localEndPoint);
Console.WriteLine("Socket connected to -> {0} ",
sender.RemoteEndPoint.ToString());
byte[] messageSent = Encoding.ASCII.GetBytes("Test Client<EOF>");
int byteSent = sender.Send(messageSent);
byte[] messageReceived = new byte[1024];
int byteRecv = sender.Receive(messageReceived);
Console.WriteLine("Message from Server -> {0}",
Encoding.ASCII.GetString(messageReceived,
0, byteRecv));
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());
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
和服务器端
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine(string.Format("Starting TCP and UDP servers on port {0}...", port));
devices_battery_midlow_pic_1.Visible = false;
devices_battery_low_pic_1.Visible = false;
devices_battery_midhigh_pic_1.Visible = false;
devices_battery_high_pic_1.Visible = false;
play_button.Anchor = (AnchorStyles.Bottom);
Console.WriteLine(DateTime.Now.ToString() + " Getting IP...");
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
connection_id_label.Text = "This IP:\n" + ipAddress.MapToIPv4().ToString();
Console.WriteLine(DateTime.Now.ToString() + " Starting Connection Thread...");
connectionThread = new Thread(new ThreadStart(StartServer));
connectionThread.IsBackground = true;
connectionThread.Start();
}
private void StartServer()
{
string strHostName = "";
strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);
IPAddress addr = ipEntry.AddressList[0];
Console.WriteLine(addr.MapToIPv4().ToString() + " Starting Connection Thread...");
IPEndPoint localEndPoint = new IPEndPoint(addr, 90);
Socket listener = new Socket(addr.AddressFamily,
SocketType.Stream, ProtocolType.Tcp);
try
{
listener.Bind(localEndPoint);
listener.Listen(10);
while (true)
{
Console.WriteLine("Waiting connection ... ");
Socket clientSocket = listener.Accept();
// Data buffer
byte[] bytes = new Byte[1024];
string data = null;
Console.WriteLine("Text received -> {0} ", data);
byte[] message = Encoding.ASCII.GetBytes("Test Server");
clientSocket.Send(message);
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
这是在一台电脑上运行的,但是如果我们将服务器代码移动到另一台电脑上,我们将无法得到任何响应。哪里出错了??
【问题讨论】:
-
检查以下行以查看它是否返回 IPV4 或 IPV6。许多计算机都有 4 和 6,[0] 是 V6,[1] 是 V4。 : IPAddress ipAddress = ipHostInfo.AddressList[0];
-
是的,你是对的。但是你的意思是客户端还是服务器端?因为我在服务器端使用 IPAddresss.any。
-
它是从本地机器名 Dns.GetHostName() 中获取的本地端点地址。 IP.Any 也是一个本地端点。用于收听。连接需要源 IP 地址(本地端点)和目标 IP 地址。在客户端,源 IP 是从本地端点获取的。