- 论坛徽章:
- 0
|
int main(void)
282 {
283 int i,sd1,connsd;
284 socklen_t clientaddr_len,myaddr_len;
285 struct sockaddr_in myaddr,clientaddr;
286 TASK_MORE chos;
287 pthread_t brast_pid;
288 SER_THREAD_ARG arg[PROGRAM_NUM];
289 pthread_t pid[PROGRAM_NUM],manage_pid;
290 char ipv4[15];
291
292 bzero(&chos,sizeof(chos));
293
294 /*create a thread to broadcast*/
295 pthread_create(&brast_pid,NULL,broadcast,NULL);
296 pthread_detach(brast_pid);
297 sd1 = socket(AF_INET,SOCK_STREAM,0);
298 if(sd1 == -1){
299 perror("socket()");
300 exit(1);
301 }
302 myaddr.sin_family = AF_INET;
303 myaddr.sin_port = htons(PORT1);
304 inet_pton(AF_INET,"0.0.0.0",&myaddr.sin_addr);
305
306 myaddr_len = sizeof(myaddr);
307
308 if(bind(sd1,(struct sockaddr *)&myaddr,myaddr_len) < 0){
309 perror(0);
310 exit(1);
311 }
312
313 listen(sd1,256);
314 while(1){
315 clientaddr_len = sizeof(clientaddr);
316 connsd = accept(sd1,(struct sockaddr *)&clientaddr,&clientaddr_len);
317 printf("12345\n");
318 if(connsd < 0){
319 perror("accept()");
320 exit(1);
321 }
322 inet_ntop(AF_INET, &clientaddr.sin_addr, ipv4, 15);
323 printf("\n##### Connect form IP:%s Port:%d #####\n",ipv4,ntohs(clientaddr.sin_port));
324 if(recv(connsd,&chos,sizeof(chos),0) < 0){
325 perror("recv()");
326 exit(1);
327 }
328 // if(chos.dload_flag.flag == EXIT)
329 // continue;
330
331 send_ensure_sigle(connsd,0);
332
333 WAIT_THREAD_EXIT wait;
334 wait.threadnum = chos.task_num;
335 wait.close_sock = connsd;
336 for(i = 0;i <= chos.task_num;i++){
337 arg.sockfd = connsd;
338 memcpy(&arg.client_chose_msg,&chos.task,sizeof(CLIENT_TO_SERVER));
339 pthread_create(&pid,NULL,ready_send_it,&arg);
340 }
341 for(i = 0;i <= wait.threadnum;i++){
342 wait.all_pid = pid;
343 }
344 pthread_create(&manage_pid,NULL,wait_send_over,(void *)&wait);
345 pthread_detach(manage_pid);
346 signal(SIGPIPE,SIG_IGN);
347 }
348 pthread_mutex_destroy(&job_mutex);
349 exit(0);
350 }
标颜色位置出现段错误,不知何故。忘指导。 |
|