0
我正在開發客戶端需要連接的服務器應用程序。 但我不希望我的用戶輸入的IP地址......我想客戶發現的所有服務器上的端口4800上運行(對我來說)發送UDP廣播,需要接收來自所有客戶端的消息
這裏是我的服務器代碼:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static ThreadStart ts = new ThreadStart(start_server);
Thread thread = new Thread(ts);
private void Form1_Load(object sender, EventArgs e)
{
thread.Start();
}
private static void start_server()
{
//Start server
Socket server = new Socket(AddressFamily.InterNetwork,
SocketType.Dgram, ProtocolType.Udp);
Console.Write("Running server..." + Environment.NewLine);
server.Bind(new IPEndPoint(IPAddress.Any, 4800));
while (true)
{
IPEndPoint sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tempRemoteEP = (EndPoint)sender;
byte[] buffer = new byte[1000];
//Recive message from anyone.
server.ReceiveFrom(buffer, ref tempRemoteEP);
Console.Write("Server got '" + Encoding.ASCII.GetString(buffer).TrimEnd(new char[] { (char)0 }) +
"' from " + tempRemoteEP.ToString() +
Environment.NewLine);
string access_code = Encoding.ASCII.GetString(buffer).TrimEnd(new char[] { (char)0 });
if (access_code == "7uz876t5r798qwe12")
{
Console.Write("Sending Response to " + tempRemoteEP.ToString() +
Environment.NewLine);
//Replay to client
server.SendTo(Encoding.ASCII.GetBytes("ACCESS GRANTED"),
tempRemoteEP);
}
else
{
Console.WriteLine("Client Access denied!");
server.SendTo(Encoding.ASCII.GetBytes("ACCESS DENIED"),
tempRemoteEP);
}
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
Environment.Exit(Environment.ExitCode);
}
}
而這就是我的客戶端發送一個UDP廣播:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
private void button1_Click(object sender, EventArgs e)
{
IPEndPoint AllEndPoint = new IPEndPoint(IPAddress.Broadcast, 4800);
//Allow sending broadcast messages
client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.Broadcast, 1);
//Send message to everyone
client.SendTo(Encoding.ASCII.GetBytes("7uz876t5r798qwe12"), AllEndPoint);
Console.Write("Client send '1' to " + AllEndPoint.ToString() +
Environment.NewLine);
IPEndPoint _sender = new IPEndPoint(IPAddress.Any, 0);
EndPoint tempRemoteEP = (EndPoint)_sender;
byte[] buffer = new byte[1000];
string serverIp;
try
{
client.SetSocketOption(SocketOptionLevel.Socket,
SocketOptionName.ReceiveTimeout, 3000);
client.ReceiveFrom(buffer, ref tempRemoteEP);
Console.Write("Client got '" + buffer[0] + "' from " +
tempRemoteEP.ToString() + Environment.NewLine);
MessageBox.Show(Encoding.ASCII.GetString(buffer).TrimEnd(new char[] { (char)0 }));
//Get server IP (ugly)
serverIp = tempRemoteEP.ToString().Split(":".ToCharArray(), 2)[0];
listServer.Items.Add(serverIp);
}
catch (Exception ex)
{
//Timout. No server answered.
MessageBox.Show(ex.Message);
}
// MessageBox.Show(serverIp);
}
}
所有服務器上運行的是接收廣播,但我的客戶只接收1個IP地址。也許到達客戶端的第一個響應?
我如何能夠發現我的端口上正在監聽的所有服務器?
UDP並不能保證到達。通常情況下,您會發出廣播,並且事情會有所反應。您可以採取第一個選項,以免等待其他人,或等待給定的時間,讓他們選擇他們想要的。 – BugFinder