//utili.h
#pragma once
#include
#include
#include
#include
#include
#include
#include
#include
#define BUFFER_SIZE 256
#define LISTEN_QUEUE_SIZE 5
int startup(char *ip, short port)
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1)
{
perror("socket");
return -1;
}
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = inet_addr(ip);
int yes = 1;
setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
socklen_t addrlen = sizeof(struct sockaddr);
bind(sockfd, (struct sockaddr*)&address, addrlen);
listen(sockfd, LISTEN_QUEUE_SIZE);
return sockfd;
}
//cli.c
#include"utili.h"
int main(int argc, char *argv[])
{
int sockCli = socket(AF_INET, SOCK_STREAM, 0);
if(sockCli == -1)
{
perror("socket");
return -1;
}
struct sockaddr_in addrSer;
addrSer.sin_family = AF_INET;
addrSer.sin_port = htons(atoi(argv[2]));
addrSer.sin_addr.s_addr = inet_addr(argv[1]);
socklen_t addrlen = sizeof(struct sockaddr);
int ret = connect(sockCli, (struct sockaddr*)&addrSer, addrlen);
if(ret == -1)
{
perror("connect");
return -1;
}
char buffer[256];
char recvbuf[256];
while(1)
{
printf("Cli:>");
scanf("%s",buffer);
send(sockCli, buffer, strlen(buffer)+1, 0);
recv(sockCli, recvbuf, 256, 0);
printf("Form Server self msg:> %s\n",recvbuf);
}
close(sockCli);
return 0;
}
//select.c
#include"../utili.h"
#include
#define MAX_CLIENT_SIZE 5
void data_handler(int sockConn)
{
char buffer[256];
recv(sockConn, buffer, 256, 0);
printf("cli msg:> %s\n",buffer);
send(sockConn, buffer, strlen(buffer)+1, 0);
}
// ./ser 127.0.0.1 8080
int main(int argc, char *argv[])
{
int sockSer = startup(argv[1], atoi(argv[2]));
//1 accept
//2 data_handler
fd_set set;
int max_sock = sockSer;
int client_fd[MAX_CLIENT_SIZE] = {0};
int client_conn_num = 0;
int i;
while(1)
{
FD_ZERO(&set);
FD_SET(sockSer, &set);
for(i=0; i= MAX_CLIENT_SIZE)
{
printf("Server Over Load.\n");
continue;
}
client_fd[client_conn_num++] = sockConn;
if(sockConn > max_sock)
max_sock = sockConn;
}
else
{
for(i=0; i
//poll.c
#include"../utili.h"
#include
#define MAX_CLIENT_SIZE 5
void data_handler(int sockConn)
{
char buffer[256];
recv(sockConn, buffer, 256, 0);
printf("from client msg:> %s\n",buffer);
send(sockConn, buffer, strlen( buffer)+1, 0);
}
int main(int argc, char *argv[])
{
int sockSer = startup(argv[1], atoi(argv[2]));
struct pollfd client_fd[MAX_CLIENT_SIZE+1];
client_fd[0].fd = sockSer;
client_fd[0].events = POLLIN;
int client_conn_num = 0;
int i;
while(1)
{
//1 accept
int ret = poll(client_fd, client_conn_num+1, -1);
if(ret == -1)
{
perror("poll");
continue;
}
else if(ret == 0)
{
printf("Server Time Out.\n");
continue;
}
else
{
if(client_fd[0].fd==sockSer && client_fd[0].revents==POLLIN)
{
struct sockaddr_in addrCli;
socklen_t addrlen = sizeof(struct sockaddr);
int sockConn = accept(sockSer, (struct sockaddr*)&addrCli, &addrlen);
if(sockConn == -1)
{
perror("accpet");
continue;
}
if(client_conn_num >= MAX_CLIENT_SIZE)
{
char *msg = "Server Over Load.";
send(sockConn, msg, strlen(msg)+1, 0);
continue;
}
client_conn_num++;
client_fd[client_conn_num].fd = sockConn;
client_fd[client_conn_num].events = POLLIN;
}
else
{
for(i=1; i
//epoll.c
#include"../utili.h"
#include
#define MAX_CLIENT_SIZE 5
#define EPOLL_SIZE 1024
void data_handler(int sockConn)
{
char buffer[256];
recv(sockConn, buffer, 256, 0);
printf("from client msg:> %s\n",buffer);
send(sockConn, buffer, strlen( buffer)+1, 0);
}
void add_event(int epfd, int fd, int event)
{
struct epoll_event ev;
ev.events = event;
ev.data.fd = fd;
epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &ev);
}
void mod_event(int epfd, int fd, int event)
{
struct epoll_event ev;
ev.events = event;
ev.data.fd = fd;
epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &ev);
}
void del_event(int epfd, int fd, int event)
{
struct epoll_event ev;
ev.events = event;
ev.data.fd = fd;
epoll_ctl(epfd, EPOLL_CTL_DEL, fd, &ev);
}
int main(int argc, char *argv[])
{
int sockSer = startup(argv[1], atoi(argv[2]));
int epfd = epoll_create(EPOLL_SIZE);
//add server event
//struct epoll_event ev;
//ev.events = EPOLLIN;
//ev.data.fd = sockSer;
//epoll_ctl(epfd, EPOLL_CTL_ADD, sockSer, &ev);
add_event(epfd, sockSer, EPOLLIN);
int i;
struct epoll_event revent[MAX_CLIENT_SIZE];
int client_conn_num = 0;
while(1)
{
int ret = epoll_wait(epfd, revent, MAX_CLIENT_SIZE, -1);
if(ret == -1)
{
perror("poll");
continue;
}
else if(ret == 0)
{
printf("Server Time Out.\n");
continue;
}
else
{
for(i=0; i= MAX_CLIENT_SIZE)
{
char *msg = "Server Over Load.";
send(sockConn, msg, strlen(msg)+1, 0);
continue;
}
client_conn_num++;
//ev.events = EPOLLIN;
//ev.data.fd = sockConn;
//epoll_ctl(epfd, EPOLL_CTL_ADD, sockConn, &ev);
add_event(epfd, sockConn, EPOLLIN);
}
else if(revent[i].events & EPOLLIN)
{
data_handler(revent[i].data.fd);
}
}
}
}
close(sockSer);
return 0;
}
参考资料:《linux高性能服务编程》第九章