Thread: Winsock and UDP

  1. #1
    Registered User headexplodes's Avatar
    Join Date
    Aug 2001

    Red face Winsock and UDP

    I'm using nice simple MFC Socket commands with CAsyncSocket. I have sucessfully used it to create a server and client that connect through TCP and send data to eachother. What I want to do is use UDP. I understand that this is a connectionless protocol and unreliable. Well... i cant get to work at all.

    Would someone be able to tell me the basic chain of events i have have to do to use UDP?

    Thanx, HeadExplodes
    /* MSN and E-Mail - */
    If Bill Gates got a dollar for every time windows crahsed... oh... he does.
    I always use MSVC++6.0

  2. #2
    Registered User Esss's Avatar
    Join Date
    Aug 2001
    Since UDP is connectionless, things like Accept() and Listen() are meaningless, and so you use a different technique.

    To initialise UDP, pass SOCK_DGRAM instead of SOCK_STREAM to CAsyncSocket::Create().

    You can now use CAsyncSocket::SendTo and ReceiveFrom to send and receive between the client and server. You can also use CAsyncSocket::Connect on the client end to set the remote end address, and then use Send and Receive - but note that no connection is actually made; it's just setting defaults.

    This means there is no way to decide whether the connection was dropped at some point (and therefore you lost a datagram); it's up to you whether the bandwidth increase is worth the risk. It's okay for streaming audio, for example, where dropouts are acceptable, but for reliable file transfer, you're better off sticking with TCP.
    Like a rat in a maze who says,
    "Watch me choose my own direction"
    Are you under the illusion
    The path is winding your way?
    - Rush

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help problem in winsock code
    By lolguy in forum C Programming
    Replies: 8
    Last Post: 02-12-2009, 07:33 PM
  2. unable to recieve UDP packet
    By caroundw5h in forum Networking/Device Communication
    Replies: 15
    Last Post: 09-19-2007, 11:11 AM
  3. Winsock Problem
    By Noxir in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2004, 10:50 AM
  4. winsock
    By pode in forum Networking/Device Communication
    Replies: 2
    Last Post: 09-26-2003, 12:45 AM
  5. winsock experts
    By chorus in forum Windows Programming
    Replies: 1
    Last Post: 09-13-2002, 01:18 AM