客户端
#include <iostream>
#include <arpa/inet.h>
#include <unistd.h>
#include <strings.h>
#include <thread>
#include <sys/epoll.h>
#include <linux/fs.h>
using namespace std;
string name;
void show_connect(int fd) {
struct sockaddr_in local_addr;
socklen_t local_addr_len = sizeof(local_addr);
bzero(&local_addr,local_addr_len);
getsockname(fd,(struct sockaddr*)&local_addr,&local_addr_len);
cout << "local " << inet_ntoa(local_addr.sin_addr) << ":" << ntohs(local_addr.sin_port) << endl;
struct sockaddr_in remote_addr;
socklen_t remote_addr_len = sizeof(remote_addr);
bzero(&remote_addr,remote_addr_len);
getpeername(fd,(struct sockaddr*)&remote_addr,&remote_addr_len);
cout << "remote " << inet_ntoa(remote_addr.sin_addr) << ":" << ntohs(remote_addr.sin_port) << endl;
}
int main(int argc,char* argv[]) {
if(4!=argc) {
cout << "Usage:" << argv[0] << " IP port name" << endl;
return 1;
}
name = argv[3];
int connfd = socket(AF_INET,SOCK_STREAM,0);
if(-1 == connfd) {
cout << "socket error" << endl;
return 1;
}
struct sockaddr_in remote_addr;
remote_addr.sin_family = AF_INET;
remote_addr.sin_addr.s_addr = inet_addr(argv[1]);
remote_addr.sin_port = htons(atoi(argv[2]));
if(-1==connect(connfd,(struct sockaddr*)&remote_addr,sizeof(remote_addr))) {
cout << "connect error" << endl;
return 1;
} else {
cout << "connect success" << endl;
cout << inet_ntoa(remote_addr.sin_addr) << ":" << ntohs(remote_addr.sin_port) << endl;
show_connect(connfd);
}
int epollfd = epoll_create(2);
struct epoll_event evt;
evt.data.fd = STDIN_FILENO;
evt.events = EPOLLIN;
epoll_ctl(epollfd,EPOLL_CTL_ADD,STDIN_FILENO,&evt);
evt.data.fd = connfd;
evt.events = EPOLLIN;
epoll_ctl(epollfd,EPOLL_CTL_ADD,connfd,&evt);
bool stop = false;
while(!stop) {
int count = 2;
struct epoll_event revt[2];
int revt_count = epoll_wait(epollfd,revt,count,-1);
for(int i=0;i<revt_count;++i){
if(revt[i].data.fd == STDIN_FILENO && revt[i].events & EPOLLIN){
string message;
getline(cin,message);
message = name + ":" + message;
write(connfd,message.c_str(),message.size()+1);
}else if(revt[i].data.fd == connfd && revt[i].events & EPOLLIN){
char buffer[1024] = {0};
int len = read(connfd,buffer,sizeof(buffer));
if(len == 0) {
cout << "server exit" << endl;
epoll_ctl(epollfd,EPOLL_CTL_DEL,connfd,revt+i);
--count;
stop = true;
break;
} else {
cout << buffer<< endl;
}
}
}
}
close(epollfd);
close(connfd);
return 0;
}
服务端
#include <iostream>
#include <arpa/inet.h>
#include <unistd.h>
#include <strings.h>
#include <pthread.h>
#include <list>
#include <thread>
#include <algorithm>
#include <sys/epoll.h>
#include <linux/fs.h>
using namespace std;
void show_connect(int fd) {
struct sockaddr_in local_addr;
socklen_t local_addr_len = sizeof(local_addr);
bzero(&local_addr,local_addr_len);
getsockname(fd,(struct sockaddr*)&local_addr,&local_addr_len);
cout << "local " << inet_ntoa(local_addr.sin_addr) << ":" << ntohs(local_addr.sin_port) << endl;
struct sockaddr_in remote_addr;
socklen_t remote_addr_len = sizeof(remote_addr);
bzero(&remote_addr,remote_addr_len);
getpeername(fd,(struct sockaddr*)&remote_addr,&remote_addr_len);
cout << "remote " << inet_ntoa(remote_addr.sin_addr) << ":" << ntohs(remote_addr.sin_port) << endl;
}
int main(int argc,char* argv[]) {
if(3!=argc) {
cout << "Usage:" << "输入 IP port" << endl;
return 1;
}
int listenfd = socket(AF_INET,SOCK_STREAM,0);
if(-1 == listenfd) {
cout << "listen socket error" << endl;
return 1;
}
int flag = 1;
setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&flag,sizeof(flag));
struct sockaddr_in local_addr;
local_addr.sin_family = AF_INET;
local_addr.sin_addr.s_addr = inet_addr(argv[1]);
local_addr.sin_port = htons(atoi(argv[2]));
if(-1 == bind(listenfd,(struct sockaddr*)&local_addr,sizeof(local_addr))) {
cout << "bind error" << endl;
return 1;
} else {
cout << "bind success" << endl;
}
if(-1==listen(listenfd,10)) {
cout << "listen error" << endl;
return 1;
} else {
cout << "listen success" << endl;
}
int epollfd = epoll_create(INR_OPEN_MAX);
struct epoll_event evt;
evt.data.fd = STDIN_FILENO;
evt.events = EPOLLIN;
epoll_ctl(epollfd,EPOLL_CTL_ADD,STDIN_FILENO,&evt);
evt.data.fd = listenfd;
epoll_ctl(epollfd,EPOLL_CTL_ADD,listenfd,&evt);
int count = 2;
list<int> fds;
while(true) {
struct epoll_event revt[count];
int revt_cnt = epoll_wait(epollfd,revt,count,-1);
for(int i=0; i<revt_cnt; ++i) {
if(revt[i].data.fd == STDIN_FILENO && revt[i].events & EPOLLIN) {
string message;
getline(cin,message);
cin.clear();
if(!message.empty()) {
message = "广告:" + message;
for(auto fd:fds) {
write(fd,message.c_str(),message.size()+1);
}
}
}else if(revt[i].data.fd == listenfd && revt[i].events & EPOLLIN) {
struct sockaddr_in remote_addr;
bzero(&remote_addr,sizeof(remote_addr));
socklen_t remote_addr_len = sizeof(remote_addr);
int connfd = accept(listenfd,(struct sockaddr*)&remote_addr,&remote_addr_len);
if(-1 == connfd) {
cout << "accept error" << endl;
return 1;
} else {
cout << "accept success" << endl;
cout << inet_ntoa(remote_addr.sin_addr) << ":" << ntohs(remote_addr.sin_port) << endl;
show_connect(connfd);
evt.data.fd = connfd;
evt.events = EPOLLIN;
epoll_ctl(epollfd,EPOLL_CTL_ADD,connfd,&evt);
fds.push_back(connfd);
++count;
}
} else {
int connfd = revt[i].data.fd;
char buffer[1024] = {0};
int n = read(connfd,buffer,1024);
if(n == 0) {
cout << "client exit" << endl;
close(connfd);
epoll_ctl(epollfd,EPOLL_CTL_DEL,connfd,revt+i);
fds.remove(connfd);
--count;
break;
} else {
for(auto fd:fds) {
if(fd == connfd) continue;
write(fd,buffer,1024);
}
}
}
}
}
close(epollfd);
close(listenfd);
}
|