socketTCP.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //
00003 // $Id$
00004 //
00005 // Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno    
00006 //
00007 // This file is part of MIP.
00008 //
00009 // MIP is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // MIP is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
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 // #define TCP_MSG_SIZE 255
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    /*portare nella derivata*/
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 /* @} */

Generated on Mon Feb 20 07:01:06 2017 for MIP by  doxygen 1.5.6