cdlSocket.cpp
Go to the documentation of this file.
00001 
00002 /*
00003  *   Katana Native Interface - A C++ interface to the robot arm Katana.
00004  *   Copyright (C) 2005 Neuronics AG
00005  *   Check out the AUTHORS file for detailed contact information.
00006  *
00007  *   This program is free software; you can redistribute it and/or modify
00008  *   it under the terms of the GNU General Public License as published by
00009  *   the Free Software Foundation; either version 2 of the License, or
00010  *   (at your option) any later version.
00011  *
00012  *   This program is distributed in the hope that it will be useful,
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *   GNU General Public License for more details.
00016  *
00017  *   You should have received a copy of the GNU General Public License
00018  *   along with this program; if not, write to the Free Software
00019  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020  */
00021 /****************************************************************************/
00022 #include "KNI/cdlSocket.h"
00023 #include <iostream>
00024 /****************************************************************************/
00025 //test write:
00026 int writesz;
00027 //-------------------------------------------------------//
00028 #ifdef WIN32
00029 //-------------------------------------------------------//
00030 //Default arguments for local Katana Simulator:
00031 CCdlSocket::CCdlSocket(char* ipAddr, int port): _ipAddr(ipAddr), _port(port){
00032         //Getting the version and correct Winsock dll:
00033         WORD wVersionRequested = MAKEWORD(2, 2);
00034         WSADATA wdData;
00035         WSAStartup(wVersionRequested, &wdData);
00036         _socketfd = INVALID_SOCKET;
00037         if((_socketfd = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET){
00038                 return;
00039         }
00040         _socketAddr.sin_family = AF_INET;
00041         _socketAddr.sin_addr.s_addr = inet_addr(_ipAddr);
00042         _socketAddr.sin_port = htons(_port);
00043         if(connect(_socketfd, (SOCKADDR *) &_socketAddr, sizeof(_socketAddr)) == SOCKET_ERROR){
00044                 std::cout <<"client could not connect, check if server is running\n";
00045                 return;
00046         }
00047 }
00048 
00049 CCdlSocket::~CCdlSocket() {
00050         //close the socket:
00051         closesocket(_socketfd);
00052         WSACleanup();
00053 }
00054 
00055 int  CCdlSocket::send(const void* _buf, int _size) {
00056         writesz = -1;
00057         writesz = ::send(_socketfd, (char*) _buf, _size, 0);
00058         if(writesz < 0) throw DeviceWriteException( _ipAddr, strerror(errno) );
00059         if (writesz != _size) {
00060                 throw WriteNotCompleteException(_ipAddr);
00061         }
00062         return writesz;
00063 }
00064 
00065 int  CCdlSocket::recv(void* _buf, int _size) {
00066         int read_return;
00067         //recv is blocking...
00068         read_return = ::recv(_socketfd, (char*) _buf, _size, 0);
00069         if(read_return < 0) {
00070                 throw DeviceReadException( _ipAddr, strerror(errno));
00071         }
00072         // if (read_return != _size) {
00073         if (read_return == 0) {
00074                 throw ReadNotCompleteException(_ipAddr);
00075         }
00076         return read_return;
00077 }
00078 
00079 int CCdlSocket::disconnect(){
00080         closesocket(_socketfd);
00081         WSACleanup();
00082 
00083         return 0;
00084 }
00085 //-------------------------------------------------------//
00086 #else //LINUX
00087 //-------------------------------------------------------//
00088 //Default arguments for local Katana Simulator:
00089 CCdlSocket::CCdlSocket(char* ipAddr, int port): _ipAddr(ipAddr), _port(port){
00090         //filling in the _socketAddr structure:
00091         int res;
00092         _socketfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
00093         if(_socketfd == -1){
00094                 std::cout << "socket could not be created"<<_ipAddr<<" port: "<< _port<<" \n";
00095                 exit(1);
00096         }
00097         memset(&_socketAddr, 0, sizeof(_socketAddr));  
00098         _socketAddr.sin_family = AF_INET;
00099         _socketAddr.sin_addr.s_addr = inet_addr(_ipAddr);
00100         _socketAddr.sin_port = htons(_port);
00101         _len = sizeof(_socketAddr);
00102         //connect to the server
00103         res = inet_pton ( AF_INET, _ipAddr, &_socketAddr.sin_addr );
00104         if ( errno == EAFNOSUPPORT ){
00105                 std::cout << "inet_pton failed, try again "<<_ipAddr<<" port: "<< _port<<" \n";
00106                 exit(1);
00107         }
00108 //      std::cout << "trying to connect to server...\n";
00109         res = connect(_socketfd, (struct sockaddr *) &_socketAddr, _len);
00110         if(res != 0){
00111                 std::cout << "client could not connect, check if server is running on ip "<<_ipAddr<<" port: "<< _port<<" \n";
00112                 exit(1);
00113         }
00114         else{
00115 //              std::cout << "client connected to ip "<<_ipAddr<<", port: "<< _port<<" \n";
00116         }
00117         
00118 }
00119 
00120 CCdlSocket::~CCdlSocket() {
00121         //close the socket:
00122         close(_socketfd);
00123 }
00124 
00125 int  CCdlSocket::send(const void* _buf, int _size) {
00126         writesz = -1;
00127         writesz = ::send(_socketfd, _buf, _size, 0/*MSG_NOSIGNAL*/);
00128         if(writesz < 0){
00129                 throw DeviceWriteException( _ipAddr, strerror(errno) );
00130         }
00131 
00132         if (writesz != _size) {
00133                 throw WriteNotCompleteException(_ipAddr);
00134                 std::cout << "Write not complete: size and length of buf written do not match\n";
00135         }
00136         else{
00137 //              std::cout << "written: " << _buf << " _size: " << writesz << std::endl;
00138         }
00139         return writesz;
00140 }
00141 
00142 int  CCdlSocket::recv(void* _buf, int _size) {
00143         int read_return = 0;
00144         read_return = read(_socketfd, _buf, _size);
00145         //if (read_return != _size) {
00146         if (read_return <= 0) {
00147                 throw ReadNotCompleteException(_ipAddr);
00148                 std::cout << "Read not complete. Buffer size and message length do not match. buf: " << _buf << " _size: " << read_return << std::endl;
00149         }
00150         else{
00151 //              std::cout << "received: " << _buf << " _size: " << read_return << std::endl;
00152         }
00153         return read_return;
00154 }
00155 
00156 int CCdlSocket::disconnect(){
00157         //close the socket:
00158         close(_socketfd);
00159         return 0;
00160 }
00161 
00162 //-------------------------------------------------------//
00163 #endif //WIN32 else LINUX
00164 //-------------------------------------------------------//
00165 


kni
Author(s): Martin Günther
autogenerated on Thu Jun 6 2019 21:42:33