国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > 2高并发服务器:多线程服务器

2高并发服务器:多线程服务器

来源:程序员人生   发布时间:2014-10-07 08:00:01 阅读次数:2632次

1多线程并发服务器

在使用线程模型开发服务器时需要考虑以下问题:

A调整进程最大文件描述符上限

B线程如有共享数据,考虑线程同步

C服务于客户端线程退出时,退出处理。(退出值,分离态)

D系统负载,随着连接客户端增加,导致其它线程不能及时得到CPU

2.案例说明

server.c,代码如下:

/* server.c */

#include <stdio.h>

#include <string.h>

#include <netinet/in.h>

#include <arpa/inet.h>

#include <pthread.h>

#include "wrap.h"

#define MAXLINE 80

#define SERV_PORT 8000

 

struct s_info {

struct sockaddr_in cliaddr;

int connfd;

};

 

void *do_work(void *arg)

{

int n,i;

struct s_info *ts = (struct s_info*)arg;

char buf[MAXLINE];

char str[INET_ADDRSTRLEN];

/*可以在创建线程前设置线程创建属性,设为分离态,哪种效率高内? 答:线程前设置线程属性*/

pthread_detach(pthread_self());

while (1) {

n = Read(ts->connfd, buf, MAXLINE);

if (n == 0) {

printf("the other side has been closed. ");

break;

}

printf("received from %s at PORT %d ",

inet_ntop(AF_INET, &(*ts).cliaddr.sin_addr, str, sizeof(str)),

ntohs((*ts).cliaddr.sin_port));

for (i = 0; i < n; i++) {

buf[i] = toupper(buf[i]);

       }

Write(ts->connfd, buf, n);

}

Close(ts->connfd);

}

 

int main(void) {

struct sockaddr_in servaddr, cliaddr;

socklen_t cliaddr_len;

int listenfd, connfd;

int i = 0;

pthread_t tid;

struct s_info ts[383];

listenfd = Socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

servaddr.sin_addr.s_addr = htonl(INADDR_ANY);

servaddr.sin_port = htons(SERV_PORT);

Bind(listenfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

Listen(listenfd, 20);

printf("Accepting connections ... ");

while (1) {

cliaddr_len = sizeof(cliaddr);

connfd = Accept(listenfd, (struct sockaddr *)&cliaddr, &cliaddr_len);

ts[i].cliaddr = cliaddr;

ts[i].connfd = connfd;

/*达到线程最大数时,pthread_create出错处理,增加服务器稳定性*/

pthread_create(&tid, NULL, do_work, (void*)&ts[i]);

i++;

}

return 0;

}

client.c

/* client.c */

#include <stdio.h>

#include <string.h>

#include <unistd.h>

#include <netinet/in.h>

#include "wrap.h"

#define MAXLINE 80

#define SERV_PORT 8000

int main(int argc, char *argv[])

{

struct sockaddr_in servaddr;

char buf[MAXLINE];

int sockfd, n;

sockfd = Socket(AF_INET, SOCK_STREAM, 0);

bzero(&servaddr, sizeof(servaddr));

servaddr.sin_family = AF_INET;

inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);

servaddr.sin_port = htons(SERV_PORT);

Connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

while (fgets(buf, MAXLINE, stdin) != NULL) {

Write(sockfd, buf, strlen(buf));

n = Read(sockfd, buf, MAXLINE);

if (n == 0)

printf("the other side has been closed. ");

else

Write(STDOUT_FILENO, buf, n);

}

Close(sockfd);

return 0;

}

wrap.h

#ifndef __WRAP_H_

#define __WRAP_H_

 

void perr_exit(const char *s);

int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr);

void Bind(int fd, const struct sockaddr *sa, socklen_t salen);

void Connect(int fd, const struct sockaddr *sa, socklen_t salen);

void Listen(int fd, int backlog);

int Socket(int family, int type, int protocol);

ssize_t Read(int fd, void *ptr, size_t nbytes);

ssize_t Write(int fd, const void *ptr, size_t nbytes);

void Close(int fd);

ssize_t Readn(int fd, void *vptr, size_t n);

ssize_t Writen(int fd, const void *vptr, size_t n);

static ssize_t my_read(int fd, char *ptr);

ssize_t Readline(int fd, void *vptr, size_t maxlen);

 

#endif

wrap.c

#include <stdlib.h>

#include <errno.h>

#include <sys/socket.h>

 

void perr_exit(const char *s)

{

        perror(s);

        exit(1);

}

 

int Accept(int fd, struct sockaddr *sa, socklen_t *salenptr)

{

        int n;

 

again:

        if ( (n = accept(fd, sa, salenptr)) < 0) {

                  if ((errno == ECONNABORTED) || (errno

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生