我正在尝试构建一个网络应用程序。我已经成功地创建了一个发送和接收数据包的服务器。到目前为止,测试是在指向127.0.0.1(没有比家更好的地方)的一台计算机上进行的。现在我想转移到网络上。如何找到正在监听我特定端口的局域网中的计算机?
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
class RecvBroadcst
{
public static void Main()
{
Socket sock = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, 9050);
sock.Bind(iep);
EndPoint ep = (EndPoint)iep;
Console.WriteLine("Ready to receive…");
byte[] data = new byte[1024];
int recv = sock.ReceiveFrom(data, ref ep);
string stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}",
stringData, ep.ToString());
data = new byte[1024];
recv = sock.ReceiveFrom(data, ref ep);
stringData = Encoding.ASCII.GetString(data, 0, recv);
Console.WriteLine("received: {0} from: {1}",
stringData, ep.ToString());
sock.Close();
}
}
192.168.1.255:3000
),使用与他正在监听的端口相同的端口进行发送。他发送某种有效负载以使服务器只向你的客户端发送回复,而不是发生了与你使用相同端口的其他人。(假设它发送字符串Send me your info for XYZ app!
)Send me your info for XYZ app!
并将UDP消息Name:Server1 IP:192.168.1.2 Port:1234
发送回发送者的源端口和IP组合(192.168.1.4:50123
)Send me your info for XYZ app!
并将UDP消息Name:Server2 IP:192.168.1.3 Port:4567
发送回发送者的源端口和IP组合(192.168.1.4:50123
)