comparison 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
comparison
equal deleted inserted replaced
44:f5b5c648cd5d 53:6907e2177eb8
5 * Author: giulio moro 5 * Author: giulio moro
6 */ 6 */
7 #include "../include/UdpClient.h" 7 #include "../include/UdpClient.h"
8 8
9 UdpClient::UdpClient(){ 9 UdpClient::UdpClient(){
10 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
11 isSetPort=false;
12 isSetServer=false;
10 enabled=false; 13 enabled=false;
11 } 14 }
12 UdpClient::UdpClient(int aPort, const char* aServerName){ 15 UdpClient::UdpClient(int aPort, const char* aServerName){
13 outSocket=socket(AF_INET, SOCK_DGRAM, 0); 16 outSocket=socket(AF_INET, SOCK_DGRAM, 0);
14 if(outSocket<0){ 17 if(outSocket<0){
15 enabled=false; 18 enabled=false;
16 return; 19 return;
17 } 20 }
18 setPort(aPort); 21 setPort(aPort);
19 setServer(aServerName); 22 setServer(aServerName);
23 isSetPort=true;
24 isSetServer=true;
20 enabled=true; 25 enabled=true;
21 } 26 }
22 UdpClient::~UdpClient(){ 27 UdpClient::~UdpClient(){
23 close(outSocket); 28 close(outSocket);
24 } 29 }
25 void UdpClient::setPort(int aPort){ 30 void UdpClient::setPort(int aPort){
26 port=aPort; 31 port=aPort;
27 destinationServer.sin_port = htons(port); 32 destinationServer.sin_port = htons(port);
28 destinationServer.sin_family = AF_INET; 33 destinationServer.sin_family = AF_INET;
34 isSetPort=true;
35 if(isSetServer){
36 enabled=true;
37 }
29 }; 38 };
30 void UdpClient::setServer(const char* aServerName){ 39 void UdpClient::setServer(const char* aServerName){
31 inet_pton(AF_INET,aServerName,&destinationServer.sin_addr); 40 inet_pton(AF_INET,aServerName,&destinationServer.sin_addr);
41 isSetServer=true;
42 if(isSetPort){
43 enabled=true;
44 }
32 }; 45 };
33 int UdpClient::send(void * message, int size){ 46 int UdpClient::send(void * message, int size){
34 if(!enabled) 47 if(!enabled)
35 return -1; 48 return -1;
36 unsigned int length; 49 unsigned int length;
37 length=sizeof(struct sockaddr_in); 50 length=sizeof(struct sockaddr_in);
38 int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length); 51 int n=sendto(outSocket,message,size,0,(const struct sockaddr *)&destinationServer,length);
39 if (n < 0) 52 if (n < 0){
40 return n; 53 return n;
54 }
41 return 1; 55 return 1;
42 }; 56 };
43 57