19 #define HEADER_LENGTH 6 // byte1=id1, byte2=id2, byte3+byte4=packetSize, byte5+byte6=packetsLeftToRead 38 #elif defined (__linux) || defined (__APPLE__) 47 if (WSAStartup(0x101,&_socketWsaData)!=0)
50 _socketConn=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
67 #elif defined (__linux) || defined (__APPLE__) 119 std::vector<char> receivedData;
122 std::vector<char> inDat;
129 receivedData.insert(receivedData.end(),inDat.begin(),inDat.end());
132 dataSize=int(receivedData.size());
133 char* retBuff=
new char[dataSize];
134 for (
int i=0;i<dataSize;i++)
135 retBuff[i]=receivedData[i];
146 unsigned short s=(
unsigned short)packetLength;
150 ((
unsigned short*)(header+2))[0]=s;
151 ((
unsigned short*)(header+2))[1]=packetsLeft;
153 std::vector<char> toSend;
155 toSend.push_back(header[i]);
156 for (
int i=0;i<packetLength;i++)
157 toSend.push_back(packet[i]);
159 int dl=send(
_socketConn,&toSend[0],packetLength+HEADER_LENGTH,0);
160 if (dl==packetLength+HEADER_LENGTH)
185 unsigned short dataLength=((
unsigned short*)(headerAndSize+2))[0];
188 packet.resize(dataLength,0);
191 while(totalReceived!=dataLength)
193 int nb=recv(
_socketConn,&packet[0]+totalReceived,dataLength-totalReceived,0);
200 if (totalReceived!=dataLength)
202 return(
int(((
unsigned short*)(headerAndSize+2))[1]));
208 return(timeGetTime()&0x03ffffff);
209 #elif defined (__linux) || defined (__APPLE__) 211 unsigned int result=0;
212 if (gettimeofday(&tv,NULL)==0)
213 result=(tv.tv_sec*1000+tv.tv_usec/1000)&0x03ffffff;
221 if (currentTime<lastTime)
222 return(currentTime+0x03ffffff-lastTime);
223 return(currentTime-lastTime);
bool sendData(char *data, int dataSize)
struct sockaddr_in _socketServer
char * receiveReplyData(int &dataSize)
bool _sendSimplePacket(char *packet, int packetLength, unsigned short packetsLeft)
virtual ~CSocketOutConnection()
int _socketConnectionPort
int _getTimeDiffInMs(int lastTime)
unsigned short _maxPacketSize
std::string _socketConnectionAddress
int _receiveSimplePacket(std::vector< char > &packet)
CSocketOutConnection(const char *theConnectionAddress, int theConnectionPort, unsigned short maxPacketSize=250, char headerID1=59, char headerID2=57)