linux下实现p2p功能
目的:实现客户端与服务器的对等通信
服务器端:
/*************************************************************************
> File Name: test.c
> Author: kinght
> Mail: zhjnight@163.com
> Created Time: Wed 20 Jun 2018 06:58:02 PM PDT
************************************************************************/#include
#include//connect,send,recv,setsockopt等
#include#include // sockaddr_in, "man 7 ip" ,htons
#include //poll,pollfd
#include//inet_addr,inet_aton
#include //read,write
#include //gethostbyname#include //perror
#include
#include //errno#include// memset
#include
#include
#include
#ifdef HAVE_NETINET_IN_H
#include
#endif#define ERR_EXIT(m)\
do\
{\
perror(m);
\
exit(EXIT_FAILURE);
\
}while(0)ssize_t readn(int fd, void *buf, size_t count)
{
size_t nleft = count;
ssize_t nread;
char *bufp = (char*)buf;
while(nleft > 0)
{
if((nread= read(fd,bufp,nleft)< 0))
{
if(errno = EINTR)
{
continue;
}
else
{
return -1;
}
}
else if(nread = 0)
{
return count - nleft;
}
bufp += nread;
nleft -= nread;
}
return count;
}
ssize_t writen(int fd, void *buf, size_t count)
{
size_t nleft = count;
ssize_t nwriteen;
char *bufp = (char*)buf;
while(nleft > 0)
{
if((nwriteen= write(fd,bufp,nleft)< 0))
{
if(errno = EINTR)
{
continue;
}
else
{
return -1;
}
}
else if(nwriteen = 0)
{
continue;
}
bufp += nwriteen;
nleft -= nwriteen;
}
return count;
}
void doServiceConn(int conn)
{
char recvbuf[1024];
while(true)
{
memset(recvbuf,0,sizeof(recvbuf));
int ret = read(conn,recvbuf,sizeof(recvbuf));
if(ret == 0)
{
printf("client close");
break;
}
else if(ret == -1)
{
ERR_EXIT("read");
}write(conn,recvbuf,ret);
printf(recvbuf,stdout);
} }
int main(void)
{
int listenfd;
if((listenfd =socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
{
ERR_EXIT("ERROR");
}
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port = htons(5188);
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
int on = 1;
if(setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))< 0)
{
ERR_EXIT("SETSOCKOPT");
}
if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))< 0)
{
ERR_EXIT("error");
}
if(listen(listenfd,SOMAXCONN)<0)
{
ERR_EXIT("listen");
}
struct sockaddr_in peeraddr;
socklen_t peerlen = sizeof(peeraddr);
int conn;
pid_t pid;
while(1)
{
conn = accept(listenfd,(struct sockaddr*)&peeraddr,&peerlen);
if(conn < 0)
{
ERR_EXIT("accept");
}
printf("ip= %s,port = %d\n",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port));
pid = fork();
if(pid == -1)
ERR_EXIT("fork");
if(pid == 0)
{char sendbuf[1024]={0};
while(fgets(sendbuf,sizeof(sendbuf),stdin)!= NULL)
{
write(conn,sendbuf,strlen(sendbuf));
memset(sendbuf,0,sizeof(sendbuf));
}
exit(EXIT_SUCCESS);
}
else
{
char recvbuf[1024];
while(1)
{
memset(recvbuf,0,sizeof(recvbuf));
int ret= read(conn,recvbuf,sizeof(recvbuf));
if(ret == -1)
ERR_EXIT("read");
else if(ret == 0)
{
printf("peer close");
break;
}fputs(recvbuf,stdout);
}
exit(EXIT_SUCCESS);
}
}return 0;
}
客户端代码:
/*************************************************************************
> File Name: client.c
> Author: kinght
> Mail: zhjnight@163.com
> Created Time: Wed 20 Jun 2018 06:58:02 PM PDT
************************************************************************/#include
#include//connect,send,recv,setsockopt等
#include#include // sockaddr_in, "man 7 ip" ,htons
#include 【linux下实现p2p功能】//poll,pollfd
#include//inet_addr,inet_aton
#include //read,write
#include //gethostbyname#include //perror
#include
#include //errno#include// memset
#include
#include
#include
#ifdef HAVE_NETINET_IN_H
#include
#endif#define ERR_EXIT(m)\
do\
{\
perror(m);
\
exit(EXIT_FAILURE);
\
}while(0)int main(void)
{
int sock;
if((sock =socket(PF_INET,SOCK_STREAM,IPPROTO_TCP))<0)
{
ERR_EXIT("ERROR");
}
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port = htons(5188);
servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
if(connect(sock,(struct sockaddr*)&servaddr,sizeof(servaddr))< 0)
{
ERR_EXIT("connect");
}
pid_t pid;
pid = fork();
if(pid == -1)
{
ERR_EXIT("pid");
}
else if(pid == 0)
{
char recvbuf[1024]={0};
while(1)
{
memset(recvbuf,0,sizeof(recvbuf));
int ret = read(sock,recvbuf,sizeof(recvbuf));
if(ret == -1)
ERR_EXIT("read");
else if(ret == 0)
{
printf("peer close");
}
fputs(recvbuf,stdout);
}
}
else
{
char sendbuf[1024]={0};
while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL)
{
write(sock,sendbuf,strlen(sendbuf));
memset(sendbuf,0,sizeof(sendbuf));
}}
return 0;
}
推荐阅读
- 开学第一天(下)
- 【故障公告】周五下午的一次突发故障
- 生活随笔|好天气下的意外之喜
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- MongoDB,Wondows下免安装版|MongoDB,Wondows下免安装版 (简化版操作)
- 汇讲-勇于突破
- python学习之|python学习之 实现QQ自动发送消息
- Android中的AES加密-下
- 说的真好