469,906 Members | 2,191 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,906 developers. It's quick & easy.

Problem with connecting udp socket

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.Set tings.Default.InitServerAddress),
Properties.Settings.Default.InitServerPort);

initServer2.Send(Encoding.ASCII.GetBytes("REGISTER 2PORT-" +
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
Dec 21 '07 #1
1 3477
On Fri, 21 Dec 2007 05:28:54 -0800, El*****@gmail.com <El*****@gmail.com>
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
Dec 21 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nick Zdunic | last post: by
2 posts views Thread by Daniel Lindberg | last post: by
1 post views Thread by Rain | last post: by
8 posts views Thread by JDavis | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.