Other Software > Developer's Corner
Announcing with UDP in C# - Punching above my weight
mediaguycouk:
Hi all,
I've got a multicast IPTV system where I work and a piece of software, which no longer works in Windows 7, discovers what channels are available using SSDP. I was hoping to write a short console application that would annouce itself and then wait for replies.
My programming skills lend themselves to manipulating online examples to do what I want, but I'm having trouble finding any example code for this type of thing, this being the only example I can find - http://www.codeproject.com/KB/IP/upnpnattraversal.aspx?display=Print . I was wondering if anyone knew of anything else out there?
Renegade:
I'm not sure I know what you're asking for.
Are you broadcasting over the LAN or WAN?
I don't know what your requirements are, but my gut is telling me that you'll need to do a significant amount of modifying.
mediaguycouk:
LAN.
I'm working my way through the example code I linked above and was wondering if anyone else had seen any code like it. I suppose it really was a shot in the dark.
This is the kind of code I'm after
--- Code: C# ---public static bool Discover() { Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, 1); string req = "M-SEARCH * HTTP/1.1\r\n" + "HOST: 239.255.255.250:1900\r\n" + "ST:urn:schemas-upnp-org:device:InternetGatewayDevice:1\r\n" + "MAN:\"ssdp:discover\"\r\n" + "MX:3\r\n\r\n"; byte[] data = Encoding.ASCII.GetBytes(req); IPEndPoint ipe = new IPEndPoint(IPAddress.Broadcast, 1900); byte[] buffer = new byte[0x1000]; DateTime start = DateTime.Now; Console.WriteLine("Starting do while < timeout"); do { s.SendTo(data, ipe); s.SendTo(data, ipe); s.SendTo(data, ipe); int length = 0; do { length = s.Receive(buffer); string resp = Encoding.ASCII.GetString(buffer, 0, length).ToLower(); Console.WriteLine(resp); } while (length > 0); } while (start.Subtract(DateTime.Now) < _timeout); return false; }}
Renegade:
Sorry -- I'm a bottle of wine in, and off the top of my head, I'm drawing a blank.
Hopefully someone can chime in with something useful.
Check StackOverFlow. There's usually something there that's a good pointer. ;) I just struck out there with some EXIF issues, but most often they've got an answer. Just follow the links and explore a bit. You likely won't find anything to directly answer the question, but people often point to something that answers it in one way or another.
Stoic Joker:
If you send a packet, and then wait for a response, in the same thread ... Chances are the code will sit there forever waiting for said response.
With TCP you can call select(...) in a loop to see if the socket has become writable/readable and then do X. But if you jump straight to receive, it won't return until its buffer is full ... So if nothing is coming in ... Well...Eternity passes... :)
UDP is connectionless, so I'm not entirely sure what to use for it. In C/C++ there is recvfrom(...) what if any C# equivalent there is I don't know.
I do know you need to be sure there is something out there listening and waiting to respond, or you'll drive yourself nutz trying to debugg the protocol. I usually keep a packet sniffer running filtered down to just the test packets I'm working with. So I can try to keep track of who dropped what, why, and where.
Navigation
[0] Message Index
[#] Next page
Go to full version