Thread: udp socket library

  1. #1
    Registered User
    Join Date
    Jun 2012
    Location
    Norway
    Posts
    9

    udp socket library

    Hey,

    I'm attemping to write a little UDP socket library in c++ on linux so a user can just create a new instance of a UDPSocket class, specify desination ip and port, and just connect. Then the user should be able to call send() or receive() in any order they want.. and here I encounter a little problem..

    Most of the tutorials for udp socket sending out there include a bind() call when you create your "server" that is supposed to receive data, but the code that send data does not need one. Because I also want my library to support unicast/broadcast/multicast, I have read that I need to set the socket option SO_REUSEADDR on my sockets (since multiple sockets will need to be connected to same destination IP/port for broadcast/multicast)

    My question is.. do I need to create 2 socket handles per "UDPSocke in order to make this work? One for sending and one for receiving data? In my code when I try to work with only 1 socket, it is only able to receive stuff from itself on unicast.. Or should I just remove the SO_REUSEADDR when in unicast mode, then try to bind with both sockets, accept that the bind will only work on the 1st socket, and take it from there?

    Any socket experts out there who has some pointers?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    if you call connect() or sendto() on a UDP socket, then you don't have to call bind(). if you don't want your first operation required to be sendto() or connect(), then you have to call bind. pick a range of ports, say 5500 to 5600, and call bind until you find one for which bind() returns zero.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Socket operation on non-socket problem
    By MiniComa in forum C Programming
    Replies: 9
    Last Post: 08-20-2012, 12:59 PM
  2. little help in simple socket library
    By SIFE in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-31-2010, 11:01 PM
  3. Simple Socket Library Not so simple
    By jbully in forum C Programming
    Replies: 4
    Last Post: 12-23-2010, 09:23 AM
  4. How to initialize a non blocking socket using only winsock library
    By *DEAD* in forum Networking/Device Communication
    Replies: 4
    Last Post: 01-18-2008, 07:03 AM
  5. C++ Socket Library
    By iron in forum C++ Programming
    Replies: 1
    Last Post: 06-28-2002, 09:01 AM