cdlSocket.cpp
Go to the documentation of this file.
1 
2 /*
3  * Katana Native Interface - A C++ interface to the robot arm Katana.
4  * Copyright (C) 2005 Neuronics AG
5  * Check out the AUTHORS file for detailed contact information.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21 /****************************************************************************/
22 #include "KNI/cdlSocket.h"
23 #include <iostream>
24 /****************************************************************************/
25 //test write:
26 int writesz;
27 //-------------------------------------------------------//
28 #ifdef WIN32
29 //-------------------------------------------------------//
30 //Default arguments for local Katana Simulator:
31 CCdlSocket::CCdlSocket(char* ipAddr, int port): _ipAddr(ipAddr), _port(port){
32  //Getting the version and correct Winsock dll:
33  WORD wVersionRequested = MAKEWORD(2, 2);
34  WSADATA wdData;
35  WSAStartup(wVersionRequested, &wdData);
36  _socketfd = INVALID_SOCKET;
37  if((_socketfd = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET){
38  return;
39  }
40  _socketAddr.sin_family = AF_INET;
41  _socketAddr.sin_addr.s_addr = inet_addr(_ipAddr);
42  _socketAddr.sin_port = htons(_port);
43  if(connect(_socketfd, (SOCKADDR *) &_socketAddr, sizeof(_socketAddr)) == SOCKET_ERROR){
44  std::cout <<"client could not connect, check if server is running\n";
45  return;
46  }
47 }
48 
50  //close the socket:
51  closesocket(_socketfd);
52  WSACleanup();
53 }
54 
55 int CCdlSocket::send(const void* _buf, int _size) {
56  writesz = -1;
57  writesz = ::send(_socketfd, (char*) _buf, _size, 0);
58  if(writesz < 0) throw DeviceWriteException( _ipAddr, strerror(errno) );
59  if (writesz != _size) {
61  }
62  return writesz;
63 }
64 
65 int CCdlSocket::recv(void* _buf, int _size) {
66  int read_return;
67  //recv is blocking...
68  read_return = ::recv(_socketfd, (char*) _buf, _size, 0);
69  if(read_return < 0) {
70  throw DeviceReadException( _ipAddr, strerror(errno));
71  }
72  // if (read_return != _size) {
73  if (read_return == 0) {
75  }
76  return read_return;
77 }
78 
80  closesocket(_socketfd);
81  WSACleanup();
82 
83  return 0;
84 }
85 //-------------------------------------------------------//
86 #else //LINUX
87 //-------------------------------------------------------//
88 //Default arguments for local Katana Simulator:
89 CCdlSocket::CCdlSocket(char* ipAddr, int port): _ipAddr(ipAddr), _port(port){
90  //filling in the _socketAddr structure:
91  int res;
92  _socketfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
93  if(_socketfd == -1){
94  std::cout << "socket could not be created"<<_ipAddr<<" port: "<< _port<<" \n";
95  exit(1);
96  }
97  memset(&_socketAddr, 0, sizeof(_socketAddr));
98  _socketAddr.sin_family = AF_INET;
99  _socketAddr.sin_addr.s_addr = inet_addr(_ipAddr);
100  _socketAddr.sin_port = htons(_port);
101  _len = sizeof(_socketAddr);
102  //connect to the server
103  res = inet_pton ( AF_INET, _ipAddr, &_socketAddr.sin_addr );
104  if ( errno == EAFNOSUPPORT ){
105  std::cout << "inet_pton failed, try again "<<_ipAddr<<" port: "<< _port<<" \n";
106  exit(1);
107  }
108 // std::cout << "trying to connect to server...\n";
109  res = connect(_socketfd, (struct sockaddr *) &_socketAddr, _len);
110  if(res != 0){
111  std::cout << "client could not connect, check if server is running on ip "<<_ipAddr<<" port: "<< _port<<" \n";
112  exit(1);
113  }
114  else{
115 // std::cout << "client connected to ip "<<_ipAddr<<", port: "<< _port<<" \n";
116  }
117 
118 }
119 
121  //close the socket:
122  close(_socketfd);
123 }
124 
125 int CCdlSocket::send(const void* _buf, int _size) {
126  writesz = -1;
127  writesz = ::send(_socketfd, _buf, _size, 0/*MSG_NOSIGNAL*/);
128  if(writesz < 0){
129  throw DeviceWriteException( _ipAddr, strerror(errno) );
130  }
131 
132  if (writesz != _size) {
134  std::cout << "Write not complete: size and length of buf written do not match\n";
135  }
136  else{
137 // std::cout << "written: " << _buf << " _size: " << writesz << std::endl;
138  }
139  return writesz;
140 }
141 
142 int CCdlSocket::recv(void* _buf, int _size) {
143  int read_return = 0;
144  read_return = read(_socketfd, _buf, _size);
145  //if (read_return != _size) {
146  if (read_return <= 0) {
148  std::cout << "Read not complete. Buffer size and message length do not match. buf: " << _buf << " _size: " << read_return << std::endl;
149  }
150  else{
151 // std::cout << "received: " << _buf << " _size: " << read_return << std::endl;
152  }
153  return read_return;
154 }
155 
157  //close the socket:
158  close(_socketfd);
159  return 0;
160 }
161 
162 //-------------------------------------------------------//
163 #endif //WIN32 else LINUX
164 //-------------------------------------------------------//
165 
virtual int disconnect()
Terminates the socket connection.
Definition: cdlSocket.cpp:156
virtual ~CCdlSocket()
Destructs the object.
Definition: cdlSocket.cpp:120
struct sockaddr_in _socketAddr
Structure to fill in the socket communication parameteres.
Definition: cdlSocket.h:88
virtual int send(const void *_buf, int _size)
Sends data to the socket.
Definition: cdlSocket.cpp:125
int _port
Port number of the KNI communication socket.
Definition: cdlSocket.h:71
int writesz
Definition: cdlSocket.cpp:26
int _len
Length of the message.
Definition: cdlSocket.h:73
char * _ipAddr
IP Address of the Robot or simulation environment.
Definition: cdlSocket.h:69
virtual int recv(void *_buf, int _size)
Receives data from the socket.
Definition: cdlSocket.cpp:142
CCdlSocket(char *adress, int port)
Constructs a CCdlSocket object.
Definition: cdlSocket.cpp:89
int _socketfd
File handler for the socket.
Definition: cdlSocket.h:86


kni
Author(s): Martin Günther
autogenerated on Fri Jan 3 2020 04:01:16