Problem with connecting udp socket

E

ElvisRS

Hi,

I'm writing a simple udp client using sockets. My code looks like
this:

initServer2 = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp);
initServer2.Connect(IPAddress.Parse(Properties.Settings.Default.InitServerAddress),
Properties.Settings.Default.InitServerPort);

initServer2.Send(Encoding.ASCII.GetBytes("REGISTER2PORT-" +
userName));
initServer2.Receive(buffer);

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?
Thanks for any help
 
P

Peter Duniho

[...]
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top