• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2284
  • Last Modified:

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
         m_sConnectSocket.Create();
         // Open the connection to the server
         m_sConnectSocket.Connect(m_strName, m_iPort);
      
     }
     else
     {
         // Server, create a socket bound to the port specified
         m_sListenSocket.Create(m_iPort);
         // Listen for connection requests
         m_sListenSocket.Listen();

             
     }
- function OnAccept () to call Accept
m_sListenSocket.Accept(m_sConnectSocket);
and one OnReceive() to call Receive
iRcvd = m_sConnectSocket.Receive(pBuf, iBufSize);
I followed step by step in Learn Visual C++ 24 days
http://serghei.net/docs/programming/c++/ty%20visualC++6%2021%20days/ewtoc.html
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.
Thanks,
Vy
0
VTV
Asked:
VTV
2 Solutions
 
alb66Commented:
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:
http://www.codeproject.com/useritems/UDP_with_CAsyncSocket.asp
Also if it use a derived class, it is very easy to understand what call it makes in the base class.
0
 
itsmeandnobodyelseCommented:
>>>> 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
0
 
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...
}
0
 
itsmeandnobodyelseCommented:
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.
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now