接着接收该客户的名字并显示 。然后接收来自该客户的信息(字符串) 。
每当收到一个字符串,则显示该字符串,并将字符串反转,再将反转的字
符发回客户 。之后,继续等待接收该客户的信息直至该客户关闭连接 。服
务器具有同时处理多客户的能力 。
*/
#include <stdio.h> /* These are the usual header files */
#include <strings.h> /* for bzero() */
#include <unistd.h> /* for close() */
#include <sys/types.h>
【linux下高并发服务器实现】#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 1234 /* Port that will be opened */
#define BACKLOG 2 /* Number of allowed connections */
#define MAXDATASIZE 1000
void process_cli(int connectfd, struct sockaddr_in client);
main()
{
int listenfd, connectfd; /* socket descriptors */
pid_t pid;
struct sockaddr_in server; /* server's address information */
struct sockaddr_in client; /* client's address information */
int sin_size;
/* Create TCP socket */
if ((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
/* handle exception */
perror("Creating socket failed.");
exit(1);
}
int opt = SO_REUSEADDR;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_port=htons(PORT);
server.sin_addr.s_addr = htonl (INADDR_ANY);
if (bind(listenfd, (struct sockaddr *)&server, sizeof(struct sockaddr)) == -1) {
/* handle exception */
perror("Bind error.");
exit(1);
}
if(listen(listenfd,BACKLOG) == -1){ /* calls listen() */
perror("listen() errorn");
exit(1);
}
sin_size=sizeof(struct sockaddr_in);
while(1)
{
/*accept connection.what causes the acceptance? */
if ((connectfd = accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1) {
perror("accept() errorn");
exit(1);
}
/* Create child process to service client */
if ((pid=fork())>0) {
/* parent process */
close(connectfd);
continue;
}
else if (pid==0) {
/*child process*/
close(listenfd);
process_cli(connectfd, client);
exit(0);
}
else {
printf("fork errorn");
exit(0);
}
}
close(listenfd); /* close listenfd */
}
void process_cli(int connectfd, struct sockaddr_in client)
{
int num;
char recvbuf[MAXDATASIZE], sendbuf[MAXDATASIZE], cli_name[MAXDATASIZE];
printf("You got a connection from %s. ",inet_ntoa(client.sin_addr) ); /* prints client's IP */
/* Get client's name from client */
num = recv(connectfd, cli_name, MAXDATASIZE,0);
if (num == 0) {
close(connectfd);
printf("Client disconnected.n");
return;
}
cli_name[num - 1] = '';
printf("Client's name is %s.n",cli_name);
while (num = recv(connectfd, recvbuf, MAXDATASIZE,0))
{
int i = 0;
recvbuf[num] = '';
printf("Received client( %s ) message: %s",cli_name, recvbuf);
for (i = 0; i < num - 1; i++) {
sendbuf[i] = recvbuf[num - i -2];
}
sendbuf[num - 1] = '';
send(connectfd,sendbuf,strlen(sendbuf),0); /* send to the client welcome message */
}
close(connectfd); /* close connectfd */
}
例子:多线程并发服务器
#include <stdio.h> /* These are the usual header files */
#include <string.h> /* for bzero() */
#include <unistd.h> /* for close() */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <stdlib.h> /* for exit in c++(.C/.cc) ; no need for c ??*/
#define PORT 1234 /* Port that will be opened */
#define BACKLOG 5 /* Number of allowed connections */
#define MAXDATASIZE 1000
//void process_cli(int connectfd, sockaddr_in client); // c only supports struct sockaddr_in, but c++ support sockaddr_in
void process_cli(int connectfd, struct sockaddr_in client);
/* function to be executed by the new thread */
void* start_routine(void* arg);
typedef struct _ARG {
int connfd;
struct sockaddr_in client;
}ARG;
// it's better to use typedef struct
推荐阅读
- Linux引导101
- 20 个 Linux 终端下的生产力工具
- Linux 中软链接和硬链接的区别
- LINUX系统和Unix系统有什么区别和联系呢?
- 基于Linux系统的高可用中小型网站集群架构说明
- Linux世界——ssh登录安全简单介绍
- Linux下防御DDOS攻击的操作梳理
- Linux系统常用的文件管理命令
- 在 Linux 上检查 MySQL/MariaDB 数据库正常运行时间的三种方法
- 一文带你彻底理解Linux的各种终端类型及概念