简体   繁体   English

C#UDP服务器与客户端对话

[英]C# UDP Server talk with Client

I've tried multiple methods of doing this, and non seem to work out. 我尝试了多种方法来执行此操作,但似乎无法解决。 But there must be a way. 但是一定有办法。

What I'm trying to do (in C#) is create a server. 我想做的(在C#中)是创建一个服务器。 I want the server to listen on a IP and port, and when it connects to a client, I want it to read what the client says, and send a reply. 我希望服务器在IP和端口上进行侦听,并且当它连接到客户端时,我希望它读取客户端说的内容并发送回复。 For the client, I want to connect to a server and send data, and receive the reply from the server. 对于客户端,我想连接到服务器并发送数据,并接收来自服务器的回复。

How can I go about doing this? 我该怎么做呢?

I've used Microsoft examples and examples found on MSDN. 我使用了Microsoft示例和在MSDN上找到的示例。 They have Client > data > Server but it doesn't ever seem to include the server reply. 他们有“客户端”>“数据”>“服务器”,但似乎从未包含服务器回复。

I know this can be done, obviously, because we have multiplayer games. 我知道这可以做到,因为我们有多人游戏。

Thanks for the help. 谢谢您的帮助。

EDIT - Code snippets 编辑 -代码段

SERVER 服务器

 static void Main(string[] args) { int recv; byte[] data = new byte[1024]; IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 904); Socket newSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); newSocket.Bind(endPoint); Console.WriteLine("Listening for connections..."); //LISTEN FOR CLIENT IPEndPoint sender = new IPEndPoint(IPAddress.Any, 904); EndPoint tmpRemote = (EndPoint)sender; //READ MESSAGE FROM CLIENT recv = newSocket.ReceiveFrom(data, ref tmpRemote); Console.WriteLine("Messaged received from: " + tmpRemote.ToString()); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); string welcome = "Welcome to server!"; data = Encoding.ASCII.GetBytes(welcome); //SEND WELCOME REPLY TO CLIENT Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); sock.Bind(tmpRemote); sock.SendTo(data, tmpRemote); Console.WriteLine("Reply sent to client"); while (true) { if(!newSocket.Connected) { Console.WriteLine("Client disconnected."); break; } data = new byte[1024]; recv = newSocket.ReceiveFrom(data, ref tmpRemote); if (recv == 0) break; Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); } newSocket.Close(); Console.WriteLine("Server disconnected."); Console.ReadLine(); } } 

CLIENT 客户

  static void Main(string[] args) { Console.WriteLine("Message [127.0.0.1:904]: "); string msg = Console.ReadLine(); byte[] packetData = ASCIIEncoding.ASCII.GetBytes(msg); string IP = "127.0.0.1"; int port = 904; IPEndPoint ep = new IPEndPoint(IPAddress.Parse(IP), port); Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); client.SendTo(packetData, ep); Console.WriteLine("Data sent!"); int recv; byte[] data = new byte[1024]; EndPoint tmpRemote = (EndPoint)ep; while(true) { //READ MESSAGE FROM SERVER recv = client.ReceiveFrom(data, ref tmpRemote); Console.WriteLine("Messaged received from: " + tmpRemote.ToString()); Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv)); } client.Close(); Console.WriteLine("Client disconnected."); Console.ReadLine(); } 

I can't get the server to talk back to the client and have the client read/display the server's reply. 我无法让服务器与客户端进行回覆并让客户端读取/显示服务器的回复。

change this sentence 改变这句话

sock.SendTo(data, tmpRemote);

to

newSocket.SendTo(data, tmpRemote);

and remove these sentences, you have bind local EndPoint twice. 并删除这些句子,您已经绑定了本地EndPoint两次。

Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
sock.Bind(tmpRemote);

In Net, you can use UdpClient instead of Socket . 在Net中,可以使用UdpClient代替Socket

If you want a demo, look at this demo . 如果要演示,请查看此演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM