Bonjour,

Voila, suite de mes aventures avec mon serveur/client

Le programme se connecte a un serveur (socket A)
deux clients se connectent au programme (socket B)


Le probleme est que quand je veux envoyer une donnée depuis le socket A vers le socket B, le programme "bloque" les données (recu via A)à son niveau et les envoye vers B seulement quand celui ci envoye quelque choses.

Ci-joint le programme.
Pour information, la classe parser obtient les données concernant les sockets (ip & port) via un fichier xml.

Merci de votre aide.
F.

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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#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 int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
 
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
 
#include "parser.h"
 
#define bool char;
static void remove_client(SOCKET* hosts, int to_rem, int *now);
 
static int init_client(const char *address, int port)
{
   SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
   SOCKADDR_IN sin = { 0 };
   struct hostent *hostinfo;
   if(sock == INVALID_SOCKET)
   {
      perror("socket()");
	close(sock);
      return 0;
   }
   hostinfo = gethostbyname(address);
   if (hostinfo == NULL)
   {
      	fprintf (stderr, "Unknown host %s.\n", address);
	close(sock);
	return 0;
   }
   sin.sin_addr = *(IN_ADDR *) hostinfo->h_addr;
   sin.sin_port = htons(port);
   sin.sin_family = AF_INET;
   if(connect(sock,(SOCKADDR *) &sin, sizeof(SOCKADDR)) == SOCKET_ERROR)
   {
	close(sock);
      perror("connect()");
      return 0;
   }
   return sock;
}
 
 
static int init_serveurs(int port)
{
   SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
   SOCKADDR_IN sin = { 0 };
   if(sock == INVALID_SOCKET)
   {
      perror("socket()");
   }
   sin.sin_addr.s_addr = htonl(INADDR_ANY);
   sin.sin_port = htons(port);
   sin.sin_family = AF_INET;
   if(bind(sock,(SOCKADDR *) &sin, sizeof sin) == SOCKET_ERROR)
   {
      perror("bind()");
      exit(errno);
   }
   if(listen(sock, 2) == SOCKET_ERROR)
   {
      perror("listen()");
      exit(errno);
   }
   return sock;
}
 
 
static int read_client(SOCKET sock, char *buffer)
{
  int n = 0;
  if((n = recv(sock, buffer, 100, 0)) < 0)
  {
     perror("recv()");
     /* if recv error we disonnect the client */
     n = 0;
  }
  buffer[n] = 0;
  return n;
}
 
static void send_to(SOCKET* hosts, int nb, char* buf, int c)
{
	if (nb == 0)
		return;
 
	if (nb == 1)
	{
		send(hosts[0], buf, c, 0);
		return;
	}
 
	if (nb == 2)
	{
		send(hosts[0], buf, c, 0);
		send(hosts[1], buf, c, 0);
	}
}
 
 
int main(int argc, char **argv)
{
SOCKET client;
SOCKET serveur;
SOCKET hosts[2];
int nbconnected = 0;
char buffer[200];
int max;
fd_set rdfs;
 
int port_ser;
int port_cli;
 
 
 
 
if(argc != 2)
{
	fprintf(stdout, "Bad number of args (xml file to parse)\n");
 
exit(0);
}
 
Parser p(argv[1]);
 
 
 
if(p.Get_port_serveur() < 1  || p.Get_port_client() < 1)
{
	fprintf(stderr,"Bad port number, server: %i, server: %i\n", p.Get_port_serveur() , p.Get_port_client());
	exit(0);
}
fprintf(stderr, "%i, %i\n" , p.Get_port_serveur() , p.Get_port_client());
 
 
while( (client = init_client(p.Get_host_serveur().c_str(),p.Get_port_serveur())) == 0)sleep(5);
serveur = init_serveurs(p.Get_port_client());
 
 
max = serveur>client?serveur:client;
 
 
while(1)
{
	int i= 0;
	FD_ZERO(&rdfs);
printf("top");
	FD_SET(serveur, &rdfs);
	FD_SET(client,  &rdfs);
 
	if(nbconnected == 1)FD_SET(hosts[0], &rdfs);
	if(nbconnected == 2)
	{
		FD_SET(hosts[0], &rdfs);
		FD_SET(hosts[1], &rdfs);
	}
 
	if(select(max+1, &rdfs, NULL, NULL,NULL) == -1)
	{
		perror("select()");
	}
 
 	if(FD_ISSET(serveur, &rdfs))
	{
		SOCKADDR_IN csin = {0};
		size_t sinsize = sizeof csin;
 
		SOCKET csock = accept(serveur, (SOCKADDR*) &csin, &sinsize);
 
		if(csock == SOCKET_ERROR)
		{
			perror("accept()");
			continue;
		}
 
		max = csock > max ? csock:max;
		FD_SET(csock, &rdfs);
 
		hosts[nbconnected] = csock;
		nbconnected ++;
	}
 
	if(FD_ISSET(client, &rdfs))
	{
		int c = read_client(client, buffer);
 
		if(c == 0)
		{
			closesocket(client);
 
			client = 0;
 
			while(client == 0)
			{
				client = init_client(p.Get_host_serveur().c_str(), p.Get_port_serveur() );
				sleep(5);
			}
		}
 
		send_to(hosts,nbconnected,buffer, c);
 
	}
 
	for(i=0; i < nbconnected; i++)
	{
		if( FD_ISSET(hosts[i], &rdfs) )
		{
			int c = read_client(hosts[i], buffer);
 
			if (c == 0)
			{
				closesocket(hosts[i]);
				remove_client(hosts, i, &nbconnected);
			}
 
			send_to(&client, 1, buffer, c);
		}
	}
}
}
 
static void remove_client(SOCKET* hosts, int to_rem, int *now)
{
	if(to_rem ==1)
	{
		(*now)--;
	}
 
	if(to_rem == 0)
	{
		hosts[0]=hosts[1];
		(*now)--;
	}
return;
}