-
/*点对点server.c*/
-
#include<stdio.h>
-
#include<sys/types.h>
-
#include<string.h>
-
#include<sys/socket.h>
-
#include<unistd.h>
-
#include<stdlib.h>
-
#include<errno.h>
-
#include<netinet/in.h>
-
#include<arpa/inet.h>
-
#include<signal.h>/**/
-
void die(char *s)
-
{perror(s);
-
exit(0);
-
}
-
void handler(int sig)
-
{
-
printf("recv a sig=%d\n",sig);
-
exit(0);
-
}
-
int main(void)
-
{ /*socket*/
-
int listenfd;
-
if((listenfd=socket(AF_INET,SOCK_STREAM,0))<0)
-
die("socket");
-
/*bind*/
-
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);
-
/*servaddr.sin_addr.s_addr=inet_addr("127.0.0.1"");*/
-
/* if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0) */
-
int yes=1;
-
if(setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
-
die("reuaddr");
-
-
if(bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
-
die("bind");
-
/*listen*/
-
if(listen(listenfd,SOMAXCONN)<0)
-
die("listen");
-
/*accept*/
-
struct sockaddr_in peeraddr;
-
socklen_t peerlen=sizeof(peeraddr);
-
int conn;
-
if((conn=accept(listenfd,(struct sockaddr*)&peeraddr,&peerlen))<0)
-
die("accept");
-
printf("ip=%s port=%d\n",inet_ntoa(peeraddr.sin_addr),ntohs(peeraddr.sin_port));
-
/*chat function*/
-
pid_t pid;
-
pid=fork();
-
if(pid==-1)
-
die("fork");
-
if(pid==0)
-
{ /*send process*/
-
signal(SIGUSR1,handler);
-
char sendbuf[1024]={0};
-
while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL)
-
{
-
write(conn,sendbuf,strlen(sendbuf));
-
memset(sendbuf,0,sizeof(sendbuf));
-
}
-
printf("child close\n");
-
exit(0);
-
}
-
else{
-
/*recv process*/
-
char recvbuf[1024];
-
while(1)
-
{ memset(recvbuf,0,sizeof(recvbuf));
-
int ret=read(conn,recvbuf,sizeof(recvbuf));
-
if(ret==-1)
-
die("read");
-
else if(ret==0){
-
printf("peer close\n");
-
break;
-
}
-
fputs(recvbuf,stdout);
-
}
-
printf("parent close\n");
kill(pid,SIGUSR1);
exit(0);
}
/*close socket*/
close(conn);
close(listenfd);
return 0;
}
-
/*点对点client.c*/
-
#include<stdio.h>
-
#include<sys/types.h>
-
#include<string.h>
-
#include<sys/socket.h>
-
#include<unistd.h>
-
#include<stdlib.h>
-
#include<errno.h>
-
#include<netinet/in.h>
-
#include<arpa/inet.h>
-
#include<signal.h>
-
void die(char *s)
-
{perror(s);
-
exit(0);
-
}
-
void handler(int sig)
-
{
-
printf("recv a sig=%d\n",sig);
-
exit(0);
-
}
-
int main(void)
-
{
-
/*socket*/
-
int sock;
-
if((sock=socket(AF_INET,SOCK_STREAM,0))<0)
-
die("socket");
-
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); */
-
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
-
/*connect*/
-
if(connect(sock,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
-
die("connect");
-
pid_t pid;
-
pid=fork();
-
if(pid==-1)
-
die("fork");
-
/*recv process*/
-
if(pid==0)
-
{
-
char recvbuf[1024];
-
while(1)
-
{
-
memset(recvbuf,0,sizeof(recvbuf));
-
int ret=read(sock,recvbuf,sizeof(recvbuf));
-
if(ret==-1)
-
die("read");
-
else if(ret==0)
-
{printf("peer close\n");
-
break;}
-
fputs(recvbuf,stdout);
-
}
-
close(sock);
-
kill(getppid(),SIGUSR1);
-
}
-
/*send process*/
-
else {
-
signal(SIGUSR1,handler);
-
char sendbuf[1024]={0};
-
while(fgets(sendbuf,sizeof(sendbuf),stdin)!=NULL)
-
{
-
write(sock,sendbuf,strlen(sendbuf));
-
memset(sendbuf,0,sizeof(sendbuf));
-
}
-
}
-
-
-
return 0;
-
}
阅读(807) | 评论(0) | 转发(0) |