/*************************************************************************
Author: Wan Xinshuo
Created Time: 2009年04月14日 星期二 11时33分18秒
File Name: server02.c
Description:
************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXLINE 100
#define OPEN_MAX 100
#define LISTENQ 20
#define SERV_PORT 8889
#define INFTIM 1000
void setnonblocking(int sock)
{
int opts;
opts=fcntl(sock,F_GETFL);
if(opts<0)
{
perror("fcntl(sock,GETFL)");
_exit(1);
}
opts = opts|O_NONBLOCK;
if(fcntl(sock,F_SETFL,opts)<0)
{
perror("fcntl(sock,SETFL,opts)");
_exit(1);
}
}
int main(int argc, char *argv[] )
{
int i, maxi, listenfd, connfd, sockfd,epfd,nfds;
ssize_t n;
char line[MAXLINE];
//声明epoll_event结构体的变量,ev用于注册事件,数组用于回传要处理的事件
struct epoll_event ev,events[20];
if ( argc != 2 )
{
fprintf(stderr, "usage: server02 port\nEx:\n$./server02 8889\n");
_exit(1);
}
//生成用于处理accept的epoll专用的文件描述符
epfd=epoll_create(256);
struct sockaddr_in clientaddr;
struct sockaddr_in serveraddr;
socklen_t clilen = sizeof clientaddr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
//把socket设置为非阻塞方式
setnonblocking(listenfd);
//设置与要处理的事件相关的文件描述符
ev.data.fd=listenfd;
//设置要处理的事件类型
ev.events=EPOLLIN|EPOLLET;
//注册epoll事件
epoll_ctl(epfd,EPOLL_CTL_ADD,listenfd,&ev);
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
const char *local_addr="192.168.100.7";
inet_aton(local_addr,&(serveraddr.sin_addr));//htons(SERV_PORT);
serveraddr.sin_port=htons(atoi(argv[1]));
bind(listenfd,(struct sockaddr *)&serveraddr, sizeof(serveraddr));
listen(listenfd, LISTENQ);
maxi = 0;
for ( ; ; ) {
//等待epoll事件的发生
nfds=epoll_wait(epfd,events,20,500);
//处理所发生的所有事件
for(i=0;i
{
if(events[i].data.fd==listenfd)
{
connfd = accept(listenfd,(struct sockaddr *)&clientaddr, &clilen);
if(connfd<0){
perror("connfd<0");
_exit(1);
}
setnonblocking(connfd);
char *str = inet_ntoa(clientaddr.sin_addr);
printf("connect from %s\n", inet_ntoa(clientaddr.sin_addr));
//设置用于读操作的文件描述符
ev.data.fd=connfd;
//设置用于注测的读操作事件
ev.events=EPOLLIN|EPOLLET;
//注册ev
epoll_ctl(epfd,EPOLL_CTL_ADD,connfd,&ev);
}
else if(events[i].events&EPOLLIN)
{
if ( (sockfd = events[i].data.fd) < 0) continue;
if ( (n = read(sockfd, line, MAXLINE)) < 0) {
if (errno == ECONNRESET) {
close(sockfd);
events[i].data.fd = -1;
} else
printf("readline error\n");
} else if (n == 0) {
close(sockfd);
events[i].data.fd = -1;
} else {
printf("buff = %s\n",line );
}
//设置用于写操作的文件描述符
ev.data.fd=sockfd;
//设置用于注测的写操作事件
ev.events=EPOLLOUT|EPOLLET;
//修改sockfd上要处理的事件为EPOLLOUT
epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
}
else if(events[i].events&EPOLLOUT)
{
sockfd = events[i].data.fd;
write(sockfd, line, n);
//设置用于读操作的文件描述符
ev.data.fd=sockfd;
//设置用于注测的读操作事件
ev.events=EPOLLIN|EPOLLET;
//修改sockfd上要处理的事件为EPOLIN
epoll_ctl(epfd,EPOLL_CTL_MOD,sockfd,&ev);
}
}
}
}
/*************************************************************************
Author: Wan Xinshuo
Created Time: 2009年04月14日 星期二 11时33分18秒
File Name: client01.c
Description:
************************************************************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define MAXDATASIZE 100 // max number of bytes we can get at once
int main(int argc, char *argv[])
{
int sockfd, numbytes;
char buf[MAXDATASIZE];
struct hostent *he;
struct sockaddr_in their_addr; // connector'saddress information
if (( argc == 1) || (argc==2) ) {
fprintf(stderr,"usage: client hostname\nEx:\n$./client01 ip port\n");
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
herror("gethostbyname");
exit(1);
}
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) {
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(atoi(argv[2])); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(their_addr.sin_zero, '\0', sizeof their_addr.sin_zero);
if (connect(sockfd, (struct sockaddr *)&their_addr,
sizeof their_addr) == -1) {
perror("connect");
exit(1);
}
while( 1 )
{
if( send(sockfd, "hello, this is client message!", strlen("hello, this is client message!"), 0 ) == -1 )
{
perror("send");
}
if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1)
{
perror("recv");
}
else if( numbytes == 0 )
{
printf("Remote server has shutdown!\n");
break;
}
buf[numbytes] = '\0';
printf("Received: %s \n",buf);
sleep(1);
}
close(sockfd);
return 0;
}
编译:
gcc -o server02 server02.c
gcc -o client01 client01.c
运行:
$server02 8889
$client01 ip 8889
注: "ip" 为 server02所在的ip
阅读(1097) | 评论(0) | 转发(0) |