Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to write a C# code that will wait for any broadcasted udp message. This message will be an IP Address. After receiving the message, I want to parse the IP address received and then continue to receive udp packets from that IP Address. I already have working code that can Parse a known IP address and the udp packet sending works fine. But now I need to be able to receive a broadcasted IP and then parse it.

Here is some code I am trying:

C#
Socket newsock = new Socket(AddressFamily.InterNetwork,
               SocketType.Dgram, ProtocolType.Udp);

           IPEndPoint iep = new IPEndPoint(IPAddress.Any, 11000);
           newsock.Bind(iep);
           EndPoint ep = (EndPoint)iep;

           byte[] datip = new byte[1024];
           recv = newsock.ReceiveFrom(datip, ref ep);
           string stringData = Encoding.ASCII.GetString(datip, 0, recv);
           IP_Address.Text = ep.ToString();
           Application.DoEvents();

           IPEndPoint dsender = new IPEndPoint(IPAddress.Parse(IP_Address.Text), 11000);                     //IP address of device. Waits for connection.
           EndPoint tmpRemote = (EndPoint)dsender;  //stores device into variable


Thanks,
G
Posted
Comments
[no name] 15-Jul-12 13:08pm    
And?
Trak4Net 15-Jul-12 13:36pm    
It looks like you have all of the code ready to do that. Have you tried the code you show and got errors? If you use "stringData" to parse IP address does it fail? are you trying to figure out how to only listen to messages from that IP address?
GabeA 17-Jul-12 14:05pm    
I tried it and no luck. No errors, it just wouldn't receive any packets from the parsed address. I'm not convinced that it was able to parse the address. I am trying to only listen to this one device, so once I get the broadcasted IP, I want to only receive messages from that IP.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900