Update lua versions
[ryzomcore.git] / nel / samples / net / udp_ping / client.cpp
blob12760ccf403509988b97cfc77b0497a8ab25ccf0
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // Includes
21 #include "nel/misc/types_nl.h"
22 #include "nel/misc/debug.h"
23 #include "nel/misc/mem_stream.h"
25 #include "nel/net/inet_address.h"
26 #include "nel/net/udp_sim_sock.h"
30 // Namespaces
33 using namespace std;
34 using namespace NLMISC;
35 using namespace NLNET;
39 // Main
42 int main (int argc, char **argv)
44 if (argc != 3)
46 nlinfo ("%s send udp datagram to a specific port to test a connection between client and server", argv[0]);
47 nlinfo ("usage: <ip_address> <port>");
48 exit (EXIT_FAILURE);
51 CUdpSimSock *UdpSock = NULL;
52 UdpSock = new CUdpSimSock( false );
53 try
55 UdpSock->connect (CInetAddress (argv[1], atoi(argv[2])));
57 catch (Exception &e)
59 nlwarning ("Cannot connect to remote UDP host: %s", e.what());
60 exit (EXIT_FAILURE);
63 uint8 *packet = new uint8[1000];
64 uint32 psize;
66 while(true)
68 CMemStream msgout;
69 uint32 foo = 10;
70 msgout.serial (foo);
71 uint32 size = msgout.length();
72 UdpSock->send (msgout.buffer(), size);
73 nldebug ("Sent UDP datagram size %d to %s", size, UdpSock->localAddr().asString().c_str());
75 while (UdpSock->dataAvailable())
77 psize = 1000;
78 try
80 UdpSock->receive (packet, psize);
81 nldebug ("Received UDP datagram size %d bytes from %s", psize, UdpSock->localAddr().asString().c_str());
83 catch ( Exception& e )
85 nlwarning ("Received failed: %s", e.what());
88 nlSleep (1000);
91 return EXIT_SUCCESS;