Function :
1.The client can write to the server and also the server can write to the client.
2. Client and server both of them can response to the each other's off line(with signal designed by yourself SIGUSR1 )
Server :
/************************************************************************
> file name: p2pserv.c
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: Thu 29 Oct 16:38:57 2015
************************************************************************/
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#define ERR_EXIT(m) \
do \
{\
perror(m) ;\
exit(EXIT_FAILURE) ;\
}while(0)
void handler ( int sig )
{
printf ("recv a sig= %d\n" , sig ) ;
exit ( EXIT_SUCCESS ) ;
}
int
main ()
{
int listenfd ;
if (( listenfd = socket ( PF_INET , SOCK_STREAM , IPPROTO_TCP )) < 0 )
{
ERR_EXIT("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) ; //INTADDR_ANY == 0
//other two means to change the host address to network address
//servaddr.sin_addr.s_addr = inet_addr("127.0.0.1") ;
//inet_aton("127.0.0.1" , &servaddr.sin_addr) ;
/* set out the problem that the address has already been used by SO_REUSEADDR
int on = 1 ;
int opt ;
if (( opt = setsockopt (listenfd ,SOL_SOCKET , SO_REUSEADDR, &on , sizeof(on) )) < 0 )
{
ERR_EXIT("setsocketopt") ;
}
*/
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("bind") ;
}
if ( listen ( listenfd , SOMAXCONN) < 0 )
{
ERR_EXIT("listen") ;
}
struct sockaddr_in peeraddr ;
socklen_t peerlen = sizeof(peeraddr) ;
int conn ;
if ( (conn = accept ( listenfd , ( struct sockaddr * )&peeraddr , &peerlen )) < 0 )
{
ERR_EXIT("accept") ;
}
// print the client's address and ip '
printf ("ip = %s port = %d\n", inet_ntoa(peeraddr.sin_addr) ,ntohs(peeraddr.sin_port) ) ;
pid_t pid ;
pid = fork() ; // creat tow processes , one is to send message , the other is to write message
if ( -1 == pid )
{
ERR_EXIT("fork") ;
}
if( 0 == pid ) // the son's process is to send message
{
signal (SIGUSR1 , handler ) ; // add siagnal to notify the son's process to quit when the father's process quit.
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 ( EXIT_SUCCESS ) ;
}
else // the father's procss is to get(read) message
{
char recvbuf[1024] ;
while(1)
{
memset ( recvbuf , 0 , sizeof(recvbuf) ) ;
int ret = read ( conn, recvbuf , sizeof(recvbuf) ) ;
if ( -1 == ret )
{
ERR_EXIT("read") ;
}
else if ( 0 == ret )
{
printf ("peer close\n") ;
break ;
}
fputs ( recvbuf , stdout ) ;
}
kill ( pid , SIGUSR1 ) ;
printf ("parent close\n") ;
exit (EXIT_SUCCESS ) ;
}
return 0 ;
}
Client :
/************************************************************************
>file name : p2pcli.c
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: Thu 29 Oct 16:38:57 2015
************************************************************************/
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#define ERR_EXIT(m) \
do \
{\
perror(m) ;\
exit(EXIT_FAILURE) ;\
}while(0)
void handler ( int sig )
{
printf ("recv a sig=%d\n" , sig ) ;
exit ( EXIT_SUCCESS ) ;
}
int
main ()
{
int sock ;
if (( sock= socket ( PF_INET , SOCK_STREAM , IPPROTO_TCP )) < 0 )
{
ERR_EXIT("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") ;
//inet_aton("127.0.0.1" , &servaddr.sin_addr) ;
if ( connect ( sock , ( struct sockaddr * )& servaddr, sizeof(servaddr )) < 0 )
{
ERR_EXIT("connet") ;
}
pid_t pid ;
pid = fork() ;
if ( -1 == pid )
{
ERR_EXIT("fork") ;
}
if ( 0 == pid ) // the son's process is to get message and print
{
char recvbuf[1024] ;
while(1)
{
memset ( recvbuf , 0 , sizeof(recvbuf) ) ;
int ret = read ( sock , recvbuf , sizeof(recvbuf) ) ;
if ( -1 == ret )
{
ERR_EXIT("read") ;
}
else if ( 0 == ret )
{
printf ("peer close\n") ;
break ;
}
fputs ( recvbuf , stdout ) ;
}
close(sock) ;
kill ( getppid() , SIGUSR1 ) ; //when the son's process gonna quit tell the father's process
}
else // the father's process is to send message
{
signal ( SIGUSR1 , handler ) ;
char sendbuf[1024] = {0} ;
while( fgets ( sendbuf , sizeof(sendbuf) , stdin ) != NULL )
{
write ( sock , sendbuf , sizeof(sendbuf) ) ;
memset ( sendbuf , 0 , sizeof(sendbuf) ) ;
}
close(sock) ;
}
return 0 ;
}