I'm developing a C # application that uses UDP, the problem is that I can not respond to the client. I get the following error: Additional information: A request to send or receive data was not allowed because the socket is not connected and (when sending to a datagram socket using a sendto call) an address was not provided. p>
Follow the system: Server:
string data = "";
byte[] d = new byte[1024];
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Any, 6969);
Console.WriteLine(" S E R V E R IS S T A R T E D ");
Console.WriteLine("* Waiting for Client...");
server.Bind(remoteIPEndPoint);
while (data != "q") {
//data = Console.ReadLine();
server.Receive(d);
data = Encoding.ASCII.GetString(d);
Console.WriteLine("Handling client at " + remoteIPEndPoint + " - ");
Console.WriteLine("Message Received " + data.TrimEnd());
d = Encoding.ASCII.GetBytes("oi");
server.Send(d);
Console.WriteLine("Message sended to" + remoteIPEndPoint + " " + data);
}
Console.WriteLine("Press Enter Program Finished");
Console.ReadLine(); //delay end of program
server.Close(); //close the connection
Customer
string data = "";
byte[] d = new byte[1024];
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint remoteIPEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 6969);
Console.WriteLine(" C L I E N T IS S T A R T E D ");
server.Connect(remoteIPEndPoint);
Console.WriteLine("* Server connected...");
while (data != "q") {
d = Encoding.ASCII.GetBytes("teste");
server.Send(d);
Console.WriteLine("Message sended to" + remoteIPEndPoint + " " + data);
server.Receive(d);
data = Encoding.ASCII.GetString(d);
Console.WriteLine("Received from server: "+data);
}
Console.WriteLine("Press Enter Program Finished");
Console.ReadLine(); //delay end of program
server.Close(); //close the connection