18 #if defined WINDOWS || defined __WIN32__ || defined _WIN32
25 int ret = WSAStartup(MAKEWORD(2,0), &data);
27 fprintf(stderr,
"WSAStartup() has failed %d\n", ret);
30 int ret = WSACleanup();
32 fprintf(stderr,
"WSACleanup() has failed %d\n", ret);
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #include <netinet/tcp.h>
43 #include <arpa/inet.h>
49 #define MIN_BUFFER_SIZE 256
52 :
XPort(intf), m_socket(-1) {
55 XTCPSocketPort::~XTCPSocketPort() {
57 #if defined WINDOWS || defined __WIN32__ || defined _WIN32
58 closesocket(m_socket);
65 XTCPSocketPort::reopen_socket() throw (
XInterface::XCommError &) {
67 #if defined WINDOWS || defined __WIN32__ || defined _WIN32
68 closesocket(m_socket);
78 struct sockaddr_in dstaddr;
80 std::string ipaddr = portString();
81 int colpos = ipaddr.find_first_of(
':');
82 if(colpos == std::string::npos)
85 if(sscanf(ipaddr.substr(colpos + 1).c_str(),
"%u", &port) != 1)
87 ipaddr = ipaddr.substr(0, colpos);
89 m_socket = socket(AF_INET, SOCK_STREAM, 0);
94 struct timeval timeout;
97 if(setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (
char*)&timeout,
sizeof(timeout)) ||
98 setsockopt(m_socket, SOL_SOCKET, SO_SNDTIMEO, (
char*)&timeout,
sizeof(timeout))){
103 if(setsockopt(m_socket, SOL_SOCKET, SO_KEEPALIVE, (
char*)&opt,
sizeof(opt)))
108 if(setsockopt(m_socket, IPPROTO_TCP, TCP_NODELAY, (
char*)&opt,
sizeof(opt)))
114 memset( &dstaddr, 0,
sizeof(dstaddr));
115 dstaddr.sin_port = htons(port);
116 dstaddr.sin_family = AF_INET;
117 dstaddr.sin_addr.s_addr = inet_addr(ipaddr.c_str());
119 if(connect(m_socket, (
struct sockaddr *) &dstaddr,
sizeof(dstaddr)) == -1) {
120 #if defined WINDOWS || defined __WIN32__ || defined _WIN32
121 errno = WSAGetLastError();
123 throw XInterface::XCommError(formatString_tr(I18N_NOOP(
"tcp open failed %u"), errno).c_str(), __FILE__, __LINE__);
130 this->write(buf.c_str(), buf.length());
136 FD_SET(m_socket , &fs);
137 struct timeval timeout;
140 int ret = ::select(0, NULL, &fs, NULL, &timeout);
148 int ret = ::send(m_socket, sendbuf, size - wlen, 0);
150 #if defined WINDOWS || defined __WIN32__ || defined _WIN32
151 errno = WSAGetLastError();
154 if((errno == EINTR) || (errno == EAGAIN)) {
155 dbgPrint(
"TCP/IP, EINTR/EAGAIN, trying to continue.");
158 gErrPrint(i18n(
"write error, trying to reopen the socket"));
164 }
while (wlen < size);
167 XTCPSocketPort::receive() throw (
XInterface::XCommError &) {
170 FD_SET(m_socket , &fs);
171 struct timeval timeout;
174 int ret = ::select(0, &fs, NULL, NULL, &timeout);
180 buffer().resize(MIN_BUFFER_SIZE);
182 const char *ceos = eos().c_str();
183 unsigned int eos_len = eos().length();
184 unsigned int len = 0;
186 if(
buffer().size() <= len + 1)
187 buffer().resize(len + MIN_BUFFER_SIZE);
188 int rlen = ::recv(m_socket, &
buffer().at(len), 1, 0);
194 #if defined WINDOWS || defined __WIN32__ || defined _WIN32
195 errno = WSAGetLastError();
198 if((errno == EINTR) || (errno == EAGAIN)) {
199 dbgPrint(
"TCP/IP, EINTR/EAGAIN, trying to continue.");
202 gErrPrint(formatString_tr(I18N_NOOP(
"read error %u, trying to reopen the socket"), errno).c_str());
208 if( !strncmp(&
buffer().at(len - eos_len), ceos, eos_len)) {
219 buffer().resize(length);
220 unsigned int len = 0;
222 while(len < length) {
223 int rlen = ::recv(m_socket, &buffer().at(len), 1, 0);
227 #if defined WINDOWS || defined __WIN32__ || defined _WIN32
228 errno = WSAGetLastError();
229 if((errno == WSAEINTR)) {
231 if((errno == EINTR) || (errno == EAGAIN)) {
233 dbgPrint(
"TCP/IP, EINTR/EAGAIN, trying to continue.");
236 gErrPrint(i18n(
"read error, trying to reopen the socket"));