annotate core/UdpClient.cpp @ 53:6907e2177eb8 ultra-staging

Fixed bugs in Udp classes, updated tests
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 07 Jun 2015 14:58:34 +0100
parents f5b5c648cd5d
children 3068421c0737
rev   line source
giuliomoro@44 1 /*
giuliomoro@44 2 * udpClient.cpp
giuliomoro@44 3 *
giuliomoro@44 4 * Created on: 19 May 2015
giuliomoro@44 5 * Author: giulio moro
giuliomoro@44 6 */
giuliomoro@44 7 #include "../include/UdpClient.h"
giuliomoro@44 8
giuliomoro@44 9 UdpClient::UdpClient(){
giuliomoro@53 10 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
giuliomoro@53 11 isSetPort=false;
giuliomoro@53 12 isSetServer=false;
giuliomoro@44 13 enabled=false;
giuliomoro@44 14 }
giuliomoro@44 15 UdpClient::UdpClient(int aPort, const char* aServerName){
giuliomoro@44 16 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
giuliomoro@44 17 if(outSocket<0){
giuliomoro@44 18 enabled=false;
giuliomoro@44 19 return;
giuliomoro@44 20 }
giuliomoro@44 21 setPort(aPort);
giuliomoro@44 22 setServer(aServerName);
giuliomoro@53 23 isSetPort=true;
giuliomoro@53 24 isSetServer=true;
giuliomoro@44 25 enabled=true;
giuliomoro@44 26 }
giuliomoro@44 27 UdpClient::~UdpClient(){
giuliomoro@44 28 close(outSocket);
giuliomoro@44 29 }
giuliomoro@44 30 void UdpClient::setPort(int aPort){
giuliomoro@44 31 port=aPort;
giuliomoro@44 32 destinationServer.sin_port = htons(port);
giuliomoro@44 33 destinationServer.sin_family = AF_INET;
giuliomoro@53 34 isSetPort=true;
giuliomoro@53 35 if(isSetServer){
giuliomoro@53 36 enabled=true;
giuliomoro@53 37 }
giuliomoro@44 38 };
giuliomoro@44 39 void UdpClient::setServer(const char* aServerName){
giuliomoro@44 40 inet_pton(AF_INET,aServerName,&destinationServer.sin_addr);
giuliomoro@53 41 isSetServer=true;
giuliomoro@53 42 if(isSetPort){
giuliomoro@53 43 enabled=true;
giuliomoro@53 44 }
giuliomoro@44 45 };
giuliomoro@44 46 int UdpClient::send(void * message, int size){
giuliomoro@44 47 if(!enabled)
giuliomoro@44 48 return -1;
giuliomoro@44 49 unsigned int length;
giuliomoro@44 50 length=sizeof(struct sockaddr_in);
giuliomoro@44 51 int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length);
giuliomoro@53 52 if (n < 0){
giuliomoro@44 53 return n;
giuliomoro@53 54 }
giuliomoro@44 55 return 1;
giuliomoro@44 56 };
giuliomoro@44 57