I have a program that sends a message to a server and the server recognizes the message inteprets it and sends back the message to the port it was listening from in the begining. so let's say the client sends from port 5000. The server will read the message from port 5000, and send back a message to the port the client used to send the message so the client can receive it. My question now is that I don't want to fix a port for the server ( i don't want the server to always send back to client to port 1000), so how can i know what port my client used to send the message every single time? So in my client, my UdpClient Listener = new UdpClient(xxx) [(xxxx) being the port number] can be filled by a new value every single time? the value of the port it used to send the message
thanks
here's my program :
private const int sendPort = 9999;
static void Main(string[] args)
{
Boolean done = false;
IPAddress send_to_address = IPAddress.Parse("xx.xxx.xxx.xxx");
IPEndPoint sending_end_point = new IPEndPoint(send_to_address, sendPort);
IPEndPoint receiving_end_point = new IPEndPoint(IPAddress.Any, 0);
UdpClient Listener = new UdpClient(WHAT TO PUT HERE???);
while (!done)
{
Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Console.WriteLine("Enter text to send");
string text_to_send = Console.ReadLine();
// the socket object must have an array of bytes to send.
// this loads the string entered by the user into an array of bytes.
byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
// Remind the user of where this is going.
Console.WriteLine("sending to address: {0} port: {1}", sending_end_point.Address, sending_end_point.Port);
sending_socket.SendTo(send_buffer, sending_end_point);
Console.WriteLine("Message has been sent to the broadcast address");
Console.WriteLine("Now we are waiting for a message back from the Listener");
// here we receive the Message from the Server
Byte[] ByteFromListener = Listener.Receive(ref receiving_end_point);
string datafromreceiver = Encoding.ASCII.GetString(ByteFromListener);
Console.WriteLine(Datafrom receiver)
}
}
}
}
The answer was quite simple. Just needed to use the same socket to receive the message.
Here is the code :
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
class Program
{
private const int sendPort = 9999;
static void Main(string[] args)
{
Boolean done = false;
IPAddress send_to_address = IPAddress.Parse("10.128.105.177");
IPEndPoint sending_end_point = new IPEndPoint(send_to_address, sendPort);
EndPoint receiving_end_point = new IPEndPoint(IPAddress.Any, 0);
while (!done)
{
Socket sending_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
Console.WriteLine("Enter text to send");
string text_to_send = Console.ReadLine();
// the socket object must have an array of bytes to send.
// this loads the string entered by the user into an array of bytes.
byte[] send_buffer = Encoding.ASCII.GetBytes(text_to_send);
// Remind the user of where this is going.
Console.WriteLine("sending to address: {0} port: {1}", sending_end_point.Address, sending_end_point.Port);
sending_socket.SendTo(send_buffer, sending_end_point);
Console.WriteLine("Message has been sent to the broadcast address");
Console.WriteLine("Now we are waiting for a message back from the Listener");
Byte[] ByteFromListener = new byte[8000];
sending_socket.ReceiveFrom(ByteFromListener, ref receiving_end_point);
string datafromreceiver;
datafromreceiver = Encoding.ASCII.GetString(ByteFromListener).TrimEnd('\0');
//Here we print the message from the server
Console.WriteLine(datafromreceiver.ToString());
}
}
}
}