bonjour à tous,

j'ai commencé les sockets en lisant le tutoriel de Benjamin Roux http://broux.developpez.com/articles/c/sockets/

J'ai adapté son code afin de tester le nombre maximum de bit que je pouvais envoyer. Selon la documentation je pourrais en envoyer 64KB sans problème mais dans les faits j'ai du mal à envoyer ne serait-ce que 32KB. (mais pour envoyer des messages petits ça marche nickel).

J'ai bien vérifié qu'un char occupe 8 bits sur mon système d'exploitation (snow leopard).

voici mon code (j'ai volontairement désactivé le chat et le serveur est identique à celui du tutorial)

main.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
 
#include "client.h"
 
void clean(char *chain){
	int *p= NULL;
	p=strchr(chain, '\n'); // on cherche la caractère 'entrée'
	if(p != NULL)// si il existe
		*p=0;//on le supprime
	else
		purge();// on vide le buffer
}
 
void purge(){
	long c;
	while( (c=getchar())!='\n' && c!=EOF){
	} //fonction qui permet de vider le buffer en cas de dépassement mémoire
 
}
 
void client(char *address, char *pseudo)
{
	SOCKADDR_IN sin = { 0 };
	int sock = initialisation(address, &sin);
	char buffer[BUFFER];
 
	fd_set rdfs;
 
	sending(sock, &sin, pseudo);
	testMode(sock, &sin);
 
//	chat app
	int running = 0;
	while(running)
	{
		FD_ZERO(&rdfs);
 
		//add keyboard as entry
		FD_SET(STDIN_FILENO, &rdfs);
 
		//adding the socket
 
		FD_SET(sock, &rdfs);
 
		if (select(sock + 1, &rdfs, NULL, NULL, NULL) == -1)
		{
			perror("function select()");
			exit(errno);
		}
		if(FD_ISSET(STDIN_FILENO, &rdfs))
		{
			fgets(buffer, BUFFER - 1, stdin);
			{
				clean(buffer);
				if(strcmp(buffer, "exit") == 0)
					running = 0;
 
			}
			sending(sock, &sin, buffer);
		}
		else if (FD_ISSET(sock, &rdfs))
		{
			int n = reading(sock, &sin, buffer);
			if (n == 0)
			{
				printf("server disconnected");
				running = 0;
			}
			puts(buffer);
		}
	}
 
	closesocket(sock);
}
 
int initialisation(char *address, SOCKADDR_IN *sin)
{
	int sock = socket(AF_INET, SOCK_DGRAM, 0);
	struct hostent *hostinfo;
 
	if(sock == INVALID_SOCKET)
	{
		perror("socket()");
		exit(errno);
	}
	hostinfo = gethostbyname(address);
	if (hostinfo == NULL)
	{
		fprintf (stderr, "Unknown host %s.\n", address);
		exit(EXIT_FAILURE);
	}
 
	sin->sin_addr = *(IN_ADDR *) hostinfo->h_addr;
	sin->sin_port = htons(PORT);
	sin->sin_family = AF_INET;
 
	return sock;
}
 
 
int reading(int sock, SOCKADDR_IN *sin, char *buffer)
{
	int n = 0;
	size_t sinsize = sizeof *sin;
	if((n = recvfrom(sock, buffer, BUFFER - 1, 0, (SOCKADDR *) sin, &sinsize)) < 0)
	{
		perror("function recvfrom()");
		exit(errno);
	}
 
	buffer[n] = 0;
 
	return n;
 
 
}
 
void sending(int sock, SOCKADDR_IN *sin, char *buffer)
{
	if(sendto(sock, buffer, strlen(buffer), 0, (SOCKADDR *) sin, sizeof *sin) < 0)
	{
		perror("function sendto()");
		exit(errno);
	}
}
 
 
void testMode(int sock, SOCKADDR_IN *sin)
{
	time_t start,end;
	double dif;
	time (&start);
 
 
	char *message;
	message = malloc(31740 * sizeof(char));
	for(int i = 0; i < 31740 ; i ++)
		message[i] = 'a';
 
 
	for(int j = 0 ; j < 1000 ; j ++)
	{
 
 
		if(sendto(sock, message, strlen(message), 0, (SOCKADDR *) sin, sizeof *sin) < 0)
		{
			perror("function sendto()");
			exit(errno);
		}
		printf("%d\n", (j + 1));
 
	}
	time (&end);
	dif = difftime (end,start);
	printf ("\nIt took you %.7lf seconds\n", dif ); 
}
 
 
int main(int argc, char **argv)
{
 
 
	if(argc < 2)
	{
		printf("Usage : %s [address] [pseudo]\n", argv[0]);
		return EXIT_FAILURE;
	}
	for(int i = 0; (i + 1) < strlen(argv[1]) ; i ++)
	{
		argv[1][i] = argv[1][i+1];
	}
	argv[1][strlen(argv[1]) - 2] = '\0';
 
 
	client(argv[1], argv[2]);
 
 
	return EXIT_SUCCESS;
}
client.h
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#ifndef DEFINECLIENT
#define DEFINECLIENT
 
 
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> /* close */
#include <netdb.h> /* gethostbyname */
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
 
 
 
#define CRLF	 "\r\n"
#define PORT	 1977
 
#define BUFFER 1024
void clean(char *chain);
void purge();
void testMode(int sock, SOCKADDR_IN *sin);
void client(char *address, char *name);
int initialisation(char *address, SOCKADDR_IN *sin);
int reading(int sock, SOCKADDR_IN *sin, char *buffer);
void sending(int sock, SOCKADDR_IN *sin, char *buffer);
 
#endif /* guard */
Quelqu'un pourrait me dire d'où vient l'erreur?