comparison core/UdpClient.cpp @ 70:f3251851c718

Brought UdpClient files over from ultra-staging branch (with include fix), and updated Makefile accordingly
author andrewm
date Fri, 17 Jul 2015 17:50:54 +0100
parents
children 3068421c0737 c42a6b4dc2d4
comparison
equal deleted inserted replaced
69:272154649c46 70:f3251851c718
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 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
11 isSetPort=false;
12 isSetServer=false;
13 enabled=false;
14 }
15 UdpClient::UdpClient(int aPort, const char* aServerName){
16 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
17 if(outSocket<0){
18 enabled=false;
19 return;
20 }
21 setPort(aPort);
22 setServer(aServerName);
23 isSetPort=true;
24 isSetServer=true;
25 enabled=true;
26 }
27 UdpClient::~UdpClient(){
28 close(outSocket);
29 }
30 void UdpClient::setPort(int aPort){
31 port=aPort;
32 destinationServer.sin_port = htons(port);
33 destinationServer.sin_family = AF_INET;
34 isSetPort=true;
35 if(isSetServer){
36 enabled=true;
37 }
38 };
39 void UdpClient::setServer(const char* aServerName){
40 inet_pton(AF_INET,aServerName,&destinationServer.sin_addr);
41 isSetServer=true;
42 if(isSetPort){
43 enabled=true;
44 }
45 };
46 int UdpClient::send(void * message, int size){
47 if(!enabled)
48 return -1;
49 unsigned int length;
50 length=sizeof(struct sockaddr_in);
51 int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length);
52 if (n < 0){
53 return n;
54 }
55 return 1;
56 };
57