34 #include <sys/select.h> 35 #include <sys/types.h> 36 #include <sys/socket.h> 39 #include <sys/ioctl.h> 47 if(transport ==
TCP) {
50 else if (transport ==
UDP) {
55 std::cerr <<
"Failed to create server socket... I quit." << std::endl;
63 bzero((
char*) &server_address,
sizeof(server_address));
64 server_address.sin_family = AF_INET;
65 server_address.sin_addr.s_addr = INADDR_ANY;
66 server_address.sin_port = htons(portnum);
69 if (bind(
server_socket, (
struct sockaddr *) &server_address,
sizeof(server_address)) < 0) {
70 std::cerr <<
"Couldn't bind socket at port number " << portnum <<
" I quit." << std::endl;
77 std::cerr <<
"Couldn't listen on port number " << portnum <<
" got " << errno <<
" I quit." << std::endl;
87 if(transport ==
TCP) {
90 else if(transport ==
UDP) {
95 std::cerr <<
"Failed to create client socket... I quit." << std::endl;
99 server = gethostbyname(hostname);
101 std::cerr <<
"Couldn't find server named [" << hostname <<
"]... I quit." << std::endl;
105 bzero((
char*) &server_address,
sizeof(server_address));
106 server_address.sin_family = AF_INET;
107 bcopy((
char *) server->h_addr, (
char*) &server_address.sin_addr.s_addr, server->h_length);
108 server_address.sin_port = htons(portnum);
110 if(connect(
conn_socket, (
struct sockaddr *) &server_address,
sizeof(server_address)) < 0) {
111 std::cerr <<
"Couldn't connect to host [" << hostname <<
"] at port number " << portnum <<
" I quit." << std::endl;
121 if(
ready)
return true;
142 char * bptr = (
char*) ptr;
146 stat = write(fd, bptr, left);
148 if((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
170 if(stat < 0)
return stat;
182 stat = read(
conn_socket, &rsize,
sizeof(
unsigned int));
184 if((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
189 perror(
"Oops -- socket get -- ");
195 char * bptr = (
char*) ptr;
199 if((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
203 perror(
"Ooops -- read buffer continued");
215 unsigned int left = rsize - size;
218 ls = read(
conn_socket, dmy, (left > 100) ? 100 : left);
220 if((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
224 perror(
"Ooops -- read buffer continued");
248 if(usec_timeout != 0) {
252 tv.tv_usec = usec_timeout;
253 int stat = setsockopt(
conn_socket, SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv));
255 throw std::runtime_error(
"Failed to put client socket in timeout mode\n");
262 unsigned int left = size;
263 char * bptr = (
char*) ptr;
264 int timeout_count = 0;
270 if((errno == EWOULDBLOCK) || (errno == EAGAIN)) {
272 if(timeout_count > 2) {
276 else return size - (left + ls);
281 perror(
"Ooops -- read buffer continued");
int putRaw(const void *ptr, unsigned int size)
int loopWrite(int fd, const void *ptr, unsigned int nbytes)
ClientSocket(const char *hostname, int portnum, TransportType transport=TCP)
int getRaw(const void *ptr, unsigned int size, unsigned int usec_timeout=0)
get a raw string of <size> bytes...
ServerSocket(int portnum, TransportType transport=TCP)
int put(const void *ptr, unsigned int size)
int get(void *ptr, unsigned int size)
struct sockaddr_in server_address client_address