'How to code a UDP C# server to listen for incoming connections rather than sending one?
so I am writing a C# UDP chat program where the server will listen for incoming connections from clients and then accept the connection from the client. The problem though it appears, the server is instead sending the request to connect to the client and then waiting for it to start searching, where as I want it to listen for incoming connections on the servers IP address:
Here is the code :
bool done = false;
bool exception_thrown = false;
MessageBox.Show("Now listening on port 8001.");
Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress send_to_adress = IPAddress.Parse(ipv4.Text);
IPEndPoint sending_end_point = new IPEndPoint(send_to_adress, 8001);
Console.WriteLine("Now sending attack information to listening computers. Attack command sent: " + satt.Text);
while (!done)
{
Console.WriteLine("Enter text to send and blank line to quit. ");
string text_to_send = satt.Text;
if (text_to_send.Length == 0)
{
done = true;
}
else
{
byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
Console.WriteLine("Sending the information to the IP: {0} port: {1}",
sending_end_point.Address,
sending_end_point.Port);
const int listenPort = 8001;
UdpClient listener = new UdpClient(listenPort);
IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);
string data_recieved;
byte[] recieve_byte_array;
recieve_byte_array = listener.Receive(ref groupEP);
data_recieved = Encoding.ASCII.GetString(recieve_byte_array, 0, recieve_byte_array.Length);
Console.WriteLine("Number of searches by the listening computers: " + data_recieved);
}
}
How can this be reworked for the server to listen on its own ip rather then sending a request? Thank you.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
