Send UDP packet using CAsyncSocket

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.
Who is Participating?
alb66Connect With a Mentor Commented:
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.
>>>> 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
VTVAuthor Commented:
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...
itsmeandnobodyelseConnect With a Mentor Commented:
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.
Forced accept.

EE Admin
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.