SOL9 Sample: SocketDatagramSender
|
1 Screenshot
2 Source code
/*
* SocketDatagramSender.cpp
* Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED.
*/
#include <sol\SocketDatagram.h>
#include <sol\WSAInitializer.h>
class SocketDatagramSender{
public:
SocketDatagramSender()
{
}
public:
void send(InetAddress& inet) {
SocketDatagram socketDatagram;
char buf[SIZE_1KB];
for (int i=0; i<10; i++) {
sprintf_s(buf, SizeOf(buf), "%d Sending a message by a SocketDatagram.\n'Hello world!\n Goodbye world.'", i);
int slen = strlen(buf);
try {
//1 Send a string.
int rc = socketDatagram.sendTo(inet, buf, slen);
printf("sent: string=[%s] len=%d(bytes) sent=%d(bytes)\n",
buf, slen, rc);
Sleep(1000);
if (rc >0) {
char reply[SIZE_1KB];
//2 Receive a string.
int r = socketDatagram.recvFrom(inet.getPort(), reply, sizeof(reply));
if (r >0) {
printf("Got a reply:[%s]\n", reply);
}
}
} catch (Exception& ex) {
ex.printf();
} catch (...) {
}
}
}
};
/**
*
*/
void _tmain(int argc, TCHAR* argv[])
{
WSAInitializer wsaInitializer;
try {
printf("Create SocketDatagramSender\n");
//Host=localhost, port = 32000
InetAddress inet("localhost", 32000);
SocketDatagramSender sender;
sender.send(inet);
} catch (Exception& ex) {
ex.printf();
} catch (...) {
printf("Exception\n");
}
}
Last modified: 2 May 2016
Copyright (c) 2016 Antillia.com ALL RIGHTS RESERVED.