I'm trying to receive UDP Packets on this port: 8070
I have a program for sending UDP packets and I will receive data in another program (Receiver) but I can't find the sender IP address! i'm using socket and my code is this:
Socket UdpListener = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 8070);
byte[] buf = new byte[1000];
public Form1()
{
InitializeComponent();
UdpListener.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.PacketInformation, true);
UdpListener.Bind(ipep);
}
private void button1_Click(object sender, EventArgs e)
{
UdpListener.BeginReceive(buf, 0, buf.Length, SocketFlags.None, new AsyncCallback(OnRecv), UdpListener);
}
private void OnRecv(IAsyncResult ar)
{
UdpListener.EndReceive(ar);
UdpListener.BeginReceive(buf, 0, buf.Length, SocketFlags.None, new AsyncCallback(OnRecv), UdpListener);
}
I Will Receive Data but how can I find Sender IP Address?
i tried this:
Socket s = (Socket)ar.AsyncState;
MessageBox.Show(s.RemoteEndPoint.ToString());
but I get this Error:
A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied