Re: Problem with connecting udp socket



On Fri, 21 Dec 2007 05:28:54 -0800, ElvisRS@xxxxxxxxx <ElvisRS@xxxxxxxxx> wrote:

[...]
initServer2.Connect(IPAddress.Any, 0);
initServer2.Receive(buffer);

First I connect my socket to a specified ip and port, then I send and
receive data. next thing I want to do is opening the same socket that
it can accept every incoming package.

Everything works fine under Windows Vista, but when I run it under
Windows Xp I get an SocketException from line:
initServer2.Connect(IPAddress.Any, 0); that the requeste address is
not valid in its context: 0.0.0.0:0.

Someone knows what is wrong with that code?

Well, I can't explain why Vista works. I don't have enough experience with that OS to comment. I suspect that Vista is simply delaying the error; if you tried to send using the Send() method after that statement, I suspect you'd still get an error.

But as for the general issue, the basic problem is that "connecting" a UDP socket doesn't really do what you seem to think it does anyway.

A UDP socket is a connectionless socket. You can call Connect(), but all that does is define a default remote endpoint for the communications. That is, the destination to which data sent from the socket will go, if you don't provide an address when you send (i.e. using SendTo()). It's not useful for receiving at all. Any sender with your UDP socket's address and port will be able to send you data.

In addition, connecting to IPAddress.Any doesn't really make any sense. You can't send data to IPAddress.Any. If you are trying to send or receive broadcast data, you need to use the broadcast address 255.255.255.255 and set the Broadcast socket option. If you are trying to send to a specific endpoint, you need to use a valid and correct endpoint address.

You should probably visit the Winsock FAQ at http://tangentsoft.net/wskfaq/ It has a lot of good advice for people new to writing network code, especially those using sockets on Windows.

Pete
.



Relevant Pages

  • Re: Problem with socket
    ... Be aware that those port numbers are part of the IANA-assigned range. ... socket operations on sockets for which there are no handles... ... The result of using comma lists is ... you have used the completely meaningless word "crash" to describe your ...
    (microsoft.public.vc.mfc)
  • Re: System.Net.Sockets.SocketException -UDP
    ... > an deinen Port gesendet werden. ... > Klar muss das der selbe Socket sein. ... > Empfangen in der selben Klasse gruselig? ... UDPs senden als Broadcast an Port 19000 --> klappt ...
    (microsoft.public.de.german.entwickler.dotnet.csharp)
  • Re: Linux, IO::Socket::INET and recving broadcasted UDP
    ... destination udp port = 68) but my socket never receives it. ... Can you recieve other broadcast datagrams? ... Have you tried using a PF_PACKET socket? ... receive broadcasts a UDP socket has to be bound to the wildcard ...
    (comp.lang.perl.misc)
  • RE: call is blocked in recvfrom() and no further proceedings in Win CE
    ... In windows CE, I'm able to send a request but I'm unable to receive it. ... Create another socket & bind with server IP address. ... > My program has to send request to service through port 5070(in this port only ...
    (microsoft.public.windowsce.embedded)
  • Re: ISA Event
    ... applying ISA SP1 resolved the issue. ... So it's worth asking - is this SBS ... > Web Proxy service failed to bind its socket to 192.168.4.9 port 443. ...
    (microsoft.public.backoffice.smallbiz2000)

Loading