Archived
3
0

à revoir beaucoup beacoup beacoup

This commit is contained in:
kanguyen 2016-11-04 16:51:17 +01:00
parent 5db548c78e
commit 7678a8ab7a
6 changed files with 117 additions and 31 deletions

View File

@ -4,7 +4,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cbor.h>
//#include <cbor.h>
#include "process.h"
#include <unistd.h> // unlink

View File

@ -6,11 +6,12 @@ EXEC=$(basename $(wildcard *.c))
SOURCES=$(wildcard ../lib/communication.c ../lib/process.c)
OBJECTS=$(SOURCES:.c=.o)
TESTS=$(addsuffix .test, $(EXEC))
LCBOR=-lbor
all: $(SOURCES) $(EXEC)
$(EXEC): $(OBJECTS) $(CFILES)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin
.c.o:
$(CC) -c $(CFLAGS) $< -o $@

View File

@ -48,7 +48,7 @@ void * pongd_thread(void * pdata) {
if (nbytes == 0 || strncmp ("exit", buf, 4) == 0){
printf("------thread shutdown------------\n");
//close(cfd);
//close(sfd);
close(*sockclient);
free(buf);
break;
}else {
@ -147,21 +147,52 @@ void main_loop (struct service *srv)
else
{
printf("Server-accept() is OK...\n");
FD_SET(newfd, &master); /* add to master set */
if(newfd > fdmax)
{ /* keep track of the maximum */
fdmax = newfd;
}
}
} else {
//FD_SET(newfd, &master); /* add to master set */
//if(newfd > fdmax)
//{ /* keep track of the maximum */
//fdmax = newfd;
//}
nbytes = file_read (newfd, &buf);
if ( nbytes == -1) {
handle_error("file_read");
} else {
buf[BUFSIZ - 1] = '\0';
printf ("msg received (%d) : %s\n", nbytes, buf);
if (strncmp ("exit", buf, 4) == 0) {
break;
}
// -1 : error, 0 = no new process, 1 = new process
ret = srv_get_new_process (buf, &tab_proc[cnt]);
if (ret == -1) {
fprintf (stderr, "MAIN_LOOP: error service_get_new_process\n");
continue;
}
srv_process_print (&tab_proc[cnt]);
int ret = pthread_create( &tab_thread[cnt], NULL, &pongd_thread, (void *) &newfd);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf ("\n-------New thread created---------\n");
}
printf ("%d applications to serve\n",cnt);
cnt++;
}
}
/*else {
nbytes = file_read (i, &buf);
if ( nbytes == -1) {
handle_error("file_read");
} else if( nbytes == 0) {
} else if( nbytes == 0) {*/
/* close it... */
close(i);
//close(i);
/* remove from master set */
FD_CLR(i, &master);
/*FD_CLR(i, &master);
}else {
buf[BUFSIZ - 1] = '\0';
printf ("msg received (%d) : %s\n", nbytes, buf);
@ -189,7 +220,7 @@ void main_loop (struct service *srv)
printf ("%d applications to serve\n",cnt);
cnt++;
}
}*/
}
}
@ -207,6 +238,7 @@ void main_loop (struct service *srv)
close(sfd);
}
/*
* service ping-pong
*

View File

@ -6,11 +6,12 @@ EXEC=$(basename $(wildcard *.c))
SOURCES=$(wildcard ../lib/communication.c ../lib/process.c)
OBJECTS=$(SOURCES:.c=.o)
TESTS=$(addsuffix .test, $(EXEC))
LCBOR=-lbor
all: $(SOURCES) $(EXEC)
$(EXEC): $(OBJECTS) $(CFILES)
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -lcbor -o $@.bin
$(CC) $(CFLAGS) $(LDFLAGS) $(OBJECTS) $@.c -o $@.bin
.c.o:
$(CC) -c $(CFLAGS) $< -o $@

View File

@ -511,7 +511,7 @@ void * client_thread(void *reqq) {
/*else*/ if (FD_ISSET(sock, &read_fds)) {
int n = read_message(sock, buffer);
if(n > 0) {
printf("Client : message (%d bytes) : %s\n", n, buffer);
printf("Client : message from server(%d bytes) : %s\n", n, buffer);
if(app_write(req->p, buffer, strlen(buffer)) < 0) {
perror("file_write");
}
@ -526,7 +526,7 @@ void * client_thread(void *reqq) {
}
}else {
nbytes = app_read (req->p, &buffer);
printf("Client : size message %d : %s\n",nbytes, buffer );
printf("Client : message from app %d : %s\n",nbytes, buffer );
if ( nbytes == -1) {
handle_error("file_read");
} else if( nbytes == 0) {
@ -640,25 +640,75 @@ void main_loop (struct service *srv) {
else
{
printf("Server-accept() is OK...\n");
FD_SET(newfd, &master); /* add to master set */
if(newfd > fdmax)
{ /* keep track of the maximum */
fdmax = newfd;
}
//FD_SET(newfd, &master); /* add to master set */
//if(newfd > fdmax)
//{ /* keep track of the maximum */
//fdmax = newfd;
//}
nbytes = file_read (newfd, &buf);
if ( nbytes == -1) {
handle_error("file_read");
} else if( nbytes == 0) {
/* close it... */
close(i);
/* remove from master set */
FD_CLR(i, &master);
}else {
buf[BUFSIZ - 1] = '\0';
printf ("msg received (%d) : %s\n", nbytes, buf);
if (strncmp ("exit", buf, 4) == 0) {
break;
}
tab_req[nbclient].p = malloc(sizeof(struct process));
// -1 : error, 0 = no new process, 1 = new process
ret = srv_get_new_request (buf, &tab_req[nbclient]);
tab_req[nbclient].p->proc_fd = newfd;
if (ret == -1) {
perror("srv_get_new_request()");
exit(1);
} else if (ret == 0) {
break;
}
request_print(&tab_req[nbclient]);
if (strcmp("listen", tab_req[nbclient].request) == 0) {
int ret = pthread_create( &pid_s, NULL, &server_thread, (void *) &tab_req[nbclient]);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf("\n----------Creation of server thread ------------\n");
}
nbclient++;
}else {
int ret = pthread_create( &tab_client[nbclient], NULL, &client_thread, (void *) &tab_req[nbclient]);
if (ret) {
perror("pthread_create()");
exit(errno);
} else {
printf("\n----------Creation of client thread ------------\n");
}
nbclient++;
}
}
}
} else {
} /*else {
nbytes = file_read (i, &buf);
if ( nbytes == -1) {
handle_error("file_read");
} else if( nbytes == 0) {
} else if( nbytes == 0) {*/
/* close it... */
close(i);
//close(i);
/* remove from master set */
FD_CLR(i, &master);
/*FD_CLR(i, &master);
}else {
buf[BUFSIZ - 1] = '\0';
printf ("msg received (%d) : %s\n", nbytes, buf);
if (strncmp ("exit", buf, 4) == 0) {
if (strncmp ("exit_server", buf, 4) == 0) {
break;
}
@ -696,7 +746,7 @@ void main_loop (struct service *srv) {
}
}
} //i == listener
}*/ //i == listener
} //if FDISSET
}//boucle for
if (strncmp ("exit", buf, 4) == 0) {

View File

@ -37,12 +37,14 @@ int main(int argc, char * argv[]) {
perror("connect()");
exit(errno);
}
//printf("connected...\n");
file_write(sfd, srv_message, strlen(srv_message));
file_write(sfd, proc_message, strlen(srv_message));
//printf("%s\n", proc_message);
//sleep(1);
file_write(sfd, proc_message, strlen(proc_message));
file_read(sfd, &buffer);
printf("%s\n", buffer);
//sleep(1);
file_write(sfd, "exit", 4);
close(sfd);