前面介绍了关于连接Linux服务端方式,但是服务端的资源是有限的,所以我们通常需要重新思考,设计一套服务器模型来处理对应的客户端的请求。
第一种:并发服务器,通过主进程统一处理客户端的连接,当客户端连接过后,临时fork()进程,由子进程处理客户端请求,将连接请求和业务进行了分离。
server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#define BUFFLEN 1024
#define SERVER_PORT 8888
#define BACKLOG 5
static void handle_request(int s_c)
{
time_t now;
char buff[BUFFLEN];
int n = 0;
memset(buff, 0, BUFFLEN);
n = recv(s_c, buff, BUFFLEN,0);
if(n > 0 && !strncmp(buff, "TIME", 4))
{
memset(buff, 0, BUFFLEN);
now = time(NULL);
sprintf(buff, "%24s\r\n",ctime(&now));
send(s_c, buff, strlen(buff),0);
}
close(s_c);
}
static int handle_connect(int s_s)
{
int s_c;
struct sockaddr_in from;
socklen_t len = sizeof(from);
while(1)
{
s_c = accept(s_s, (struct sockaddr*)&from, &len);
if(s_c > 0)
{
if(fork() > 0){
close(s_c);
}else{
handle_request(s_c);
return(0);
}
}
}
}
int main(int argc, char *argv[])
{
int s_s;
struct sockaddr_in local;
s_s = socket(AF_INET, SOCK_STREAM, 0);
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_port = htons(SERVER_PORT);
bind(s_s, (struct sockaddr*)&local, sizeof(local));
listen(s_s, BACKLOG);
handle_connect(s_s);
close(s_s);
return 0;
}
- 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
- 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
代码比较详细,容易理解。
下面介绍客户端代码,后面的客户端代码都是一样的。
client.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#define BUFFLEN 1024
#define SERVER_PORT 8888
int main(int argc, char *argv[])
{
int s;
struct sockaddr_in server;
char buff[BUFFLEN];
int n = 0;
s = socket(AF_INET, SOCK_STREAM, 0);
memset(&server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(SERVER_PORT);
connect(s, (struct sockaddr*)&server,sizeof(server));
memset(buff, 0, BUFFLEN);
strcpy(buff, "TIME");
send(s, buff, strlen(buff), 0);
memset(buff, 0, BUFFLEN);
n = recv(s, buff, BUFFLEN, 0);
if(n >0){
printf("TIME:%s",buff);
}
close(s);
return 0;
}
- 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
- 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
第二种模型:通过线程来处理,线程比进程占用资源少,效率高,数据共享。通过pthread_create()建立一个连接请求处理,线程处理函数为handle_request().
server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#define BUFFLEN 1024
#define SERVER_PORT 8888
#define BACKLOG 5
static void handle_request(void *argv)
{
int s_c = *((int*)argv);
time_t now;
char buff[BUFFLEN];
int n = 0;
memset(buff, 0, BUFFLEN);
n = recv(s_c, buff, BUFFLEN,0);
if(n > 0 && !strncmp(buff, "TIME", 4))
{
memset(buff, 0, BUFFLEN);
now = time(NULL);
sprintf(buff, "%24s\r\n",ctime(&now));
send(s_c, buff, strlen(buff),0);
}
close(s_c);
}
static void handle_connect(int s_s)
{
int s_c;
struct sockaddr_in from;
socklen_t len = sizeof(from);
pthread_t thread_do;
while(1)
{
s_c = accept(s_s, (struct sockaddr*)&from, &len);
if(s_c > 0)
{
pthread_create(&thread_do,
NULL,
(void*)handle_request,
&s_c);
}
}
}
int main(int argc, char *argv[])
{
int s_s;
struct sockaddr_in local;
s_s = socket(AF_INET, SOCK_STREAM, 0);
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_port = htons(SERVER_PORT);
bind(s_s, (struct sockaddr*)&local, sizeof(local));
listen(s_s, BACKLOG);
handle_connect(s_s);
close(s_s);
return 0;
}
- 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
- 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
第三种:服务端各线程独自accept(),使用互斥锁,使用pthread_create()建立多个线程组成的线程池,主线程等待程序结束,各个线程独自接收客户端accept,以及后面数据处理。
server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#define BUFFLEN 1024
#define SERVER_PORT 8888
#define BACKLOG 5
#define CLIENTNUM 2
pthread_mutex_t ALOCK = PTHREAD_MUTEX_INITIALIZER;
static void *handle_request(void *argv)
{
int s_s = *((int*)argv);
int s_c;
struct sockaddr_in from;
socklen_t len = sizeof(from);
for(;;)
{
time_t now;
char buff[BUFFLEN];
int n = 0;
pthread_mutex_lock(&ALOCK);
s_c = accept(s_s, (struct sockaddr*)&from, &len);
pthread_mutex_unlock(&ALOCK);
memset(buff, 0, BUFFLEN);
n = recv(s_c, buff, BUFFLEN,0);
if(n > 0 && !strncmp(buff, "TIME", 4))
{
memset(buff, 0, BUFFLEN);
now = time(NULL);
sprintf(buff, "%24s\r\n",ctime(&now));
send(s_c, buff, strlen(buff),0);
}
close(s_c);
}
return NULL;
}
static void handle_connect(int s)
{
int s_s = s;
pthread_t thread_do[CLIENTNUM];
int i = 0;
for(i=0;i<CLIENTNUM;i++)
{
pthread_create(&thread_do[i],
NULL,
handle_request,
(void*)&s_s);
}
for(i=0;i<CLIENTNUM;i++)
pthread_join(thread_do[i], NULL);
}
int main(int argc, char *argv[])
{
int s_s;
struct sockaddr_in local;
s_s = socket(AF_INET, SOCK_STREAM, 0);
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_port = htons(SERVER_PORT);
bind(s_s, (struct sockaddr*)&local, sizeof(local));
listen(s_s, BACKLOG);
handle_connect(s_s);
close(s_s);
return 0;
}
- 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
- 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
第四种:IO复用服务器,并发服务器客户端越多,对服务器造成的压力越大,所以还有第四种模型,IO复用函数用select来做。
server.c
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/select.h>
#define BUFFLEN 1024
#define SERVER_PORT 8888
#define BACKLOG 5
#define CLIENTNUM 1024 /*最大支持客户端数量*/
int connect_host[CLIENTNUM];
int connect_number = 0;
static void *handle_request(void *argv)
{
time_t now;
char buff[BUFFLEN];
int n = 0;
int maxfd = -1;
fd_set scanfd;
struct timeval timeout;
timeout.tv_sec = 1;
timeout.tv_usec = 0;
int i = 0;
int err = -1;
for(;;)
{
maxfd = -1;
FD_ZERO(&scanfd);
for(i=0;i<CLIENTNUM;i++)
{
if(connect_host[i] != -1)
{
FD_SET(connect_host[i], &scanfd);
if(maxfd < connect_host[i])
{
maxfd = connect_host[i];
}
}
}
err = select(maxfd + 1, &scanfd, NULL, NULL, &timeout) ;
switch(err)
{
case 0:
break;
case -1:
break;
default:
if(connect_number<=0)
break;
for(i = 0;i<CLIENTNUM;i++)
{
if(connect_host[i] != -1)
if(FD_ISSET(connect_host[i],&scanfd))
{
memset(buff, 0, BUFFLEN);
n = recv(connect_host[i], buff, BUFFLEN,0);
if(n > 0 && !strncmp(buff, "TIME", 4))
{
memset(buff, 0, BUFFLEN);
now = time(NULL);
sprintf(buff, "%24s\r\n",ctime(&now));
send(connect_host[i], buff, strlen(buff),0);
}
connect_host[i] = -1;
connect_number --;
close(connect_host[i]);
}
}
break;
}
}
return NULL;
}
static void *handle_connect(void *argv)
{
int s_s = *((int*)argv) ;
struct sockaddr_in from;
socklen_t len = sizeof(from);
for(;;)
{
int i = 0;
int s_c = accept(s_s, (struct sockaddr*)&from, &len);
printf("a client connect, from:%s\n",inet_ntoa(from.sin_addr));
for(i=0;i<CLIENTNUM;i++)
{
if(connect_host[i] == -1)
{
connect_host[i]= s_c;
connect_number ++;
break;
}
}
}
return NULL;
}
int main(int argc, char *argv[])
{
int s_s;
struct sockaddr_in local;
int i = 0;
memset(connect_host, -1, CLIENTNUM);
s_s = socket(AF_INET, SOCK_STREAM, 0);
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_addr.s_addr = htonl(INADDR_ANY);
local.sin_port = htons(SERVER_PORT);
bind(s_s, (struct sockaddr*)&local, sizeof(local));
listen(s_s, BACKLOG);
pthread_t thread_do[2];
pthread_create(&thread_do[0],
NULL,
handle_connect,
(void*)&s_s);
pthread_create(&thread_do[1],
NULL,
handle_request,
NULL);
for(i=0;i<2;i++)
pthread_join(thread_do[i], NULL);
close(s_s);
return 0;
}
- 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
- 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
选择合适的服务器模型十分重要,对于编程有很大的影响。