socket.cpp
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
00029 #include "socket.h"
00030
00034 Socket::Socket()
00035 {
00036 this->mySock = -1 ;
00037 this->state = false;
00038 this->queue = "";
00039 }
00040
00044 Socket::~Socket()
00045 {
00046 this->closeSock();
00047 }
00048
00057 bool Socket::connectSock(int port, string serverName, string dedicatedIP)
00058 {
00059 struct sockaddr_in sockname;
00060 struct hostent *hostname;
00061 hostname = gethostbyname (serverName.c_str());
00062 if ( hostname == NULL )
00063 return false;
00064 if (hostname->h_addrtype != AF_INET)
00065 return false;
00066 if ((this->mySock=socket(AF_INET, SOCK_STREAM, 0)) <0)
00067 return false;
00068 memset ((char *) &sockname, 0, sizeof (struct sockaddr_in));
00069 memcpy ((char *) &sockname.sin_addr, (char*)hostname->h_addr, hostname->h_length);
00070 sockname.sin_family = AF_INET;
00071 sockname.sin_port = htons ((u_short)port);
00072 if ( dedicatedIP != "" )
00073 {
00074 struct sockaddr_in msim;
00075 unsigned long opt = 1;
00076 msim.sin_addr.s_addr = inet_addr (dedicatedIP.c_str());
00077 msim.sin_family = AF_INET;
00078 msim.sin_port = htons (0);
00079 setsockopt (this->mySock, SOL_SOCKET, SO_REUSEADDR, (char *) &opt,sizeof (opt));
00080 if (bind (this->mySock, (struct sockaddr *) &msim, sizeof (msim)) < 0)
00081 {
00082 cout << "ERROR : unable to bind socket"<<endl;
00083 exit(-1);
00084 }
00085 }
00086 if ((connect (this->mySock, (struct sockaddr *) &sockname, sizeof (struct sockaddr))) < 0)
00087 return false;
00088 this->state = true;
00089 return true;
00090 }
00091
00096 bool Socket::closeSock()
00097 {
00098 if ( close(this->mySock) < 0 ) {
00099 return false ;
00100 }
00101 else {
00102 this->state = false;
00103 return true ;
00104 }
00105 }
00106
00111 bool Socket::getState()
00112 {
00113 return this->state;
00114 }
00115
00123 string Socket::getOldestMessage()
00124 {
00125 string buff;
00126 string::size_type pos ;
00127 if((pos=this->queue.find("\n")) != string::npos) {
00128 buff = this->queue.substr(0,pos+1);
00129 this->queue.erase(0,buff.length());
00130 return buff;
00131 }
00132 this->queue += this->receive();
00133 if((pos=this->queue.find("\n")) != string::npos) {
00134 buff = this->queue.substr(0,pos+1);
00135 this->queue.erase(0,buff.length());
00136 return buff;
00137 }
00138 return "";
00139 }
00140
00145 string Socket::receive()
00146 {
00147 const unsigned int MAXDATAS = 1024;
00148 char buffer[MAXDATAS];
00149 int numbytes ;
00150 struct timeval tv;
00151 fd_set readfds;
00152 tv.tv_sec = 0;
00153 tv.tv_usec = 050000;
00154 FD_ZERO(&readfds);
00155 FD_SET(this->mySock, &readfds);
00156 if (select(this->mySock+1, &readfds, NULL, NULL, &tv) >=0 ) {
00157 if (FD_ISSET(this->mySock, &readfds)) {
00158 if((numbytes=recv(this->mySock,buffer,MAXDATAS, 0))<=0) {
00159 this->closeSock();
00160 return "";
00161 }
00162 else {
00163 return ((string)buffer).substr(0,numbytes);
00164 }
00165 }
00166 else {
00167 return "";
00168 }
00169 }
00170 this->state = false;
00171 return "";
00172 }
00173
00179 bool Socket::sendStr(string strData)
00180 {
00181 if( send(this->mySock, strData.c_str(), strData.length(),MSG_NOSIGNAL) < 0 ) {
00182 return false ;
00183 }
00184 else {
00185 return true ;
00186 }
00187 }