Re: Problem with connecting udp socket
- From: "Peter Duniho" <NpOeStPeAdM@xxxxxxxxxxxxxxxx>
- Date: Fri, 21 Dec 2007 09:54:21 -0800
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
.
- References:
- Problem with connecting udp socket
- From: ElvisRS@xxxxxxxxx
- Problem with connecting udp socket
- Prev by Date: Re: how to grab a handle to existing IE document?
- Next by Date: Re: RichText Box Help pls...
- Previous by thread: Problem with connecting udp socket
- Next by thread: Re: Winddow form: Hide a tab in a TabControl?
- Index(es):
Relevant Pages
|
Loading