socketTCP.h
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
00023
00024
00025
00026
00033
00035
00036
00037 #ifndef __SOCKET_TCP_
00038 #define __SOCKET_TCP_
00039
00040 #ifdef MIP_HOST_APPLE
00041 #include <applePatch.h>
00042 #endif
00043
00044 #include <sys/types.h>
00045 #include <sys/socket.h>
00046 #include <sys/signal.h>
00047 #include <netinet/in.h>
00048 #include <netdb.h>
00049 #include <stdio.h>
00050 #include <unistd.h>
00051 #include <string.h>
00052 #include <stdlib.h>
00053 #include <iostream>
00054 #include <sstream>
00055
00056 #include <Types.h>
00057 #include <LogTrace.h>
00058
00059 using namespace std;
00060
00061 #define MAX_CODA 5
00062
00063 #define TCP_MSG_SIZE 15
00064
00068 class SocketTCP{
00069
00070 protected:
00071 public:
00072 int getInt(int &n);
00073 int getDouble(Decimal &d);
00074 int sendInt(int n);
00075 int sendDouble(Decimal d);
00076
00080 int sendMessage(char * buf, int size);
00084 int getMessage(char * buf, int size);
00087 int waitMessage(char* msg);
00088
00089
00090
00091 public:
00092
00094 int m_port;
00095 int m_ds_sock_client;
00097 struct sockaddr_in my_addr;
00098
00099
00101 char m_buffer[TCP_MSG_SIZE+1];
00103 char *buffer;
00105 int bufsize;
00106
00108 SocketTCP();
00109
00112 void setBufSize(int a){
00113 if (buffer != NULL) delete buffer;
00114 bufsize = a;
00115 buffer = (char*) new char[bufsize+1];
00116 }
00117
00119 void ack(){ sendMessage((char*)"ACK", TCP_MSG_SIZE);}
00121 void ok() { sendMessage((char*)"ok", TCP_MSG_SIZE);}
00123 void waitAck(){ waitMessage((char*)"ACK");}
00125 void waitOk() { waitMessage((char*)"ok");}
00126
00128 int checkMessagePresence(void);
00129 void closeConnection();
00130 };
00131
00132
00136 class ServerTCP : public SocketTCP{
00137 public:
00138 int m_port;
00139 int m_ds_sock;
00140 struct sockaddr_in m_server;
00141
00142 ServerTCP(const int port);
00143 ServerTCP(const int port,const int endPort);
00144
00145 void waitConnection();
00146 int waitClient(char *msg);
00147
00148
00149 bool send(string &msg, const int size);
00150 };
00151
00155 class ClientTCP : public SocketTCP{
00156 public:
00157 int m_port;
00158 struct sockaddr_in m_client;
00159
00160 ClientTCP(int &res, const int port, const char* server_addr);
00161
00162 int receive(string *msg );
00163
00164 int waitServer(char* msg);
00165 };
00166
00167
00171 class TCPClient : public MIPObject{
00172 private:
00173 int sockFileDescr, portNumber, n;
00174 struct sockaddr_in servAddr;
00175 struct hostent *server;
00176
00177 static const int _packetSize = 1024;
00178
00180 string getObjectName() const {
00181 return "TCPClient";
00182 }
00183
00184 public:
00185
00189 TCPClient(string ip,int port);
00190
00194 int send(string& message);
00195
00199 int receive(string& message);
00200
00203 int checkMessagePresence(void);
00204 };
00205
00206
00207 #endif
00208
00209