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
| #include <sys/types.h> /* Primitive System Data Types */
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <netdb.h>
#include <string.h>
#include <iostream>
#include "serveur.h"
#define _MAX_HOST_LENGTH_ 100
using namespace std;
serveur::serveur(int p){
port = p;
running = false;
}
int serveur::init(){
struct in_addr MyAddress;
struct hostent *host;
char HostName[_MAX_HOST_LENGTH_];
gethostname( HostName, _MAX_HOST_LENGTH_ );
host = gethostbyname( HostName );
memcpy( &MyAddress.s_addr, host->h_addr_list[0], sizeof( MyAddress.s_addr ) );
ServerAddr.sin_family = AF_INET;
ServerAddr.sin_port = htons( port );
ServerAddr.sin_addr.s_addr = INADDR_ANY ;
cout <<"server initialized" << endl;
return 0;
}
int serveur::start (){
struct sockaddr_in ClientAddr;
socklen_t ClientAddrLen;
pthread_t hProcessThread;
int NewConnection;
struct thread_param p;
ListeningSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP ) ;
cout << "bind:" << bind( ListeningSocket, (sockaddr *)&ServerAddr, sizeof( ServerAddr ) ) << endl;
cout << "listen:" << listen( ListeningSocket, 5 ) << endl;
cout << "server started : listening to port: " << port << endl;
running = true;
ClientAddrLen = sizeof( ClientAddr );
while(running){
cout<<"wait accept"<<endl;
NewConnection = accept( ListeningSocket, (sockaddr *) &ClientAddr, &ClientAddrLen);
cout<<"accepted:"<<NewConnection;
p.ser = this;
p.soc = NewConnection;
cout << "client conected :: IP : " <<inet_ntoa( ClientAddr.sin_addr )<< " ,port = " <<ntohs( ClientAddr.sin_port ) << endl;
pthread_create (&hProcessThread, NULL, &serveur::ThreadLauncher, &p);
}
return 0;
}
int serveur::pause (){
running = false;
cout << "Serveur en pause" << endl;
close( ListeningSocket );
return 0;
}
/* ======================================================================== */
/* ========================== thread proc ================================= */
/* ======================================================================== */
void serveur::ClientThread(int soc){
cout << "kikou" << endl;
/* A mettre ici : code relatif au protocole utilisé */
} |
Partager