Compare commits

..

No commits in common. "tcp" and "master" have entirely different histories.
tcp ... master

17 changed files with 93 additions and 443 deletions

Binary file not shown.

View File

View File

@ -1,99 +0,0 @@
#include "server.h"
void traiterClient(int socket){
requete req;
int nb_octet;
int fini=0;
char i = 0;
while(!fini){
nb_octet = read(socket,&req,sizeof(requete));
IF_NB_OCTET_BREAK
if(nb_octet == 0){
break;
}
DEBUG&& printf("Request received (operation/size) : (%d/%d)\n",req.type,req.taille);
switch(req.type){
case FIN:
{
fini = 1;
break;
}
case FACTO:
{
int nb;
long res;
DEBUG&& debug("loop","FACTO asked");
nb_octet = read(socket,&nb,sizeof(int));
IF_NB_OCTET_BREAK
DEBUG&& printf("%d\n",nb);
res = factoriel(nb);
DEBUG&& debug("loop","Sending result");
nb_octet = write(socket, &res, sizeof(res));
IF_NB_OCTET_BREAK
DEBUG&& printf("done\n");
break;
}
case POW:
{
int a,b;
long res;
nb_octet = read(socket, &a, sizeof(int));
IF_NB_OCTET_BREAK
nb_octet = read(socket, &b, sizeof(int));
IF_NB_OCTET_BREAK
res = puissance(a,b);
nb_octet = write(socket, &res, sizeof(res));
IF_NB_OCTET_BREAK
break;
}
case ANALYSE:
{
int nb_elem, attendu;
int* donnees;
char message[MAX_BUFFER_LENGTH];
res_analyse_donnees res;
nb_elem = req.taille/sizeof(int);
donnees = (int*) malloc(nb_elem*sizeof(int));
attendu = req.taille;
while(attendu > 0){
nb_octet = read(socket, message, MAX_BUFFER_LENGTH);
IF_NB_OCTET_BREAK
memcpy(donnees+(req.taille - attendu), message,nb_octet);
attendu -= nb_octet;
}
analyser_donnees(donnees,nb_elem,&res);
write(socket, &res, sizeof(res));
free (donnees);
break;
}
}
}
}

View File

@ -1,16 +0,0 @@
#ifndef _RPCSERVER_H_
#define _RPCSERVER_H_
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "../calculs.h"
#include "../lib.h"
#define IF_NB_OCTET_BREAK if(nb_octet == -1){DEBUG&& debug("loop","Breaking the loop\n");fini = 1; break;}
#define MAX_BUFFER_LENGTH 200
void traiterClient(int socket);
#endif

View File

@ -1,41 +0,0 @@
#include "calculs.h"
long factoriel(int nb)
{
int i;
long res;
res = 1;
for (i=nb;i>1;i--)
res = res * i;
return res;
}
void analyser_donnees(int donnees[], int taille, res_analyse_donnees *res)
{
int i;
long somme;
res -> min = donnees[0];
res -> max = donnees[0];
somme = donnees[0];
for (i=1; i < taille; i++)
{
if (donnees[i] > res -> max) res -> max = donnees[i];
if (donnees[i] < res -> min) res -> min = donnees[i];
somme += donnees[i];
}
res -> moy = ((float)somme) / taille;
}
long puissance(int nb, int puiss)
{
long res = nb;
int i;
for (i=1; i < puiss; i++)
res = res * nb;
return res;
}

View File

@ -1,15 +0,0 @@
#ifndef _CALCULS_H_
typedef struct {
float moy;
int max;
int min;
}res_analyse_donnees;
long factoriel(int nb);
void analyser_donnees(int donnees[], int taille, res_analyse_donnees *res);
long puissance(int nb, int puiss);
#endif

BIN
client Executable file

Binary file not shown.

View File

@ -8,16 +8,14 @@ int main(int argc, char* argv[]){
=========================================================*/
/* (1) Socket information */
struct sockaddr_in server_addr; // server info
int sock, // socket
port; // chosen port
int sock; // socket
int port; // chosen port
char hostname[24]; // chosen hostname
/* (2) Misc. information */
int bytes; // transfer count
char* to_send = (char*) malloc( sizeof(requete)+sizeof(int)+1 );
long to_recv;
requete request;
int param;
char* to_send = (char*) malloc( BUFSIZE * sizeof(char) );
char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) );
/* [2] Manage arguments
@ -53,44 +51,36 @@ int main(int argc, char* argv[]){
/* [3] Create TCP socket and get target data
/* [3] Create UPD socket and get target data
=========================================================*/
sock = xconnect(hostname, port, &server_addr);
/* (1-) Manage error */
if( sock == -1 ){
perror("erreur connection server");
perror("erreur création socket");
exit(1);
}
request.type = FACTO;
//size of the request isn't important
request.taille = sizeof(int);
param = 2;
memcpy(to_send,&request,sizeof(requete));
memcpy(to_send+sizeof(requete),&param,sizeof(int));
bytes = write(sock,to_send,sizeof(requete)+sizeof(int));
sleep(2);
param = 3;
memcpy(to_send+sizeof(requete),&param,sizeof(int));
bytes = write(sock,to_send,sizeof(requete)+sizeof(int));
/* [4] Send message
=========================================================*/
/* (1) Send message to server */
bytes = xsend(sock, to_send, &server_addr);
/* (2) Check if send succesfully */
if( bytes == -1 ){
perror("erreur envoi requete");
perror("erreur envoi message");
exit(1);
}
DEBUG&& printf("*** sent %d bytes\n", bytes);
/* [5] Wait for response
=========================================================*/
/* (1) Wait for response */
bytes = recv(sock, &to_recv, sizeof(long),0);
bytes = xlisten(sock, &server_addr, to_recv, BUFSIZE);
/* (2) Check if received successfully (and consistently) */
if( bytes == -1 ){
@ -99,13 +89,12 @@ int main(int argc, char* argv[]){
}
/* (4) log */
printf("*** received : '%ld'\n", to_recv);
printf("*** received : '%s'\n", to_recv);
/* [6] Close socket
=========================================================*/
close(sock);
printf("Closing socket\n");
return 0;
}

BIN
client.o

Binary file not shown.

99
lib.c
View File

@ -25,15 +25,15 @@ int xbind(const int port){
/* [0] Initialization
=========================================================*/
int xsocket, bound, clientsock;
int xsocket, bound;
static struct sockaddr_in addr;
/* [1] Create xsocket
=========================================================*/
/* (1) Create UPD xsocket */
xsocket = socket(AF_INET, SOCK_DGRAM, 0);
DEBUG&& debug("xbind", "creating server socket");
xsocket = socket(AF_INET, SOCK_STREAM, 0);
/* (r2-) Manage error */
if( xsocket == -1 ){
@ -58,8 +58,8 @@ int xbind(const int port){
/* [3] Bind to port
=========================================================*/
/* (1) Bind and return -1 if error */
DEBUG&& debug("xbind", "binding socket to port");
bound = bind(xsocket, (struct sockaddr*)&addr, sizeof(addr));
DEBUG&& debug("xbind", "binding socket to port");
/* (2) Manage error */
if( bound == -1 ){
@ -70,20 +70,7 @@ int xbind(const int port){
DEBUG&& printf("done\n");
/* [4] Mark socket so it will listen for incoming co.
=========================================================*/
DEBUG&& debug("xbind", "listen for client");
if( listen(xsocket, maxClients) < 0 ){
DEBUG&& printf("error\n");
return -1;
}
DEBUG&& printf("done\n");
/* [5] Return xsocket
/* [4] Return xsocket
=========================================================*/
return xsocket;
}
@ -93,52 +80,19 @@ int xbind(const int port){
int xlisten(const int xsocket, struct sockaddr_in* client){
/* [1] Initialization
=========================================================*/
unsigned int sock_len;
int clientsock;
/* [2] Wait for client
=========================================================*/
/* (1) Wait for incoming client connection */
DEBUG&& debug("xlisten", "listening for client");
clientsock = accept(xsocket, (struct sockaddr*) client, &sock_len);
/* (2) Manage errors */
if( clientsock == -1 ){
DEBUG&& printf("error\n");
return -1;
}
DEBUG&& printf("done\n");
return clientsock;
}
int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
/* [0] Initialization
=========================================================*/
int xsocket, bound, connected;
int xsocket, bound;
struct hostent *host; // data found by lookup
/* [1] Create xsocket
=========================================================*/
/* (1) Create UPD xsocket */
xsocket = socket(AF_INET, SOCK_DGRAM, 0);
DEBUG&& debug("xconnect", "creating client socket");
xsocket = socket(AF_INET, SOCK_STREAM, 0);
/* (r2-) Manage error */
if( xsocket == -1 ){
@ -152,8 +106,8 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
/* [2] Get information by hostname
=========================================================*/
/* (1) Process */
DEBUG&& debug("xconnect", "fetch info by hostname");
host = gethostbyname(hostname);
DEBUG&& debug("xconnect", "fetch info by hostname");
/* (2) Manage error */
if( host == NULL ){
@ -168,8 +122,8 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
/* [3] Set server useful information from fetched info
=========================================================*/
/* (1) Reset values */
DEBUG&& debug("xconnect", "building server info");
bzero(serv, sizeof(struct sockaddr_in));
DEBUG&& debug("xconnect", "building server info");
/* (2) Set server info (ipv4, port) */
serv->sin_family = AF_INET;
@ -181,22 +135,6 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
DEBUG&& printf("built\n");
/* [4] Connect to server
=========================================================*/
/* (1) Try to connect to given server */
DEBUG&& debug("xconnect", "connecting to server");
connected = connect(xsocket, (struct sockaddr*) serv, sizeof(*serv));
/* (2) Manage error */
if( connected == -1 ){
DEBUG&& printf("error\n");
return -1;
}
DEBUG&& printf("done\n");
// return socket
return xsocket;
}
@ -208,16 +146,17 @@ int xconnect(const char* hostname, const int port, struct sockaddr_in* serv){
int xread(const int xsocket, char* buffer, int bufsize){
int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize){
/* [1] Wait for message through xsocket
=========================================================*/
/* (1) Memory allocation */
/* (1) Useful data + memory allocation */
socklen_t sock_len = sizeof(struct sockaddr_in);
buffer = (char*) realloc(buffer, sizeof(char) * bufsize );
/* (2) Listen */
DEBUG&& debug("xread", "waiting for data");
int read = recv(xsocket, buffer, bufsize, 0);
DEBUG&& debug("xlisten", "waiting for data");
int read = recvfrom(xsocket, buffer, bufsize / sizeof(char) + sizeof(char), 0, (struct sockaddr*) client, &sock_len);
/* (3) Manage error */
if( read == -1 ){
@ -227,7 +166,7 @@ int xread(const int xsocket, char* buffer, int bufsize){
DEBUG&& printf("received\n");
DEBUG&& printf("[xread:received] '%s'\n", buffer);
printf("[xlisten:received] '%s'\n", buffer);
/* [2] Return number of read characters
@ -240,16 +179,16 @@ int xread(const int xsocket, char* buffer, int bufsize){
int xwrite(const int xsocket, char* buffer){
int xsend(const int xsocket, char* buffer, struct sockaddr_in* target){
/* [1] Send buffer (message) to target
=========================================================*/
/* (1) Useful data */
unsigned int addr_len = sizeof(struct sockaddr_in);
size_t addr_len = sizeof(struct sockaddr_in);
/* (2) Send data */
DEBUG&& debug("xwrite", "sending data");
int sent = send(xsocket, buffer, strlen(buffer), 0);
DEBUG&& debug("xsend", "sending data");
int sent = sendto(xsocket, buffer, strlen(buffer)+1, 0, (struct sockaddr*) target, addr_len);
/* (3) Manage error */
if( sent == -1 ){
@ -260,7 +199,7 @@ int xwrite(const int xsocket, char* buffer){
DEBUG&& printf("sent\n");
DEBUG&& printf("[xwrite:sent] '%s'\n", buffer);
printf("[xsend:sent] '%s'\n", buffer);
/* [2] Return number of sent characters

29
lib.h
View File

@ -10,9 +10,6 @@
#define DEBUG 0x1
#define DEBUG_LEN 40
// constants
#define maxClients 0x4
#include <stdio.h>
#include <stdlib.h>
@ -34,29 +31,7 @@
int debug(const char* tag, const char* msg);
int xbind(const int port);
int xlisten(const int xsocket, struct sockaddr_in* client);
int xconnect(const char* hostname, const int port, struct sockaddr_in* serv);
int xread(const int xsocket, char* buffer, int bufsize);
int xwrite(const int xsocket, char* buffer);
/************************************************
**** Structs ****
************************************************/
typedef enum
{
FIN=0,
FACTO=1,
POW=2,
ANALYSE=3
}requete_t;
typedef struct {
requete_t type;
int taille;
}requete;
int xlisten(const int xsocket, struct sockaddr_in *client, char* buffer, int bufsize);
int xsend(const int xsocket, char* buffer, struct sockaddr_in* target);
#endif

BIN
lib.o

Binary file not shown.

View File

@ -1,7 +1,5 @@
CC=-Werror -g -pthread
CC=-Werror -g
calculs.o: calculs.c calculs.h
gcc $(CC) -c -o calculs.o calculs.c
client.o: client.h client.c
gcc $(CC) -c -o client.o client.c
@ -12,18 +10,12 @@ server.o: lib.h server.h server.c
lib.o: lib.h lib.c
gcc $(CC) -c -o lib.o lib.c
server: lib.o server.o serverRPC.o calculs.o
gcc $(CC) -o server lib.o server.o serverRPC.o calculs.o
server: lib.o server.o
gcc $(CC) -o server lib.o server.o
client: lib.o client.o
gcc $(CC) -o client lib.o client.o
clientRPC.o: calculs.h RPC/client.c
gcc $(CC) -c -o clientRPC.o RPC/client.c
serverRPC.o: RPC/server.c RPC/server.h
gcc $(CC) -c -o serverRPC.o RPC/server.c

BIN
server Executable file

Binary file not shown.

157
server.c
View File

@ -6,17 +6,19 @@ int main(int argc, char* argv[]){
/* [1] Initialisation
=========================================================*/
/* (1) Client address + port */
/* (1) Socket info */
// Client
struct sockaddr_in addr_client;
int port;
/* (2) Sockets */
int listensock, datasock;
/* (3) Struct to send arguments to threadable process */
pthread_arg_wrapper built_args = {0, 0};
char client_ip[20];
int port; // chosen port
// Socket
int sock;
/* (2) Misc. information */
char* to_send = (char*) malloc( BUFSIZE * sizeof(char) );
char* to_recv = (char*) malloc( BUFSIZE * sizeof(char) );
int bytes; // transfer count
/* [2] Manage arguments
@ -35,123 +37,64 @@ int main(int argc, char* argv[]){
return 1;
}
/* (3) Manage optional @message argument */
if( argc >= 3 ){
if( strlen(argv[2]) > BUFSIZE || sscanf(argv[2], "%s", to_send) <= 0 ){
printf("argument error: message must be a string (max: %d characters long)\n", BUFSIZE);
return 1;
}
}else
strcpy(to_send, "server default message");
/* [3] Create socket
=========================================================*/
/* (1) Create socket */
listensock = xbind(port);
sock = xbind(port);
/* (1-) Manage error */
if( listensock == -1 ){
if( sock == -1 ){
perror("erreur création socket");
exit(1);
}
/* [4] Wait for clients indefinately
/* [4] Wait for client message
=========================================================*/
int i, index = -1; // thread index
while( 1 ){
/* (1) Listen for incoming connection */
index = -1;
datasock = xlisten(listensock, &addr_client);
DEBUG&& debug("server", "wait for client");
/* (2) Manage error */
if( datasock == -1 ){
DEBUG&& printf("error\n");
continue;
}
DEBUG&& printf("done\n");
/* (3) Search for a free manager */
DEBUG&& debug("server", "finding free thread");
for( i = 0 ; i < maxClients ; i++ )
if( a_managers[i] == 0 ){
DEBUG&& printf("found[%d]\n", i);
index = i;
break;
}
/* (4) If we haven't found a free manager -> do nothing (john snow) */
if( index == -1 ){
DEBUG&& printf("not found\n");
continue;
}
/* (5) Build thread arguments */
built_args.socket = datasock;
built_args.index = index;
/* (6) Processing client connection on a new thread */
pthread_create(&managers[index], NULL, manageClient, (void*)(intptr_t) &built_args);
/* (7) Mark the manager as active */
a_managers[index] = 1;
/* (1) Listen to data */
bytes = xlisten(sock, &addr_client, to_recv, BUFSIZE);
/* (2) Manage error */
if( bytes == -1 ){
perror("erreur réception paquet");
exit(1);
}
/* [5] Wait for all threads to end
=========================================================*/
/* (1) Wait for all threads to end properly */
for( i = 0 ; i < maxClients ; i++ )
pthread_join(managers[i], NULL);
// Get client ip
inet_ntop(AF_INET, &(addr_client.sin_addr), client_ip, 20);
/* (2) Close listening socket */
close(listensock);
printf("*** received '%s' (%d bytes) from %s:%d\n", to_recv, bytes, client_ip, ntohs(addr_client.sin_port));
/* [5] Send response
=========================================================*/
/* (1) Send response */
bytes = xsend(sock, to_send, &addr_client);
/* (2) Manage error */
if( bytes == -1 ){
perror("erreur envoi réponse");
exit(1);
}
/* [6] Close socket
=========================================================*/
close(sock);
return 0;
}
void* manageClient(void* pthread_args){
/* [1] Initialization
=========================================================*/
/* (1) Initialize socket data */
struct sockaddr_in addr_client;
int bytes; // transfer count
char client_ip[20];
/* (2) Fetch arguments */
pthread_arg_wrapper* arguments = (pthread_arg_wrapper*) (intptr_t) pthread_args;
// get client_socket
int datasock = arguments->socket;
int thread_index = arguments->index;
DEBUG&& printf("starting the computation loop\n");
//Compute client request
traiterClient(datasock);
/* [2] End process
=========================================================*/
/* (1) Close data socket */
DEBUG&& debug("thread", "closing socket");
close(datasock);
DEBUG&& printf("done\n");
/* (2) Mark manager as inactive again */
DEBUG&& debug("thread", "freeing thread");
a_managers[thread_index] = 0;
DEBUG&& printf("done\n");
}

View File

@ -14,26 +14,9 @@
#include <sys/socket.h>
#include <netinet/in.h>
#include <errno.h>
#include <pthread.h>
#include "RPC/server.h"
#include "lib.h"
// DEFINITIONS
typedef struct{
int socket;
int index;
} pthread_arg_wrapper;
// VARIABLES
static pthread_t managers[maxClients]; // containa THREADS
static int a_managers[maxClients] = {0}; // contains THREADS actifs
/************************************************
**** Signatures ****
************************************************/
void* manageClient(void* pthread_args);
#endif

BIN
server.o

Binary file not shown.