Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "KNI/cdlSocket.h"
00023 #include <iostream>
00024
00025
00026 int writesz;
00027
00028 #ifdef WIN32
00029
00030
00031 CCdlSocket::CCdlSocket(char* ipAddr, int port): _ipAddr(ipAddr), _port(port){
00032
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
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
00068 read_return = ::recv(_socketfd, (char*) _buf, _size, 0);
00069 if(read_return < 0) {
00070 throw DeviceReadException( _ipAddr, strerror(errno));
00071 }
00072
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
00089 CCdlSocket::CCdlSocket(char* ipAddr, int port): _ipAddr(ipAddr), _port(port){
00090
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
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
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
00116 }
00117
00118 }
00119
00120 CCdlSocket::~CCdlSocket() {
00121
00122 close(_socketfd);
00123 }
00124
00125 int CCdlSocket::send(const void* _buf, int _size) {
00126 writesz = -1;
00127 writesz = ::send(_socketfd, _buf, _size, 0);
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
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
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
00152 }
00153 return read_return;
00154 }
00155
00156 int CCdlSocket::disconnect(){
00157
00158 close(_socketfd);
00159 return 0;
00160 }
00161
00162
00163 #endif //WIN32 else LINUX
00164
00165