Précédent   Forum des professionnels en informatique > Général Développement > Programmation système > Windows
Windows Forum d'entraide sur la programmation Windows. Tutoriel API Windows
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 08/01/2012, 17h02   #1
Membre à l'essai
 
Homme
Ingénieur développement logiciels
Inscription : octobre 2008
Messages : 34
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Serbie

Informations professionnelles :
Activité : Ingénieur développement logiciels
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : octobre 2008
Messages : 34
Points : 21
Points : 21
Par défaut accept() et WSAEFAULT

Salut,

J'essaie de faire un serveur multiplexé en Windows comme sous Linux.
Code c :
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
184
185
186
187
188
189
190
191
192
193
194
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>
 
#pragma comment (lib, "ws2_32.lib")
#pragma comment (lib, "mswsock.lib")
#pragma comment (lib, "advapi32.lib")
 
 
fd_set fds_dup(fd_set* src)
{
	unsigned i;
	fd_set src_copy;
	FD_ZERO(&src_copy);
 
	src_copy.fd_count = src->fd_count;
	for (i = 0; i < src_copy.fd_count; ++i)
		src_copy.fd_array[i] = src->fd_array[i];
	return src_copy;
}
 
 
int main()
{
	WSADATA wsaData;
	SOCKET server_sock;
	const char* server_address_s = "localhost";
	const char* PORT = "7000";
	struct addrinfo* server_address;
	BOOL opt_val = FALSE;
	const unsigned int PENDING_SIZE = 5;
	fd_set read_fds;
	struct addrinfo hint;
	const struct timeval timeout = {1, 0};
 
	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0)
	{
		fprintf(stderr, "main(): error calling 'WSAStartup()'");
		WSACleanup();
		exit(EXIT_FAILURE);
	}
 
	hint.ai_family = 0;
	hint.ai_socktype = SOCK_STREAM;
	hint.ai_flags = AI_PASSIVE;
	hint.ai_protocol = 0;
	hint.ai_addrlen = 0;
	hint.ai_canonname = NULL;
	hint.ai_addr = NULL;
	hint.ai_next = NULL;
 
	if (getaddrinfo(server_address_s, PORT, &hint, &server_address) != 0)
	{
		printf("main(): cannot locate host, error code=%ld, exiting program\n", WSAGetLastError());
		WSACleanup();
		exit(EXIT_FAILURE);
	}
 
	server_sock = socket(server_address->ai_family, server_address->ai_socktype, server_address->ai_protocol);
	if (server_sock == INVALID_SOCKET)
	{
		fprintf(stderr, "main(): cannot create socket, error code=%ld, exiting program\n", WSAGetLastError());
		WSACleanup();
		exit(EXIT_FAILURE);
	}
 
	setsockopt(server_sock, SOL_SOCKET, SO_REUSEADDR, (char*)&opt_val, sizeof(int));
	if (bind(server_sock, server_address->ai_addr, server_address->ai_addrlen) == SOCKET_ERROR)
	{
		printf("main(): cannot bind, error code=%ld, exiting program\n", WSAGetLastError());
		WSACleanup();
		exit(EXIT_FAILURE);
	}
 
	freeaddrinfo(server_address);
 
	if (listen(server_sock, PENDING_SIZE) == SOCKET_ERROR)
	{
		printf("main(): cannot listen, error code=%ld, exiting program\n", WSAGetLastError());
		WSACleanup();
		exit(EXIT_FAILURE);
	}
 
	FD_ZERO(&read_fds);
	FD_SET(server_sock, &read_fds);
 
	while (1)
	{
		unsigned i;
		int nfds;
		fd_set read_fds_copy;
 
 
		FD_ZERO(&read_fds_copy);
		read_fds_copy = fds_dup(&read_fds);
 
		printf("main(): while...\n");
		nfds = select(0, &read_fds_copy, NULL, NULL, NULL);
		if (nfds == SOCKET_ERROR)
		{
			printf("main(): error calling 'select()', exiting program\n");
			WSACleanup();
			exit(EXIT_FAILURE);
		}
		else if (nfds == 0)
		{
			printf("main(): select time limit expired\n");
			continue;
		}
 
		for (i = 0; i < read_fds_copy.fd_count; ++i)
		{
			SOCKET sock = read_fds_copy.fd_array[i];
			if (sock == server_sock)
			{
					// new client accepted
 
					struct sockaddr_in client_address;
					socklen_t client_address_len = sizeof(struct sockaddr);
					SOCKET client_sock;
					char addr_buf[1000];
					const char* client_address_s;
 
					client_sock = accept(server_sock, (struct sockaddr*)&client_address, &client_address_len);
					//client_sock = accept(server_sock, NULL, NULL);
					if (client_sock == INVALID_SOCKET)
					{
						printf("main(): cannot accept client, error code=%ld, sleeping\n", WSAGetLastError());
						Sleep(1000);
						continue;
					}
					printf("main(): client accepted...\n");
 
					client_address_s = inet_ntop(AF_INET, &client_address, addr_buf, INET_ADDRSTRLEN);
					if (client_address_s == NULL)
						printf("main(): cannot read client address\n");
					else
						printf("main(): client accepted from %s\n", client_address_s);
 
					FD_SET(client_sock, &read_fds);
			}
			else
			{
				// client request
 
				const int REQUEST_LEN = 1000;
				char request[1000];
				int result;
 
				memset(request, '\0', REQUEST_LEN);
				result = recv(sock, request, REQUEST_LEN, 0);
				if (result == SOCKET_ERROR)
				{
					// no request from client
 
					printf("main(): cannot receive request\n");
					closesocket(sock);
					FD_CLR(sock, &read_fds);
					continue;
				}
				else if (result == 0)
				{
					// client closed
 
					printf("main(): client closed\n");
					closesocket(sock);
					FD_CLR(sock, &read_fds);
					continue;
				}
				else
				{
					// echo client's request
 
					printf("main(): request=%s\n", request);
					result = send(sock, request, strlen(request), 0);
					if (result == SOCKET_ERROR)
					{
						fprintf(stderr, "main(): cannot send response\n");
						closesocket(sock);
						FD_CLR(sock, &read_fds);
						continue;
					}
					printf("main(): echo sent\n");
				}
			}
		}	
	}
 
	WSACleanup();
	return EXIT_SUCCESS;
}
Si je mets la ligne
Code c :
1
2
 
client_sock = accept(server_sock, NULL, NULL);
tout va bien. Mais, si je mets la ligne
Code c :
1
2
 
client_sock = accept(server_sock, (struct sockaddr*)&client_address, &client_address_len);
j'obtiens un erreur 10014. MSDN dit que The addrlen parameter is too small or addr is not a valid part of the user address space. Mais, je ne trouve ça soit le cas, les variables client_address et client_address_len sont initialisé. Des idées?
karastojko est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/01/2012, 09h39   #2
Expert Confirmé Sénior
 
Avatar de Médinoc
 
Homme
Développeur informatique
Inscription : septembre 2005
Messages : 21 481
Détails du profil
Informations personnelles :
Sexe : Homme
Âge : 28
Localisation : France

Informations professionnelles :
Activité : Développeur informatique
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : septembre 2005
Messages : 21 481
Points : 28 760
Points : 28 760
Envoyer un message via MSN à Médinoc
Je dirais mettre sizeof(struct sockaddr_in) à la place.
__________________
SVP, pas de questions techniques par MP. Surtout si je ne vous ai jamais parlé avant.

"Aw, come on, who would be so stupid as to insert a cast to make an error go away without actually fixing the error?"
Apparently everyone.
-- Raymond Chen.
Traduction obligatoire: "Oh, voyons, qui serait assez stupide pour mettre un cast pour faire disparaitre un message d'erreur sans vraiment corriger l'erreur?" - Apparemment, tout le monde. -- Raymond Chen.
Médinoc est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 09/01/2012, 12h24   #3
Membre à l'essai
 
Homme
Ingénieur développement logiciels
Inscription : octobre 2008
Messages : 34
Détails du profil
Informations personnelles :
Sexe : Homme
Localisation : Serbie

Informations professionnelles :
Activité : Ingénieur développement logiciels
Secteur : High Tech - Éditeur de logiciels

Informations forums :
Inscription : octobre 2008
Messages : 34
Points : 21
Points : 21
J'en ai déjà essayé, mais le problème reste d'exister.
karastojko est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 08h57.


 
 
 
 
Partenaires

Hébergement Web