先运行server:
#include<sys/socket.h>
#include<fcntl.h>
#include<netinet/in.h>
#include<netinet/tcp.h>
#include"string.h"
#include"stdlib.h"
#include"stdio.h"
#include<pthread.h>
#include<sys/epoll.h>
#define SOCKET_SND_BUF_SIZE (1024*1024)
#define SOCKET_RCV_BUF_SIZE (1024*1024)
void INFO(const char *s){
printf("%s\n",s);
}
int create_listen_sockfd(int &fd,int port,int max_conn){
//create socket
fd=socket(PF_INET,SOCK_STREAM,0);
if(fd==-1){
INFO("err:create socket");
return -1;
}else{
INFO("succeed:create socket");
}
//setsockopt
int options;
options = SOCKET_SND_BUF_SIZE;
setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &options, sizeof(int));
options = SOCKET_RCV_BUF_SIZE;
setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &options, sizeof(int));
options = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &options, sizeof(int));
options = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, options | O_NONBLOCK);
int on = 1;
int ret;
ret = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&on, sizeof(on));
if(ret==-1){
INFO("err:setsocketopt");
return -1;
}else{
INFO("succeed:setsocketopt");
}
//bind
sockaddr_in addr;
memset(&addr,0,sizeof addr);
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=INADDR_ANY;
addr.sin_port=htons(port);
ret=bind(fd,(const sockaddr*)&addr,sizeof addr);
if(ret==-1){
INFO("err:bind");
return -1;
}else{
INFO("succeed:bind");
}
//listen
ret=listen(fd,max_conn);
if(ret==-1){
INFO("err:listen");
return -1;
}else{
INFO("succeed:listen");
}
return 0;
}
int add_fd_into_epfd(int &epfd,int &fd){
epoll_event event;
event.events=EPOLLIN|EPOLLET;
event.data.fd=fd;
int ret=epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&event);
if(ret<0){
INFO("err:epoll ctl");
return -1;
}
return 0;
}
int rm_fd_from_epfd(int &epfd,int &fd){
epoll_event event;
event.events=EPOLLIN|EPOLLET;
event.data.fd=fd;
int ret=epoll_ctl(epfd,EPOLL_CTL_DEL,fd,&event);
if(ret<0){
INFO("err:epoll ctl");
return -1;
}
return 0;
}
int recv_data(int fd){
char buf[4096];
char recv_buf[4096]="";
int offset=0;
int len=-1;
while((len=recv(fd,buf,sizeof(buf),0))>0){
printf("recv:%s\n",buf);
sprintf(recv_buf+offset,"%s",buf);
offset+=len;
memset(buf,0,strlen(buf));
}
if(len<0){
INFO("err:recv data");
return -1;
}else{
INFO("connect closed");
}
return 0;
}
int main(){
int max_conn=100;
int port=8012;
//create listen sockfd
int listen_fd;
int ret;
ret=create_listen_sockfd(listen_fd,port,max_conn);
if(ret==-1) return -1;
//epoll
int epfd;
epfd=epoll_create(max_conn);
if(epfd==-1){
INFO("err:create epoll fd");
return -1;
}
ret=add_fd_into_epfd(epfd,listen_fd);
if(ret==-1) return -1;
epoll_event *ready_events=new epoll_event[max_conn];
//svr
while(true){
int event_num=epoll_wait(epfd,ready_events,max_conn,-1);
//handler
printf("epoll wake up! ready_events:%d\n",event_num);
for(int i=0;i<event_num;++i){
int ready_fd=ready_events[i].data.fd;
if(ready_fd==listen_fd){
//accept
int recv_fd=accept(ready_fd,NULL,NULL);
if(recv_fd==-1){
INFO("err:accept");
return -1;
}else{
INFO("succeed:accept");
}
ret=add_fd_into_epfd(epfd,recv_fd);
if(ret==-1) return -1;
}else{
//recv data
ret=rm_fd_from_epfd(epfd,ready_fd);
if(ret==-1) return -1;
ret=recv_data(ready_fd);
if(ret==-1){
return -1;
}
printf("--------------------------------------------\n");
}
}
}
delete []ready_events;
return 0;
}
再运行client:
#include"stdio.h"
#include"stdlib.h"
#include<sys/socket.h>
#include<netinet/in.h>
#include<netinet/tcp.h>
#include<fcntl.h>
#include<string.h>
#include<arpa/inet.h>
#include<unistd.h>
#define SOCKET_SND_BUF_SIZE (1024*1024)
#define SOCKET_RCV_BUF_SIZE (1024*1024)
void INFO(const char *s){
printf("%s\n",s);
}
int main(int argc,char *argv[]){
//create socket
int fd=socket(PF_INET,SOCK_STREAM,0);
if(fd==-1){
INFO("err:create socket");
return -1;
}else{
INFO("create socket succeed");
}
//connect
int port=8012;
sockaddr_in addr;
memset(&addr,0,sizeof addr);
addr.sin_family=AF_INET;
addr.sin_addr.s_addr=inet_addr("10.134.89.204");
addr.sin_port=htons(port);
int ret=connect(fd,(struct sockaddr *)&addr,sizeof(struct sockaddr));
if(ret==-1){
INFO("err:building connect");
return -1;
}else{
INFO("building connect succeed");
}
//send data
char buf[4096];
char s[4096];
while(true){
printf("sending: ");
scanf("%s",s);
if(strcmp(s,"over")==0) break;
sprintf(buf,s);
int len=send(fd,buf,strlen(buf),0);
if(len<0){
INFO("err:send data");
return -1;
}else{
//printf("succeed! sending data:'%s',bytes:%d\n",buf,len);
}
}
return 0;
}
运行结果:
client:
server: