1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| /*
* serverfunction.cpp
*
* Created on: 18 mars 2010
*/
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string>
#include <iostream>
#include "serverfunction.h"
using namespace std;
//SOCKET SERVER
bool SocketServer::InitSocketServer(const char * Address, int Port)
{
bool CreateSocket = false;
struct sockaddr_in SocketServerParam,
SocketClientParam;
// Configure a socket server in connected mode...
SocketServerListen = socket(AF_INET, SOCK_STREAM, 0);
if (SocketServerListen == -1)
{
throw Error(1,"Problem in the creation of the socket",2);
}
else
{
cout<<"Socket Server is correctly create"<<endl;
memset(&SocketServerParam, 0, sizeof (struct sockaddr_in)) ;
/*Set the first numbyte of the block memory pointed by ptr to the specified value (ptr, value to be set, number of bytes to be set to the value)*/
SocketServerParam.sin_family = AF_INET;
// ...on specified address and port...
SocketServerParam.sin_port = htons(Port) ; //Port
SocketServerParam.sin_addr.s_addr = inet_addr(Address) ; //Address IP
if (SocketServerParam.sin_addr.s_addr == INADDR_NONE)
{
throw Error(2,"Problem in the set a socket parameter structure with the IP address and port",2);
}
else
{
/* bind the socket to the Internet address */
if (bind(SocketServerListen, (struct sockaddr *)&SocketServerParam, sizeof(struct sockaddr_in)) == -1)
{
throw Error(3,"Problem to bind the socket to the Internet Address",2);
}
else
{
// Allow IP address immediate reuse
int flagReuseAddress = 1;
if (setsockopt(SocketServerListen, SOL_SOCKET, SO_REUSEADDR,
&flagReuseAddress, sizeof (int)) == -1)
{
throw Error(4,"Problem to allow IP address",2);
}
else
{
// ...for one client
if (listen(SocketServerListen,2) == -1)
{
throw Error(5,"Problem to prepare and accept the connection",2);
}
else
{
cout<<"Listen and wait a client"<<endl;
// Initialize SocketServerCon variable
socklen_t ClientAdress=sizeof(SocketClientParam);
SocketServerCon = accept(SocketServerListen, (struct sockaddr *)&SocketClientParam, &ClientAdress);
if (SocketServerCon == -1)
{
cout<<"Problem in the accept function"<<endl;
//throw Error(5,"Problem to connect with the client",2);
}
else
{
cout<<"The connection is established with the client"<<endl;
// The connection is established with the client
CreateSocket = true;
}
}
}
}
}
}
return CreateSocket;
}
//CLOSE SOCKET
void SocketServer::CloseSocketServer()
{
close(SocketServerListen);
cout<<"Close the socket Server"<<endl;
}
//TEMPO
HMP_MicroSleepServer::HMP_MicroSleepServer(int TEMPO_USEC_CNX_SERV)
{
sleep(TEMPO_USEC_CNX_SERV);
}
//RECEIVE AND SEND LINE
void SocketServer::SendlineServer(string s)
{
s += '\n';
if (send(SocketServerCon, s.c_str(), s.length(), 0)==-1)
{
throw Error(6,"Error in the send line server function",2);
}
}
std::string SocketServer::ReceiveLineServer()
{
fd_set input_set; // Create Input set
int s;
struct timeval timeout;
timeout.tv_sec = 10; // Attends 50 msec pour returner.
timeout.tv_usec = 0;
string ret;
while (1){
//Set up the input,and exception sets for select().
FD_ZERO(&input_set);
FD_SET(SocketServerCon, &input_set);
s = select(SocketServerCon+1, &input_set, NULL, NULL,&timeout);
if (s) // Is there data coming in?
{
// tu fais un recv.
while (1)
{
char r;
switch (recv(SocketServerCon, &r, 1, 0))
{
case 0: // not connected anymore but last line sent might not end in \n so return ret anyway.
return ret;
case -1:
return "";
throw Error(7,"Error in the receive line server function",2);
}
ret += r;
if (r == '\n')
return ret;
}
}
if (s == -1)
{
throw Error(12,"Error in the receive line server function(select function)",2);
}
if (s == 0)
cout<<"No data coming in"<<endl;
ret =0x48;
return ret;
}
} |