00001 //version 1.0, base development ij 19/01/09 00002 00003 /* Modified by anna on 19.01.2008 */ 00004 /* 00005 canceled SetupParameters() and varaibles intialized in the constructors' headers; 00006 defined SEND_REC_MAX_SIZE (for compatibilty with mythen (and possibly other) pure C servers (i would move it to the common header file) 00007 00008 added #ifndef C_ONLY... to cutout class definition when including in pure C servers (can be removed if SEND_REC_MAX_SIZE is moved to the common header file) 00009 00010 defined private variables char hostname[1000] and int portno to store connection informations; 00011 00012 defined public functions int getHostname(char *name) and int getPortNumber() to retrieve connection informations 00013 00014 added public function int getErrorStatus() returning 1 if socketDescriptor<0 00015 00016 remove exits in the constructors and replace them with socketDescriptor=-1 00017 00018 replaced the argument of send/receive data with void (to avoid too much casting or compiler errors/warnings) 00019 00020 added a function which really does not close the socket between send/receive (senddataonly, receivedataonly) 00021 00022 */ 00023 #ifndef MY_SOCKET_TCP_H 00024 #define MY_SOCKET_TCP_H 00025 00026 #define SEND_REC_MAX_SIZE 4096 00027 #define DEFAULT_PORTNO 1952 00028 00029 00030 #include <arpa/inet.h> 00031 #include <netdb.h> 00032 #include <netinet/in.h> 00033 #include <unistd.h> 00034 00035 00036 #include <math.h> 00037 00038 class MySocketTCP{ 00039 00040 public: 00041 MySocketTCP(const char* const host_ip_or_name, unsigned short int const port_number); // sender (client): where to? ip 00042 MySocketTCP(unsigned short int const port_number); // receiver (server) local no need for ip 00043 ~MySocketTCP(); 00044 00045 int getHostname(char *name); 00046 int getPortNumber(){return portno;}; 00047 int getErrorStatus(){if (socketDescriptor<0) return 1; else return 0;}; 00048 00049 00050 00051 int Connect(); //establish connection a Disconnect should always follow 00052 void Disconnect(); //free connection 00053 00054 //The following two functions will connectioned->send/receive->disconnect 00055 int SendData(void* buf,int length);//length in characters 00056 int ReceiveData(void* buf,int length); 00057 00058 00059 //The following two functions stay connected, blocking other connections, and must be manually disconnected, 00060 // when the last call is a SendData() or ReceiveData() the disconnection will be done automatically 00061 //These function will also automatically disconnect->reconnect if 00062 // two reads (or two writes) are called in a row to preserve the data send/receive structure 00063 int SendDataAndKeepConnection(void* buf,int length); 00064 int ReceiveDataAndKeepConnection(void* buf,int length); 00065 00066 00067 // Danger! These functions do not connect nor disconnect nor flush the data! be sure that send-receive match perfectly on both server and client side! 00068 int SendDataOnly(void* buf,int length); 00069 int ReceiveDataOnly(void* buf,int length); 00070 00071 private: 00072 00073 char hostname[1000]; 00074 int portno; 00075 00076 int is_a_server; 00077 int socketDescriptor; 00078 struct sockaddr_in clientAddress, serverAddress; 00079 socklen_t clientAddress_length; 00080 00081 int file_des; 00082 00083 int send_rec_max_size; 00084 bool last_keep_connection_open_action_was_a_send; 00085 00086 00087 // void SetupParameters(); 00088 00089 }; 00090 #endif