style formatting, some warning cleanup.
This commit is contained in:
@@ -24,277 +24,287 @@ unsigned char ADRESSE_IP_SERVEUR [4] = {127,0,0,1};
|
|||||||
|
|
||||||
|
|
||||||
JSocket::JSocket(string ipAddr)
|
JSocket::JSocket(string ipAddr)
|
||||||
: state(NOT_AVAILABLE),
|
: state(NOT_AVAILABLE),
|
||||||
mfd(-1)
|
mfd(-1)
|
||||||
{
|
{
|
||||||
int result = -1;
|
int result;
|
||||||
|
struct hostent *hostentptr;
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
SOCKET Desc_Socket_Cliente;
|
SOCKADDR_IN Adresse_Socket_Server;
|
||||||
|
WORD wVersionRequested;
|
||||||
|
WSADATA wsaData;
|
||||||
|
|
||||||
|
wVersionRequested=MAKEWORD(1, 1);
|
||||||
|
result = WSAStartup(wVersionRequested,&wsaData);
|
||||||
|
if (result != 0)
|
||||||
|
{
|
||||||
|
DebugTrace("WSAStartup\t");
|
||||||
|
return;
|
||||||
|
}
|
||||||
#elif LINUX
|
#elif LINUX
|
||||||
int Desc_Socket_Cliente;
|
struct sockaddr_in Adresse_Socket_Server;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct hostent *hostentptr;
|
mfd = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
#ifdef WIN32
|
if(mfd < 0)
|
||||||
SOCKADDR_IN Adresse_Socket_Server;
|
return;
|
||||||
WORD wVersionRequested;
|
DebugTrace("Connecting " << ipAddr);
|
||||||
WSADATA wsaData;
|
|
||||||
|
|
||||||
wVersionRequested=MAKEWORD(1,1);
|
#ifdef WIN32
|
||||||
result = WSAStartup(wVersionRequested,&wsaData);
|
unsigned int addr_dest = inet_addr(ipAddr.c_str());
|
||||||
if(result!=0){
|
hostentptr=gethostbyaddr((char*) &addr_dest, 4, AF_INET);
|
||||||
DebugTrace("WSAStartup\t");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#elif LINUX
|
#elif LINUX
|
||||||
struct sockaddr_in Adresse_Socket_Server;
|
hostentptr = gethostbyname(ipAddr.c_str());
|
||||||
#endif
|
#endif
|
||||||
|
if (hostentptr == NULL)
|
||||||
mfd=socket(AF_INET,SOCK_STREAM,0);
|
{
|
||||||
if(mfd < 0)
|
DebugTrace("ERROR, no such host\n");
|
||||||
return;
|
return;
|
||||||
DebugTrace("Connecting " << ipAddr);
|
}
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
unsigned int addr_dest = inet_addr(ipAddr.c_str());
|
ZeroMemory( (char*)&Adresse_Socket_Server, sizeof(Adresse_Socket_Server));
|
||||||
hostentptr=gethostbyaddr((char*) &addr_dest,4,AF_INET);
|
|
||||||
#elif LINUX
|
#elif LINUX
|
||||||
hostentptr = gethostbyname(ipAddr.c_str());
|
bzero( (char*)&Adresse_Socket_Server, sizeof(Adresse_Socket_Server));
|
||||||
#endif
|
|
||||||
if (hostentptr == NULL) {
|
|
||||||
DebugTrace("ERROR, no such host\n");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef WIN32
|
|
||||||
ZeroMemory( (char*)&Adresse_Socket_Server,sizeof(Adresse_Socket_Server));
|
|
||||||
#elif LINUX
|
|
||||||
bzero( (char*)&Adresse_Socket_Server,sizeof(Adresse_Socket_Server));
|
|
||||||
#endif //WINDOWS
|
#endif //WINDOWS
|
||||||
|
|
||||||
Adresse_Socket_Server.sin_family=(*hostentptr).h_addrtype;
|
Adresse_Socket_Server.sin_family = (*hostentptr).h_addrtype;
|
||||||
Adresse_Socket_Server.sin_port=htons(SERVER_PORT);
|
Adresse_Socket_Server.sin_port = htons(SERVER_PORT);
|
||||||
Adresse_Socket_Server.sin_addr=*((struct in_addr*)(*hostentptr).h_addr);
|
Adresse_Socket_Server.sin_addr = *((struct in_addr*)(*hostentptr).h_addr);
|
||||||
|
|
||||||
result = connect(
|
result = connect(
|
||||||
mfd,
|
mfd,
|
||||||
(const struct sockaddr*)&Adresse_Socket_Server,
|
(const struct sockaddr*)&Adresse_Socket_Server,
|
||||||
sizeof(Adresse_Socket_Server));
|
sizeof(Adresse_Socket_Server));
|
||||||
if (result != 0){
|
if (result != 0)
|
||||||
DebugTrace("client connect failed :" << strerror(errno));
|
{
|
||||||
state = FATAL_ERROR;
|
DebugTrace("client connect failed :" << strerror(errno));
|
||||||
return;
|
state = FATAL_ERROR;
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
state = CONNECTED;
|
state = CONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool JSocket::SetNonBlocking(int sock)
|
bool JSocket::SetNonBlocking(int sock)
|
||||||
{
|
{
|
||||||
int opts;
|
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#elif LINUX
|
#elif LINUX
|
||||||
opts = fcntl(sock,F_GETFL);
|
int opts = fcntl(sock,F_GETFL);
|
||||||
if (opts < 0) {
|
if (opts < 0)
|
||||||
perror("fcntl(F_GETFL)");
|
{
|
||||||
return false;
|
perror("fcntl(F_GETFL)");
|
||||||
}
|
return false;
|
||||||
opts = (opts | O_NONBLOCK);
|
}
|
||||||
if (fcntl(sock,F_SETFL,opts) < 0) {
|
opts = (opts | O_NONBLOCK);
|
||||||
perror("fcntl(F_SETFL)");
|
if (fcntl(sock,F_SETFL,opts) < 0)
|
||||||
return false;
|
{
|
||||||
}
|
perror("fcntl(F_SETFL)");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
#endif //WINDOWS
|
#endif //WINDOWS
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSocket::JSocket()
|
JSocket::JSocket()
|
||||||
: state(NOT_AVAILABLE),
|
: state(NOT_AVAILABLE),
|
||||||
mfd(-1)
|
mfd(-1)
|
||||||
{
|
{
|
||||||
int result;
|
int result;
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
SOCKADDR_IN Adresse_Socket_Connection;
|
SOCKADDR_IN Adresse_Socket_Connection;
|
||||||
WORD wVersionRequested;
|
WORD wVersionRequested;
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
|
|
||||||
wVersionRequested=MAKEWORD(1,1);
|
wVersionRequested = MAKEWORD(1,1);
|
||||||
result=WSAStartup(wVersionRequested,&wsaData);
|
result = WSAStartup(wVersionRequested,&wsaData);
|
||||||
|
|
||||||
if(result!=0){
|
if (result != 0)
|
||||||
DebugTrace("WSAStartup\t");
|
{
|
||||||
return;
|
DebugTrace("WSAStartup\t");
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
#elif LINUX
|
#elif LINUX
|
||||||
struct sockaddr_in Adresse_Socket_Connection;
|
struct sockaddr_in Adresse_Socket_Connection;
|
||||||
#endif //WINDOWS
|
#endif //WINDOWS
|
||||||
|
|
||||||
mfd=socket( AF_INET, SOCK_STREAM,0);
|
mfd=socket( AF_INET, SOCK_STREAM,0);
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#elif LINUX
|
#elif LINUX
|
||||||
int reuse_addr = 1; /* Used so we can re-bind to our port
|
int reuse_addr = 1; /* Used so we can re-bind to our port
|
||||||
while a previous connection is still
|
while a previous connection is still
|
||||||
in TIME_WAIT state. */
|
in TIME_WAIT state. */
|
||||||
/* So that we can re-bind to it without TIME_WAIT problems */
|
/* So that we can re-bind to it without TIME_WAIT problems */
|
||||||
setsockopt(mfd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr,
|
setsockopt(mfd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr,
|
||||||
sizeof(reuse_addr));
|
sizeof(reuse_addr));
|
||||||
#endif //WINDOWS
|
#endif //WINDOWS
|
||||||
|
|
||||||
SetNonBlocking(mfd);
|
SetNonBlocking(mfd);
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
ZeroMemory( &Adresse_Socket_Connection,sizeof(Adresse_Socket_Connection));
|
ZeroMemory( &Adresse_Socket_Connection,sizeof(Adresse_Socket_Connection));
|
||||||
#elif LINUX
|
#elif LINUX
|
||||||
bzero( &Adresse_Socket_Connection,sizeof(Adresse_Socket_Connection));
|
bzero( &Adresse_Socket_Connection,sizeof(Adresse_Socket_Connection));
|
||||||
#endif //WINDOWS
|
#endif //WINDOWS
|
||||||
Adresse_Socket_Connection.sin_family=AF_INET;
|
Adresse_Socket_Connection.sin_family = AF_INET;
|
||||||
Adresse_Socket_Connection.sin_port=htons(SERVER_PORT);
|
Adresse_Socket_Connection.sin_port = htons(SERVER_PORT);
|
||||||
|
|
||||||
result=::bind(mfd,
|
result = ::bind(mfd,
|
||||||
(struct sockaddr*)&Adresse_Socket_Connection,
|
(struct sockaddr*)&Adresse_Socket_Connection,
|
||||||
sizeof(Adresse_Socket_Connection));
|
sizeof(Adresse_Socket_Connection));
|
||||||
if(result!=0){
|
if (result != 0)
|
||||||
state = FATAL_ERROR;
|
{
|
||||||
DebugTrace("bind error:" << strerror(errno));
|
state = FATAL_ERROR;
|
||||||
return;
|
DebugTrace("bind error:" << strerror(errno));
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
result=listen(mfd,1);
|
result = listen(mfd,1);
|
||||||
if(result!=0){
|
if(result != 0)
|
||||||
state = FATAL_ERROR;
|
{
|
||||||
DebugTrace("listen error:" << strerror(errno));
|
state = FATAL_ERROR;
|
||||||
return;
|
DebugTrace("listen error:" << strerror(errno));
|
||||||
}
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
state = DISCONNECTED;
|
state = DISCONNECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
JSocket::JSocket(int fd)
|
JSocket::JSocket(int fd)
|
||||||
: state(CONNECTED),
|
: state(CONNECTED),
|
||||||
mfd(fd)
|
mfd(fd)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
JSocket::~JSocket(){
|
JSocket::~JSocket()
|
||||||
Disconnect();
|
{
|
||||||
|
Disconnect();
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void JSocket::Disconnect()
|
void JSocket::Disconnect()
|
||||||
{
|
{
|
||||||
state = JSocket::DISCONNECTED;
|
state = JSocket::DISCONNECTED;
|
||||||
if(mfd) {
|
if(mfd)
|
||||||
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
closesocket(mfd);
|
closesocket(mfd);
|
||||||
#elif LINUX
|
#elif LINUX
|
||||||
close(mfd);
|
close(mfd);
|
||||||
#endif
|
#endif
|
||||||
mfd = 0;
|
mfd = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JSocket* JSocket::Accept()
|
JSocket* JSocket::Accept()
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
SOCKADDR_IN Adresse_Socket_Cliente;
|
SOCKADDR_IN Adresse_Socket_Cliente;
|
||||||
int Longueur_Adresse;
|
int Longueur_Adresse;
|
||||||
#elif LINUX
|
#elif LINUX
|
||||||
struct sockaddr_in Adresse_Socket_Cliente;
|
struct sockaddr_in Adresse_Socket_Cliente;
|
||||||
socklen_t Longueur_Adresse;
|
socklen_t Longueur_Adresse;
|
||||||
#endif //WINDOWS
|
#endif //WINDOWS
|
||||||
|
|
||||||
while(mfd) {
|
JSocket* socket = NULL;
|
||||||
fd_set set;
|
|
||||||
FD_ZERO(&set);
|
|
||||||
FD_SET(mfd, &set);
|
|
||||||
struct timeval tv;
|
|
||||||
tv.tv_sec = 0;
|
|
||||||
tv.tv_usec = 1000*100;
|
|
||||||
|
|
||||||
int result = select(mfd+1, &set, NULL, NULL, &tv);
|
while (mfd)
|
||||||
if( result > 0 && FD_ISSET(mfd, &set) ) {
|
{
|
||||||
|
fd_set set;
|
||||||
|
FD_ZERO(&set);
|
||||||
|
FD_SET(mfd, &set);
|
||||||
|
struct timeval tv;
|
||||||
|
tv.tv_sec = 0;
|
||||||
|
tv.tv_usec = 1000 * 100;
|
||||||
|
|
||||||
Longueur_Adresse = sizeof(Adresse_Socket_Cliente);
|
int result = select(mfd+1, &set, NULL, NULL, &tv);
|
||||||
int val=accept(
|
if (result > 0 && FD_ISSET(mfd, &set))
|
||||||
mfd,
|
{
|
||||||
(struct sockaddr*)&Adresse_Socket_Cliente,
|
|
||||||
&Longueur_Adresse);
|
|
||||||
DebugTrace("connection on client port "<< ntohs(Adresse_Socket_Cliente.sin_port));
|
|
||||||
|
|
||||||
if(val >=0 ) {
|
Longueur_Adresse = sizeof(Adresse_Socket_Cliente);
|
||||||
state = CONNECTED;
|
int val = accept(
|
||||||
return new JSocket(val);
|
mfd,
|
||||||
} else {
|
(struct sockaddr*)&Adresse_Socket_Cliente,
|
||||||
return NULL;
|
&Longueur_Adresse);
|
||||||
}
|
DebugTrace("connection on client port "<< ntohs(Adresse_Socket_Cliente.sin_port));
|
||||||
}
|
|
||||||
}
|
if (val >= 0)
|
||||||
|
{
|
||||||
|
state = CONNECTED;
|
||||||
|
socket = new JSocket(val);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
int JSocket::Read(char* buff, int size)
|
int JSocket::Read(char* buff, int size)
|
||||||
{
|
{
|
||||||
if(state == CONNECTED)
|
if (state == CONNECTED)
|
||||||
{
|
{
|
||||||
fd_set set;
|
fd_set set;
|
||||||
FD_ZERO(&set);
|
FD_ZERO(&set);
|
||||||
FD_SET(mfd, &set);
|
FD_SET(mfd, &set);
|
||||||
struct timeval tv;
|
struct timeval tv;
|
||||||
tv.tv_sec = 0;
|
tv.tv_sec = 0;
|
||||||
tv.tv_usec = 1000*100;
|
tv.tv_usec = 1000 * 100;
|
||||||
|
|
||||||
int result = select(mfd+1, &set, NULL, NULL, &tv);
|
int result = select(mfd+1, &set, NULL, NULL, &tv);
|
||||||
if( result > 0 && FD_ISSET(mfd, &set) )
|
if (result > 0 && FD_ISSET(mfd, &set))
|
||||||
{
|
{
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
int readbytes = recv(mfd, buff, size,0);
|
int readbytes = recv(mfd, buff, size, 0);
|
||||||
#elif LINUX
|
#elif LINUX
|
||||||
int readbytes = read(mfd, buff, size);
|
int readbytes = read(mfd, buff, size);
|
||||||
#endif //WINDOWS
|
#endif //WINDOWS
|
||||||
if(readbytes < 0)
|
if(readbytes < 0)
|
||||||
{
|
{
|
||||||
DebugTrace("Error reading from socket\n");
|
DebugTrace("Error reading from socket\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
return readbytes;
|
return readbytes;
|
||||||
}
|
}
|
||||||
else if( result < 0)
|
else if( result < 0)
|
||||||
{
|
{
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int JSocket::Write(char* buff, int size)
|
int JSocket::Write(char* buff, int size)
|
||||||
{
|
{
|
||||||
int size1 = size;
|
int size1 = size;
|
||||||
while (size > 0 && state == CONNECTED) {
|
while (size > 0 && state == CONNECTED)
|
||||||
fd_set set;
|
{
|
||||||
FD_ZERO(&set);
|
fd_set set;
|
||||||
FD_SET(mfd, &set);
|
FD_ZERO(&set);
|
||||||
struct timeval tv;
|
FD_SET(mfd, &set);
|
||||||
tv.tv_sec = 0;
|
struct timeval tv;
|
||||||
tv.tv_usec = 1000*100;
|
tv.tv_sec = 0;
|
||||||
|
tv.tv_usec = 1000 * 100;
|
||||||
|
|
||||||
int result = select(mfd+1, NULL, &set, NULL, &tv);
|
int result = select(mfd+1, NULL, &set, NULL, &tv);
|
||||||
if( result > 0 && FD_ISSET(mfd, &set))
|
if( result > 0 && FD_ISSET(mfd, &set))
|
||||||
{
|
{
|
||||||
int len = send(mfd, buff, size, 0);
|
int len = send(mfd, buff, size, 0);
|
||||||
if (len < 0) {
|
if (len < 0)
|
||||||
return -1;
|
{
|
||||||
}
|
return -1;
|
||||||
size -= len;
|
}
|
||||||
buff += len;
|
size -= len;
|
||||||
} else if (result < 0) {
|
buff += len;
|
||||||
return -1;
|
}
|
||||||
}
|
else if (result < 0)
|
||||||
}
|
{
|
||||||
return size1 - size;
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return size1 - size;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user