码迷,mamicode.com
首页 > 编程语言 > 详细

0703 socket服务器多线程处理客户请求

时间:2014-07-06 16:30:11      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   2014   io   

 1 /*************************************************************************
 2     > File Name: my_socket.h
 3     > Author:Monica
 4     > Mail:liling222@126.com 
 5     > Created Time: Thu 03 Jul 2014 10:40:54 AM CST
 6  ************************************************************************/
 7 
 8 #ifndef __MY_SOCKET_H__
 9 #define __MY_SOCKET_H__
10 
11 #include <stdio.h>
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <sys/stat.h>
15 #include <arpa/inet.h>
16 #include <netinet/in.h>
17 #include <fcntl.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include "msg.h"
22 
23 int listenfd_init(char* ip, char* port);
24 
25 
26 #endif
 1 /*************************************************************************
 2     > File Name: server.c
 3     > Author: Monica
 4     > Mail:liling222@126.com 
 5     > Created Time: Thu 03 Jul 2014 10:02:48 AM CST
 6  ************************************************************************/
 7 
 8 #include  "my_socket.h"
 9 
10 int listenfd_init(char* ip, char* port){
11 
12     int fd_server;
13     int reuse = 1 ;
14     struct sockaddr_in server_addr;
15     fd_server = socket(AF_INET,SOCK_STREAM, 0);
16     if(fd_server == -1)
17     {
18         perror("socket");
19         exit(-1);
20     }
21     if(setsockopt(fd_server,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(int)) == -1)
22     {
23         perror("setsockopt");
24         close(fd_server);
25         exit(-1);
26     }
27     memset(&server_addr, 0 , sizeof(server_addr)) ;
28     server_addr.sin_family = AF_INET ;
29     server_addr.sin_port = htons(atoi(port));
30     server_addr.sin_addr.s_addr = inet_addr(ip);
31     if( bind(fd_server,(struct sockaddr*)&server_addr,sizeof(server_addr)) == -1)
32     {
33         perror("bind");
34         close(fd_server);
35         exit(-1);
36     }
37     if(listen(fd_server,5) == -1)
38     {
39         perror("listen");
40         close(fd_server);
41         exit(-1);
42     }
43     return fd_server;
44 }
 1 #include "my_socket.h"
 2 
 3 void  * thread_handle(void* fd_client);
 4 int main(int argc , char* argv[])//exe ip port
 5 {
 6     int fd_server,fd_client;
 7     struct sockaddr_in server_addr,client_addr ;
 8     int addrlen = sizeof(client_addr);
 9     int tid;
10     if(argc != 3)
11     {
12         printf("usge : exe ip port");
13         exit(-1);
14     }
15     fd_server = listenfd_init(argv[1], argv[2]);
16     while(fd_client = accept(fd_server,(struct sockaddr*)&client_addr,&addrlen)){
17         printf("a client connect IP:%s\tPORT:%d\n",inet_ntoa(client_addr.sin_addr),ntohs(client_addr.sin_port));
18         pthread_create(&tid, NULL, thread_handle, (void *)fd_client);
19     }
20     close(fd_server);
21     return 0;
22 }
23 void*  thread_handle(void* fd_client){
24     pthread_detach(pthread_self());
25     MSG recv_msg, snd_msg;
26     int read_n;
27     int fd_file;
28     int fd_cli = (int)fd_client;
29     memset(&recv_msg, 0, sizeof(recv_msg));
30     recv(fd_cli, &recv_msg.s_len, MSG_LEN , 0) ;
31     recv(fd_cli, recv_msg.s_buf, recv_msg.s_len, 0) ;
32     printf("recv msg :%s\n", recv_msg.s_buf);
33     fd_file = open(recv_msg.s_buf, O_RDONLY);
34     while(memset(&snd_msg, 0, sizeof(snd_msg)), (read_n =  read(fd_file, snd_msg.s_buf, MSG_SIZE)) > 0)
35     {
36         snd_msg.s_len = read_n ;
37         send(fd_cli, &snd_msg, snd_msg.s_len + MSG_LEN ,0);
38     }
39     snd_msg.s_len = 0 ;
40     send(fd_cli, &snd_msg, snd_msg.s_len + MSG_LEN ,0);
41     close(fd_file);
42     close(fd_cli);
43 }
 1 /*************************************************************************
 2     > File Name: client.c
 3     > Author:Monica
 4     > Mail:liling222@126.com 
 5     > Created Time: Thu 03 Jul 2014 10:46:20 AM CST
 6  ************************************************************************/
 7 
 8 #include "my_socket.h"
 9 int main(int argc, char* argv[]){
10     int fd_client;
11     struct sockaddr_in  server_addr;
12     MSG recv_msg, snd_msg;
13     int fd_file;
14     int recv_len = 0 ;
15     char filename[128];
16     server_addr.sin_family = AF_INET;
17     server_addr.sin_addr.s_addr  = inet_addr(argv[1]);
18     server_addr.sin_port   = htons(atoi(argv[2]));
19     if((fd_client = socket(AF_INET, SOCK_STREAM, 0)) == -1){
20         perror("socket");
21         exit(-1);
22     }
23     if(connect(fd_client, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1){
24         perror("connect");
25         close(fd_client);
26         exit(-1);
27     }
28     printf("inputs filename:");
29     scanf("%s", snd_msg.s_buf);
30     snd_msg.s_len = strlen(snd_msg.s_buf);
31     send(fd_client,&snd_msg, snd_msg.s_len+MSG_LEN, 0 );
32     sprintf(filename, "download_%s", snd_msg.s_buf);
33     fd_file = open(filename, O_WRONLY|O_CREAT, 0666);
34     while(1){
35         printf("begin recv\n");
36         memset(&recv_msg, 0, sizeof(recv_msg));
37         recv(fd_client, &recv_msg.s_len, MSG_LEN, 0);
38         if(recv_msg.s_len >0){
39             recv(fd_client, recv_msg.s_buf, MSG_SIZE, 0);
40             write(fd_file, recv_msg.s_buf, strlen(recv_msg.s_buf));
41             recv_len += recv_msg.s_len;
42             printf("download...%d\n", recv_len);
43         }
44         else{
45             break;
46         }
47     }
48     close(fd_file);
49     close(fd_client);
50     return 0;
51 }

 

0703 socket服务器多线程处理客户请求,布布扣,bubuko.com

0703 socket服务器多线程处理客户请求

标签:style   blog   color   os   2014   io   

原文地址:http://www.cnblogs.com/monicalee/p/3823405.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!