The simplest ech client-server model.
Function :
The client write something on the STDIN , and the server print them on the screen and write them back to the client.
Server:
/*************************************************************************
> File Name: echoserv.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>
#define ERR_EXIT(m) \
do \
{\
perror(m) ;\
exit(EXIT_FAILURE) ;\
}while(0)
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") ;
}
*/
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) ) ;
char recvbuf[1024] ;
while (1)
{
//printf ("111111111\n") ;
memset ( recvbuf , 0 , sizeof(recvbuf) ) ;
read ( conn , recvbuf , sizeof(recvbuf) ) ;
fputs ( recvbuf , stdout ) ;
//printf ("Hello world!") ;
write ( conn , recvbuf , strlen(recvbuf) ) ;
}
close ( conn ) ;
close ( listenfd ) ;
return 0 ;
}
Client:
/*************************************************************************
> File Name: echocli.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>
#define ERR_EXIT(m) \
do \
{\
perror(m) ;\
exit(EXIT_FAILURE) ;\
}while(0)
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") ;
}
char sendbuf[1024] = {0} ;
char recvbuf[1024] = {0} ;
while( fgets ( sendbuf , sizeof(sendbuf) , stdin ) != NULL )
{
write ( sock , sendbuf , sizeof(sendbuf) ) ;
read ( sock , recvbuf , sizeof(recvbuf ) ) ;
fputs ( recvbuf , stdout ) ;
memset ( sendbuf , 0 , sizeof(sendbuf) ) ;
memset ( recvbuf , 0 , sizeof(recvbuf) ) ;
}
close ( sock ) ;
return 0 ;
}