Mercurial > hg > beaglert
annotate 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 |
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@44 | 10 enabled=false; |
giuliomoro@44 | 11 } |
giuliomoro@44 | 12 UdpClient::UdpClient(int aPort, const char* aServerName){ |
giuliomoro@44 | 13 outSocket=socket(AF_INET, SOCK_DGRAM, 0); |
giuliomoro@44 | 14 if(outSocket<0){ |
giuliomoro@44 | 15 enabled=false; |
giuliomoro@44 | 16 return; |
giuliomoro@44 | 17 } |
giuliomoro@44 | 18 setPort(aPort); |
giuliomoro@44 | 19 setServer(aServerName); |
giuliomoro@44 | 20 enabled=true; |
giuliomoro@44 | 21 } |
giuliomoro@44 | 22 UdpClient::~UdpClient(){ |
giuliomoro@44 | 23 close(outSocket); |
giuliomoro@44 | 24 } |
giuliomoro@44 | 25 void UdpClient::setPort(int aPort){ |
giuliomoro@44 | 26 port=aPort; |
giuliomoro@44 | 27 destinationServer.sin_port = htons(port); |
giuliomoro@44 | 28 destinationServer.sin_family = AF_INET; |
giuliomoro@44 | 29 }; |
giuliomoro@44 | 30 void UdpClient::setServer(const char* aServerName){ |
giuliomoro@44 | 31 inet_pton(AF_INET,aServerName,&destinationServer.sin_addr); |
giuliomoro@44 | 32 }; |
giuliomoro@44 | 33 int UdpClient::send(void * message, int size){ |
giuliomoro@44 | 34 if(!enabled) |
giuliomoro@44 | 35 return -1; |
giuliomoro@44 | 36 unsigned int length; |
giuliomoro@44 | 37 length=sizeof(struct sockaddr_in); |
giuliomoro@44 | 38 int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length); |
giuliomoro@44 | 39 if (n < 0) |
giuliomoro@44 | 40 return n; |
giuliomoro@44 | 41 return 1; |
giuliomoro@44 | 42 }; |
giuliomoro@44 | 43 |