comparison core/UdpClient.cpp @ 44:f5b5c648cd5d ultra-staging

- added (unused) simple c++ classes for udp datagrams\n- added tests for the new classes
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 20 May 2015 18:07:16 +0100
parents
children 6907e2177eb8
comparison
equal deleted inserted replaced
42:24af9a14b203 44:f5b5c648cd5d
1 /*
2 * udpClient.cpp
3 *
4 * Created on: 19 May 2015
5 * Author: giulio moro
6 */
7 #include "../include/UdpClient.h"
8
9 UdpClient::UdpClient(){
10 enabled=false;
11 }
12 UdpClient::UdpClient(int aPort, const char* aServerName){
13 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
14 if(outSocket<0){
15 enabled=false;
16 return;
17 }
18 setPort(aPort);
19 setServer(aServerName);
20 enabled=true;
21 }
22 UdpClient::~UdpClient(){
23 close(outSocket);
24 }
25 void UdpClient::setPort(int aPort){
26 port=aPort;
27 destinationServer.sin_port = htons(port);
28 destinationServer.sin_family = AF_INET;
29 };
30 void UdpClient::setServer(const char* aServerName){
31 inet_pton(AF_INET,aServerName,&destinationServer.sin_addr);
32 };
33 int UdpClient::send(void * message, int size){
34 if(!enabled)
35 return -1;
36 unsigned int length;
37 length=sizeof(struct sockaddr_in);
38 int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length);
39 if (n < 0)
40 return n;
41 return 1;
42 };
43