1
I’m developing a c# application that uses UDP, the problem is that I can’t answer the customer. I get the following error: Additional information: A request for sending or receiving data was not allowed because the socket is not connected and (while sending on a datagram socket using a sendto call) an address was not provided.
It follows 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
Client
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
Opa Ono, if it is not asking too much, you could edit this commented reply that each function does and what are the parameters. I’m a beginner in C# and Client+Server programming (other than web JS+PHP)
– KaduAmaral
@Kaduamaral - no problem, here is the version with comments and some corrections.
– OnoSendai
Ono, I have a problem with the method
BeginReceive
, VS points out that it does not have 2 parameters Overload, the first ones are usuallybytes[] buffer
orIList<ArraySegment<byte>>
. I put this method in the constructor of a class I called a server, so this functiongerenciaRetorno
can be a method, or must be inside the constructor as well?– KaduAmaral