PAASS
Software suite to Acquire and Analyze Data from Pixie16
poll2_socket.cpp
Go to the documentation of this file.
1 
15 #include "poll2_socket.h"
16 
17 #include <iostream>
18 #include <string>
19 #include <sstream>
20 
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <arpa/inet.h>
24 #include <netdb.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <strings.h>
29 #include <string.h>
30 
32 // class Server
34 
40 bool Server::Init(int port_, int sec_, int usec_){
41  if(init){ return false; }
42 
43  sock = socket(AF_INET, SOCK_DGRAM, 0);
44  if(sock < 0){ return false; } // failed to open socket
45 
46  length = sizeof(serv);
47  bzero(&serv, length);
48 
49  serv.sin_family = AF_INET;
50  serv.sin_addr.s_addr = INADDR_ANY;
51  serv.sin_port = htons(port_);
52 
53  if(bind(sock, (struct sockaddr *)&serv, length) < 0){ return false; } // failed to bind to port
54 
55  fromlen = sizeof(struct sockaddr_in);
56 
57  to_sec = sec_;
58  to_usec = usec_;
59 
60  return init = true;
61 }
62 
63 int Server::RecvMessage(char *message_, size_t length_){
64  if(!init){ return -1; }
65 
66  // Zero the message
67  bzero(message_, length_);
68 
69  int nbytes = (int)recvfrom(sock, message_, length_, 0, (struct sockaddr *)&from, &fromlen);
70  return nbytes;
71 }
72 
73 int Server::SendMessage(char *message_, size_t length_){
74  if(!init){ return -1; }
75 
76  return (int)sendto(sock, message_, length_, 0, (struct sockaddr *)&from, fromlen);
77 }
78 
79 bool Server::Select(int &retval){
80  timeout.tv_sec = to_sec; // Set timeout to sec_
81  timeout.tv_usec = to_usec;
82 
83  FD_ZERO(&masterfds);
84  FD_SET(sock, &masterfds);
85  memcpy(&readfds, &masterfds, sizeof(fd_set));
86 
87  retval = select(sock+1, &readfds, NULL, NULL, &timeout);
88  if(FD_ISSET(sock, &readfds)){ return true; }
89 
90  return false;
91 }
92 
94  if(!init){ return; }
95 
96  close(sock);
97 }
98 
100 // class Client
102 
103 bool Client::Init(const char *address_, int port_){
104  if(init){ return false; }
105 
106  sock = socket(AF_INET, SOCK_DGRAM, 0);
107  if(sock < 0){ return false; } // failed to open socket
108 
109  serv.sin_family = AF_INET;
110  hp = gethostbyname(address_);
111  if(!hp){ return false; } // failed to resolve hostname
112 
113  bcopy((char *)hp->h_addr, (char *)&serv.sin_addr, hp->h_length);
114  serv.sin_port = htons(port_);
115  length = sizeof(struct sockaddr_in);
116 
117  return init = true;
118 }
119 
120 int Client::RecvMessage(char *message_, size_t length_){
121  if(!init){ return -1; }
122 
123  // Zero the message
124  bzero(message_, length_);
125 
126  return (int)recvfrom(sock, message_, length_, 0, (struct sockaddr *)&from, &length);
127 }
128 
129 int Client::SendMessage(char *message_, size_t length_){
130  if(!init){ return -1; }
131 
132  return (int)sendto(sock, message_, length_, 0, (const struct sockaddr *)&serv, length);
133 }
134 
136  if(!init){ return; }
137 
138  close(sock);
139 }
void Close()
Close the socket.
int to_usec
Definition: poll2_socket.h:31
struct sockaddr_in serv
Definition: poll2_socket.h:27
fd_set masterfds
Definition: poll2_socket.h:32
void Close()
Close the socket.
Provides network connectivity for poll2.
int RecvMessage(char *message_, size_t length_)
struct sockaddr_in from
Definition: poll2_socket.h:28
int SendMessage(char *message_, size_t length_)
bool Select(int &retval)
bool init
Definition: poll2_socket.h:29
socklen_t fromlen
Definition: poll2_socket.h:26
struct timeval timeout
Definition: poll2_socket.h:33
bool Init(const char *address_, int port_)
int RecvMessage(char *message_, size_t length_)
int sock
Definition: poll2_socket.h:25
int SendMessage(char *message_, size_t length_)
bool Init(int port_, int sec_=10, int usec_=0)
Initialize the serv object and open a specified port. Returns false if the socket fails to open or th...
int length
Definition: poll2_socket.h:25
fd_set readfds
Definition: poll2_socket.h:32
int to_sec
Definition: poll2_socket.h:31