[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [Un peu HS] Truc bizarre avec des sockets Linux



	Je viens de reproduire la chose avec deux bouts de programmes écrits en
C (voir les deux pièces jointes).

hilbert:[~/rpl-test] > ./server
[+]Server Socket is created.
[+]Bind to port 4444
[+]Listening....
accept(3, 4)
Connection accepted from 127.0.0.1:43388
Client: aze
Disconnected from 127.0.0.1:43388
shutdown: 0
close: 0
accept(3, 6)
Connection accepted from 127.0.0.1:49504
Client: aze
Disconnected from 127.0.0.1:49504
shutdown: 0
close: 0

hilbert:[~/rpl-test] > ./client
[+]Client Socket is created.
[+]Connected to Server.
Client:         aze
Server:         aze
Client:         :exit
[-]Disconnected from server.
hilbert:[~/rpl-test] > ./client
[+]Client Socket is created.
[+]Connected to Server.
Client:         aze
Server:         aze
Client:         :exit
[-]Disconnected from server.

	Un coup de valgrind montre exactement la même chose lorsque le
processus serveur racine est tué par un SIGINT :

==10415== Process terminating with default action of signal 2 (SIGINT)
==10415==    at 0x49A04D0: accept (accept.c:26)
==10415==    by 0x109233: main (tcpServer.c:52)
==10415==
==10415== FILE DESCRIPTORS: 7 open (3 std) at exit.
==10415== Open AF_INET socket 6: 127.0.0.1:4444 <-> 127.0.0.1:57256
==10415==    at 0x49A04D0: accept (accept.c:26)
==10415==    by 0x109233: main (tcpServer.c:52)
==10415==
==10415== Open AF_INET socket 4: 127.0.0.1:4444 <-> 127.0.0.1:46402
==10415==    at 0x49A04D0: accept (accept.c:26)
==10415==    by 0x109233: main (tcpServer.c:52)
==10415==
==10415== Open AF_INET socket 3: 127.0.0.1:4444 <-> unbound
==10415==    at 0x49A0BA7: socket (syscall-template.S:120)
==10415==    by 0x109141: main (tcpServer.c:25)
==10415==
==10415== Open file descriptor 5:
==10415==    <inherited from parent>

Les sockets créées par accept() [ici fd=4 et fd=6] restent ouvertes dans
le processus racine du serveur et au bout d'un certain nombre de
connexions (dépendant de la valeur max open files), accept() retourne
une erreur.

	À noter : la socket est bien fermée dans le processus créé par fork()
[shutdown + close] et est fermée dans le client.

	Merci de vos lumières,

	JB
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 4444

int main(){

	int clientSocket, ret;
	struct sockaddr_in serverAddr;
	char buffer[1024];

	clientSocket = socket(AF_INET, SOCK_STREAM, 0);
	if(clientSocket < 0){
		printf("[-]Error in connection.\n");
		exit(1);
	}
	printf("[+]Client Socket is created.\n");

	memset(&serverAddr, '\0', sizeof(serverAddr));
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(PORT);
	serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");

	ret = connect(clientSocket, (struct sockaddr*)&serverAddr, sizeof(serverAddr));
	if(ret < 0){
		printf("[-]Error in connection.\n");
		exit(1);
	}
	printf("[+]Connected to Server.\n");

	while(1){
		printf("Client: \t");
		scanf("%s", &buffer[0]);
		send(clientSocket, buffer, strlen(buffer), 0);

		if(strcmp(buffer, ":exit") == 0){
			printf("shutdown: %d\n", shutdown(clientSocket, SHUT_RDWR));
			printf("close: %d\n", close(clientSocket));
			printf("[-]Disconnected from server.\n");
			exit(1);
		}

		if(recv(clientSocket, buffer, 1024, 0) < 0){
			printf("[-]Error in receiving data.\n");
		}else{
			printf("Server: \t%s\n", buffer);
		}
	}

	return 0;
}

Attachment: signature.asc
Description: OpenPGP digital signature


Reply to: