Mercurial > hg > beaglert
view core/UdpClient.cpp @ 215:b10f5ba8fd33
Updated to Eclipse Mars.1 Release (4.5.1) on MacOS
author | Giulio Moro <giuliomoro@yahoo.it> |
---|---|
date | Sat, 13 Feb 2016 01:45:21 +0000 |
parents | f3251851c718 |
children | 3068421c0737 c42a6b4dc2d4 |
line wrap: on
line source
/* * udpClient.cpp * * Created on: 19 May 2015 * Author: giulio moro */ #include "../include/UdpClient.h" UdpClient::UdpClient(){ outSocket=socket(AF_INET, SOCK_DGRAM, 0); isSetPort=false; isSetServer=false; enabled=false; } UdpClient::UdpClient(int aPort, const char* aServerName){ outSocket=socket(AF_INET, SOCK_DGRAM, 0); if(outSocket<0){ enabled=false; return; } setPort(aPort); setServer(aServerName); isSetPort=true; isSetServer=true; enabled=true; } UdpClient::~UdpClient(){ close(outSocket); } void UdpClient::setPort(int aPort){ port=aPort; destinationServer.sin_port = htons(port); destinationServer.sin_family = AF_INET; isSetPort=true; if(isSetServer){ enabled=true; } }; void UdpClient::setServer(const char* aServerName){ inet_pton(AF_INET,aServerName,&destinationServer.sin_addr); isSetServer=true; if(isSetPort){ enabled=true; } }; int UdpClient::send(void * message, int size){ if(!enabled) return -1; unsigned int length; length=sizeof(struct sockaddr_in); int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length); if (n < 0){ return n; } return 1; };