红联Linux门户
Linux帮助

Linux网络编程之简易的点对点聊天程序

发布时间:2016-08-08 09:17:12来源:linux网站作者:空灵花
本程序基于上一篇echo_server(http://www.linuxdiyf.com/linux/23013.html)的实现,稍作修改,增加了发送消息,服务端客户端各自运行两个进程,一个进程负责发送消息,另一个进程负责接收消息。
 
代码如下:
 
p2p_server.c:
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <memory.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#define ERR_EXIT(m) \
do{ \
perror(m);   \
exit(EXIT_FAILURE);\
}while(0)
int main(int argc, char *argv[])
{
int socket_fd;
if((socket_fd=socket(PF_INET,SOCK_STREAM,0))==-1)
{
ERR_EXIT("socket");
}
struct sockaddr_in servaddr;
memset(&servaddr,0,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(5678);
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
/*
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
inet_aton("127.0.0.1",&servaddr.sin_addr);
*/
int on=1;
if(setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,&on,sizeof(on))==-1){
ERR_EXIT("setsockopt");
}
if(bind(socket_fd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1)
ERR_EXIT("bind");
if(listen(socket_fd,SOMAXCONN)==-1)
ERR_EXIT("listen");
struct sockaddr_in peeraddr;
memset(&peeraddr,0,sizeof(peeraddr));
socklen_t len=sizeof(peeraddr);
int conn;
if((conn=accept(socket_fd,(struct sockaddr*)&peeraddr,&len))==-1)
ERR_EXIT("accept");
printf("ip:%s\tport:%d\n",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port));
pid_t pid;
pid=fork();
if(pid<0){
ERR_EXIT("fork");
}
else if(pid==0){
//child process receive message
close(socket_fd);
char recebuf[1024]={};
while(1){
memset(recebuf,0,sizeof(recebuf));
int rlen=read(conn,recebuf,sizeof(recebuf));
if(rlen==-1){
ERR_EXIT("receive");
}
else if(rlen==0){
printf("close:\n");
close(conn);
exit(EXIT_SUCCESS);
}
else{
fputs(recebuf,stdout);
write(conn,recebuf,rlen);
}
}
close(conn);
}else{
//parent process send message
char sendbuf[1024]={};
while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL){
write(conn,sendbuf,sizeof(sendbuf));
fputs(sendbuf,stdout);
memset(sendbuf,0,sizeof(sendbuf));
}
close(conn);
close(socket_fd);
}
return 0;
}
 
p2p_client.c:
#include <sys/socket.h>
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <memory.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#define ERR_EXIT(m)\
do{\
perror(m);\
exit(EXIT_FAILURE);\
}  while(0)
int main(int argc, char *argv[])
{
if(argc<2){
ERR_EXIT("too little arguments");
}
int socket_fd;
if((socket_fd=socket(AF_INET,SOCK_STREAM,0))==-1){
ERR_EXIT("socket");
}
struct sockaddr_in servaddr;
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(5678);
servaddr.sin_addr.s_addr=inet_addr(argv[1]);
if(connect(socket_fd,(struct sockaddr*)&servaddr,sizeof(servaddr))==-1){
ERR_EXIT("connect");
}
pid_t pid;
pid=fork();
if(pid==-1){
ERR_EXIT("fork");
}
else if(pid==0)
{
//child process receive message
char recvbuf[1024]={};
int ret;
while(1){
ret=read(socket_fd,recvbuf,sizeof(recvbuf));
if(ret<0)
{
ERR_EXIT("read failure");
}
else if(ret==0){
printf("close\n");
exit(EXIT_SUCCESS);
}else{
fputs(recvbuf,stdout);
}
memset(recvbuf,0,sizeof(recvbuf));
}
}else {
//parent process send message
char sendbuf[1024]={};
while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL){
write(socket_fd,sendbuf,strlen(sendbuf));
memset(sendbuf,0,sizeof(sendbuf));
}
}
close(socket_fd);
return 0;
}
 
运行截图:
Linux网络编程之简易的点对点聊天程序
 
本文永久更新地址:http://www.linuxdiyf.com/linux/23128.html