bonjour,
je suis en train de creer un serveur pour jeux, avec la notion de multithread! voir le code "fichier joint", dans mon programme le thread de serveur ecoute et accept les connexion et a chaque demande de connexion il lance des threads pour les client!!!
la compilation passe bien a lors que l'excution non plus il me donne erreur de segmentation lors un client la demande de connexion
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
 
/* gcc -o ser serveur.c -Wall -Werror -ansi -pedantic -pthread -D_REENTRANT
 
*/
 
 
 
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include<string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include "serveur.h"
#include "client.h"
#define MAX 5
typedef struct{
  SOCKET      sock;
  struct sockaddr_in data;
  pthread_t   clientThread;
} socket_t;
 
int nb_connectee;
void
tcp_send(int sock,struct sockaddr_in provenance);
void 
tcp_recev(int sock,struct sockaddr_in provenance);
int
creer_socket_stream(void);
int 
affiche_adresse_socket(int sock);
static void * 
clientManager(void * client);
 
void
tcp_send(int sock,struct sockaddr_in provenance)
	{
	/*struct sockaddr_in adresse;*/
	socklen_t longueur;
	char buffer[256];
	longueur=sizeof(struct sockaddr_in);
	fgets(buffer,255,stdin);
	sendto(sock,buffer,strlen(buffer),0,(struct sockaddr *)&provenance,longueur);
	send(sock, buffer, strlen (buffer) + 1,0);/*send*/
 
	}
void
tcp_recev(int sock,struct sockaddr_in provenance)
	{
	unsigned int longueur_out;
	char buffer_out[256];
	longueur_out=sizeof(struct sockaddr_in);
	memset(buffer_out,0,sizeof(buffer_out));
	recv(sock, buffer_out,strlen(buffer_out), 0);/*receve*/
	fprintf(stdout,"Client: %s\n",buffer_out);
 
	}
int
creer_socket_stream()
	{
	struct sockaddr_in adresse;
	int sock;
	if((sock=socket(AF_INET,SOCK_STREAM,0))<0)
		{
		perror("socket");
		return -1;
		}
 
	memset(&adresse,0,sizeof(struct sockaddr_in));
	adresse.sin_family=AF_INET;
	adresse.sin_port=htons(3128);
	adresse.sin_addr.s_addr= htonl (INADDR_ANY);
	if(bind(sock,(struct sockaddr*) &adresse,sizeof(struct sockaddr_in))<0)
		{
		close(sock);
		perror("bind");
		return -1;
		}
	return sock;
	}
 
void *
serveur(void * rien)
	{
	socket_t * client = NULL;
	int retval;
	int sock_contact;
	int sock_connectee;
	struct sockaddr_in provenance;
	socklen_t longueur;
 	fd_set readfds;
 	FD_ZERO(&readfds);
  	FD_SET(sock_contact, &readfds);
	longueur=sizeof(struct sockaddr_in);
	sock_contact=creer_socket_stream();
	if(sock_contact<0)
		perror("cration socket");
	listen(sock_contact,5);
	nb_connectee = 0;
	client = malloc(sizeof(socket_t));
	while(1)
		{
		retval = 1;/*select(1, &readfds, NULL, NULL, NULL);*/
   		if (retval) {
			sock_connectee=accept (sock_contact, (struct sockaddr *) &provenance, &longueur);
			if(sock_connectee<0)
				{
				perror("accept connection");
				}
			else
				{
				client->sock=sock_connectee;
				client->data=provenance;
				nb_connectee++;
      					/*pthread_create(&thread_serveur,  NULL,ouverture_flux, (void *)sock_connectee);*/
				pthread_create(&(client->clientThread), 0, clientManager,(void *) client); 
				client = NULL;
 
				}
 
			}
		}
	}
 
void * 
clientManager(void * client)
{
  socket_t * mclient =(socket_t *) client;
  char * buffer = NULL;
  /*char * clientBuffer = NULL;*/
  int i=1;
  	char  msg[1024];
	char r_msg [1024];
	memset(r_msg,0,sizeof(r_msg));
 
  pthread_detach(mclient->clientThread);
  pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
 
  buffer = malloc(sizeof(char) * 64);
  sprintf(buffer, "%s", inet_ntoa(mclient->data.sin_addr)); 
  printf("Client : %s - %i client(s) en ligne\n", buffer, nb_connectee);
 
  	/*send(mclient->sock, buffer, (int)strlen(buffer), 0);*/
	buffer="\r\nWelcome MEHDI Experimental Server v 1\r\n";
	send(mclient->sock, buffer, (int)strlen(buffer), 0);
 
  while(i)
  {
 
		recv(mclient->sock, r_msg, 1024, 0);
		printf ("Message:[%s] \n",r_msg);
		printf("\nServeur:");
		fgets(msg,1024,stdin);
		send(mclient->sock, msg, strlen (msg) + 1,0);
		printf("\n 1 or 0?");
		scanf("%d",&i);
	/*tcp_recev(mclient->sock,mclient->data);
	printf("\nServeur with %d:",mclient->sock);
	tcp_send(mclient->sock,mclient->data);	*/
  };
 
  shutdown(mclient->sock, 2);
  nb_connectee--;
  printf("deconnection de %s - %i client(s) en ligne\n", buffer, nb_connectee);
  free(mclient);
 
  pthread_exit(0); 
  return 0;
}
 
 
 
int 
main(void){
	int ret=0;
	pthread_t thread_ser;
   /* Creation du thread du magasin. */
   	printf ("Creation du thread du serveur !\n");
   	ret = pthread_create (& thread_ser, NULL,serveur, NULL );
 
 
   if (ret) perror("creation server");
   /* Creation des threads des clients si celui du magasinn a reussi. *
   {
      printf ("Creation des threads clients !\n");
      for (i = 0; i < NB_CLIENTS; i++)
      {
         ret = pthread_create (
            & store.thread_clients [i], NULL,
            fn_clients, (void *) i
         );
 
         if (ret)
         {
            fprintf (stderr, "%s", strerror (ret));
         }
      }
   }
   else
   {
      fprintf (stderr, "%s", strerror (ret));
   }
 
   
   * Attente de la fin des threads. *
   i = 0;
   for (i = 0; i < NB_CLIENTS; i++)
   {
      pthread_join (store.thread_clients [i], NULL);
   }
   pthread_join (store.thread_store, NULL);*/
 
 
   return EXIT_SUCCESS;
 
}