Problem with connecting udp socket

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ElvisRS@gmail.com

    #1

    Problem with connecting udp socket

    Hi,

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

    initServer2 = new Socket(AddressF amily.InterNetw ork, SocketType.Dgra m,
    ProtocolType.Ud p);
    initServer2.Con nect(IPAddress. Parse(Propertie s.Settings.Defa ult.InitServerA ddress),
    Properties.Sett ings.Default.In itServerPort);

    initServer2.Sen d(Encoding.ASCI I.GetBytes("REG ISTER2PORT-" +
    userName));
    initServer2.Rec eive(buffer);

    initServer2.Con nect(IPAddress. Any, 0);
    initServer2.Rec eive(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.Con nect(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
  • Peter Duniho

    #2
    Re: Problem with connecting udp socket

    On Fri, 21 Dec 2007 05:28:54 -0800, ElvisRS@gmail.c om <ElvisRS@gmail. com>
    wrote:
    [...]
    initServer2.Con nect(IPAddress. Any, 0);
    initServer2.Rec eive(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.Con nect(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

    Comment

    Working...