(Socket.SendTo) 我如何 response/send 将我的服务器开始接收的数据返回给客户端? C#
(Socket.SendTo) How do I response/send back data to the client that my server began receive? C#
我正在做一个 UDP 服务器,我可以很好地接收数据,但我不知道如何存储端点以发送回数据。
SetupServer()
最后一行的评论是我不知道如何获得EndPoint
的地方。
这是我的服务器代码:
static private byte[] buffer = new byte[1024];
static private Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private const int port = 904;
static void Main(string[] args) {
SetupServer();
Console.ReadLine();
}
static private void SetupServer() {
Console.WriteLine($"{DateTime.Now}: Setting up server on {port}...");
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, port);
serverSocket.Bind(remoteEP);
Console.WriteLine($"{DateTime.Now}: Server created successfully");
serverSocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP,BeginReceive, null);
//serverSocket.SendTo(buffer, 0, buffer.Length, SocketFlags.None, remoteEP);
}
static private void BeginReceive(IAsyncResult ar) {
int recv = serverSocket.EndReceive(ar);
byte[] dataBuf = new byte[recv];
Array.Copy(buffer, dataBuf, recv);
Console.WriteLine($"{DateTime.Now}: {Encoding.ASCII.GetString(dataBuf)}");
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, port);
serverSocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, BeginReceive, null);
}
我知道怎么做了。我将 BeginReceive
方法中的 serverSocket.EndReceive(ar)
替换为 EndReceiveFrom(ar,ref epSender)
并且使用该终点 (epSender) 我可以调用 BeginReceiveFrom
方法。
static private void SetupServer() {
Console.WriteLine($"{DateTime.Now}: Setting up server on {port}...");
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, port);
serverSocket.Bind(remoteEP);
Console.WriteLine($"{DateTime.Now}: Server created successfully");
serverSocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP,BeginReceive, serverSocket);
}
static private void BeginReceive(IAsyncResult ar) {
EndPoint epSender = new IPEndPoint(IPAddress.Any, port);
int recv = serverSocket.EndReceiveFrom(ar,epSender);
byte[] dataBuf = new byte[recv];
Array.Copy(buffer, dataBuf, recv);
Console.WriteLine($"{DateTime.Now}: {Encoding.ASCII.GetString(dataBuf)}");
serverSocket.SendTo(dataBuf,epSender)
serverSocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, BeginReceive, serverSocket);
}
我正在做一个 UDP 服务器,我可以很好地接收数据,但我不知道如何存储端点以发送回数据。
SetupServer()
最后一行的评论是我不知道如何获得EndPoint
的地方。
这是我的服务器代码:
static private byte[] buffer = new byte[1024];
static private Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
private const int port = 904;
static void Main(string[] args) {
SetupServer();
Console.ReadLine();
}
static private void SetupServer() {
Console.WriteLine($"{DateTime.Now}: Setting up server on {port}...");
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, port);
serverSocket.Bind(remoteEP);
Console.WriteLine($"{DateTime.Now}: Server created successfully");
serverSocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP,BeginReceive, null);
//serverSocket.SendTo(buffer, 0, buffer.Length, SocketFlags.None, remoteEP);
}
static private void BeginReceive(IAsyncResult ar) {
int recv = serverSocket.EndReceive(ar);
byte[] dataBuf = new byte[recv];
Array.Copy(buffer, dataBuf, recv);
Console.WriteLine($"{DateTime.Now}: {Encoding.ASCII.GetString(dataBuf)}");
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, port);
serverSocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, BeginReceive, null);
}
我知道怎么做了。我将 BeginReceive
方法中的 serverSocket.EndReceive(ar)
替换为 EndReceiveFrom(ar,ref epSender)
并且使用该终点 (epSender) 我可以调用 BeginReceiveFrom
方法。
static private void SetupServer() {
Console.WriteLine($"{DateTime.Now}: Setting up server on {port}...");
EndPoint remoteEP = new IPEndPoint(IPAddress.Any, port);
serverSocket.Bind(remoteEP);
Console.WriteLine($"{DateTime.Now}: Server created successfully");
serverSocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP,BeginReceive, serverSocket);
}
static private void BeginReceive(IAsyncResult ar) {
EndPoint epSender = new IPEndPoint(IPAddress.Any, port);
int recv = serverSocket.EndReceiveFrom(ar,epSender);
byte[] dataBuf = new byte[recv];
Array.Copy(buffer, dataBuf, recv);
Console.WriteLine($"{DateTime.Now}: {Encoding.ASCII.GetString(dataBuf)}");
serverSocket.SendTo(dataBuf,epSender)
serverSocket.BeginReceiveFrom(buffer, 0, buffer.Length, SocketFlags.None, ref remoteEP, BeginReceive, serverSocket);
}