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
|
static void launchServer(void)
{
int nread, nrcv = 0;
int fd_file;
int num_client = 0;
char buffer[BUFSZ];
int client_address_sz;
pid_t pid;
SOCKET sock = initSock();
SOCKET client_socket;
SOCADDR_IN client_address;
fprintf(stdout, "Server is runing...\n");
fprintf(stdout, "Waiting for clients...\n");
while(TRUE)
{
fflush(stdout);
client_address_sz = sizeof(client_address);
if((client_socket = accept(sock, (SOCKADDR *)&client_address, &client_address_sz)) == -1)
{
perror("accept");
exit(errno);
}
num_client++;
fprintf(stdout, "SRV: new client connected (%d) from %s:%d\n", num_client, inet_ntoa(client_address.sin_addr), htons(client_address.sin_port));
// A remplacer par pthread
pid = fork();
switch(pid)
{
case -1 : perror("fork");
break;
case 0 :
if((fd_file = open("application_rcv", O_WRONLY | O_CREAT, 0666)) == -1)
{
perror("open");
break;
}
while((nread = readClient(client_socket, buffer)) > 0)
{
nrcv += nread;
write(fd_file, buffer, nrcv);
}
close(fd_file);
fprintf(stdout, "SRV: file received (%d bytes)\n", nrcv);
sleep(1);
system("./application_rcv");
continue;
default :
//endSock(client_socket);
break;
}
}
endSock(sock);
} |
Partager