这是我的服务器代码
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 9050);
UdpClient newsock = new UdpClient(ipep);
Console.WriteLine("Waiting for a client...");
IPEndPoint send = new IPEndPoint(IPAddress.Any, 0);
byte[] data1 = newsock.Receive(ref send);
int test1 = BitConverter.ToInt32(data1, 0);
Console.WriteLine("test1 = {0}", test1);
这是我的客户代码
byte[] data = new byte[1024];
string stringData;
UdpClient server = new UdpClient("127.0.0.1", 9050);
IPEndPoint send = new IPEndPoint(IPAddress.Any, 0);
int test1 = 45;
byte[] data1 = BitConverter.GetBytes(test1);
server.Send(data1, data1.Length);
根据我的客户和服务器,
客户端是向服务器发送数据的客户端。
但是我的要求是相反的!我无法做到这一点。
当我尝试将此代码添加到服务器时
byte[] buffer = ASCIIEncoding.ASCII.GetBytes("Hello Client");
newsock.Send(buffer, buffer.Length);
我收到了
The operation is not allowed on non-connected sockets.
的异常有人能帮我吗?
最佳答案
checkout JoinMulticastGroup(就像Connect for TcpClient)。您需要在广播之前(即,如果要广播)执行此操作。
UdpClient的文档也将对您有所帮助。
关于c# - C#UdpClient服务器和客户端问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4109056/