Send UDP packet using CAsyncSocket

Posted on 2007-10-01
Last Modified: 2013-11-20
Hi all,
I have a program to send socket using CAsyncSocket. By default it is TCP, I list here some steps:
- Create connection for client and server
if (m_iType == 0)
         // Client, create a default socket
         // Open the connection to the server
         m_sConnectSocket.Connect(m_strName, m_iPort);
         // Server, create a socket bound to the port specified
         // Listen for connection requests

- function OnAccept () to call Accept
and one OnReceive() to call Receive
iRcvd = m_sConnectSocket.Receive(pBuf, iBufSize);
I followed step by step in Learn Visual C++ 24 days
It runs ok now for TCP type. However all I need now is I also want  the program has one more function is sending UDP packet (I added one combo box whcih contain alll packet type such as UDP, SIP...)
How can I do now? Please show me how to modify or add code for this existing program.
I searched and found alot of UDP samples but this means I must add many new classes which run indepedently with old code and this caused the program bulky.
Question by:VTV
    LVL 19

    Accepted Solution

    You must create the CAsyncSocket object with SOCK_DGRAM instead of the default SOCK_STREAM flag.
    You have not to call neither Connect() nor Listen().
    Simply use SendTo() and ReceiveFrom() in order to send and receive datagrams.

    This is a very simple example:
    Also if it use a derived class, it is very easy to understand what call it makes in the base class.
    LVL 39

    Expert Comment

    >>>> I also want  the program has one more function is sending UDP packet
    UDP is a 'connection-less' protocol. You simply send a message by IP address and port. It doesn't work for sockets that already were connected(client) or listening(server) or accepted(server). If you want to make UDP messaging you should forget 'server' and 'client' but make them all 'peers'. Use different ports if already TCP sockets were used or UDP will fail with WSAEISCONN (socket is connected).

    Note, UDP message delivery is not guaranteed nor the order of messages. That normally doesn't matter in a LAN without routers and gateways. In a bigger network or if going to a remote network, you would need to organizing some kind of handshake, numbering the messages, and giving the chance of requesting lost messages by number.

    Regards, Alex

    Author Comment

    Thanks for your solutions. It seems that I can't re-use the existing program to send UDP packet?
    The only solution now is add new class for UDP?
    When I sent this question I hoped that there is one solution else other than adding new class. Because currently I have CMySocket class which is derived from CAsyncSocket already. I don't want to add new class for UDP which also is derived from CAsyncSocket.
    I hope you can help to show me how to add some functions or modify some codes in order to run UDP like this:
    if (TCP packet)
    call functions...
    if (UDP packet)
    cal functions...
    LVL 39

    Assisted Solution

    You could send UDP packages if you take a new CAsyncSocket that was created with the SOCK_DGRAM option. You may not connect, or listen but simply read or write to the socket as alb66 described above. I see no reason why it should not work with your current derived class of CAsyncSocket beside the class would rely too much on TCP. But UDP is not simply a different package, it is a different design and you cannot expect that you can use a TCP socket both for TCP and UDP without any difference.
    LVL 1

    Expert Comment

    Forced accept.

    EE Admin

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
    Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now