Results 1 to 2 of 2

Thread: UDP broadcast under C/C++

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2006
    Posts
    3

    UDP broadcast under C/C++

    Hello again people. Hopefully somebody will be able to help me out with this problem too!!

    I need to implement a way of utuilising UDP broadcast in my clinet/server model. More specifically, the client needs to broadcast when it first runs, searching for a server. The server then needs to send a message back with its IP and port number, at which point the TCP connection can be established.

    I have the TCP part in my work already - it is fixed within the client and the server, so I always know where to connect to.

    What I need now is help on using the UDP broadcast. To be perfectly honest, I have no idea how to use UDP. My google searches always turn up inaccurate results - using it in linux, Java etc etc.

    If anybody can provide me with a C/C++ link for some decent tutorials, or indeed help me solve the problem, I'd be very grateful.

    Thanks all.

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: UDP broadcast under C/C++

    As far as I know, sending and receiving a UDP packet uses the same functions as a TCP session, with a few exceptions:

    1. You don't need to use listen() or accept() with a UDP socket, since there is no "connection" to speak of. You do use bind() to bind the socket to a port.
    2. You use recvfrom() instead of recv() to receive a packet from the specified address. The address you pass in can have ADDR_ANY to receive from any sender.
    3. you need to change your call to socket() when creating the socket to pass SOCK_DGRAM instead of SOCK_STREAM, and IPPROTO_UDP instead of IPPROTO_TCP.


    I found these snippets, which might help you:
    http://www.cs.rpi.edu/courses/syspro...s/server_udp.c
    http://www.cs.rpi.edu/courses/syspro...s/client_udp.c

    Good luck!

    Edit: the sites I posted above are written to be compiled on a *nix platform, but there should be minor differences (if any) to compile them on windows -- simply include the winsock2 header instead of the net/* headers, etc.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width