Now you can download a copy of these docs so you can use them offline! Download now
SocketStreamFactory.cpp
1 /*
2  * SocketStreamFactory.cpp
3  *
4  * Created on: Nov 3, 2012
5  * Author: Mitchell Wills
6  */
7 
8 
9 #include <cstring>
10 #ifdef _WRS_KERNEL
11 #else
12  #include <stdlib.h>
13  #include <stdio.h>
14  #include <sys/types.h>
15  #include <unistd.h>
16  #ifdef WIN32
17  #include <winsock.h>
18  #include <winsock2.h>
19  #else
20  #include <sys/socket.h>
21  #include <sys/un.h>
22  #include <netdb.h>
23  #include <netinet/in.h>
24  #include <netinet/tcp.h>
25  #endif
26 #endif
27 #include "networktables2/stream/FDIOStream.h"
28 #include "networktables2/stream/SocketStreamFactory.h"
29 
30 
31 SocketStreamFactory::SocketStreamFactory(const char* _host, int _port):host(_host), port(_port){}
32 
33 SocketStreamFactory::~SocketStreamFactory(){}
34 
36 #ifdef _WRS_KERNEL
37  //crio client not supported
38  return NULL;
39 #else
40  struct sockaddr_in serv_addr;
41  struct hostent *server;
42 
43  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
44  if (sockfd < 0){
45  //error("ERROR opening socket");
46  return NULL;
47  }
48  server = gethostbyname(host);
49  if (server == NULL) {
50  //fprintf(stderr,"ERROR, no such host\n");
51  return NULL;
52  }
53  memset(&serv_addr, 0, sizeof(serv_addr));
54  serv_addr.sin_family = AF_INET;
55  memcpy(&serv_addr.sin_addr.s_addr, server->h_addr, server->h_length);
56  serv_addr.sin_port = htons(port);
57  if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0) {
58  //error("ERROR connecting");
59  return NULL;
60  }//TODO close fd if an error occured
61 
62  //int on = 1;
63  //setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof(on));
64 
65  return new FDIOStream(sockfd);
66 #endif
67 }

Generated on Sat Apr 26 2014 12:26:45 for WPILibC++ by doxygen 1.8.6