基于TCP的本地通信(客户机): 创建流程: 一、创建字节流式套接字(socket函数):   
	int  sock_fd =  socket ( AF_LOCAL, SOCK_STREAM, 0 ) ; 
  
二、创建客户机和服务器的本地网络信息结构体并填充客户机和服务器本地网络信息结构体 (struct sockaddr_un): 本地网络信息结构体:   
	# include  <sys/un.h> struct  sockaddr_un  { sa_family_t  sun_family;                char         sun_path[ 108 ] ;             } ; 
  
	struct  sockaddr_un  serveraddr,  clientaddr; socklen_t  serveraddr_len =  sizeof ( serveraddr) ; socklen_t  clientaddr_len =  sizeof ( clientaddr) ; memset ( & serveraddr,  0 ,  serveraddr_len) ; memset ( & clientaddr,  0 ,  clientaddr_len) ; serveraddr. sun_family =  AF_LOCAL; clientaddr. sun_family =  AF_LOCAL; strcpy ( serveraddr. sun_path,  "./tcpserver" ) ;   strcpy ( clientaddr. sun_path,  "./tcpclient" ) ;  
  
 
	bind ( sock_fd,  ( struct  sockaddr  * ) & clientaddr,  clientaddr_len) ; 
  
 
	connect ( sock_fd,  ( struct  sockaddr  * ) & serveraddr,  serveraddr_len) ; 
  
五、客户机端发收数据(send函数、recv函数):   
		memset ( buf,  0 ,  sizeof ( buf) ) ; fgets ( buf, sizeof ( buf) , stdin ) ; buf[ strlen ( buf)  -  1 ]  =  '\0' ; int  ret1 =  send ( sock_fd, buf, sizeof ( buf) , 0 ) ; if ( - 1  ==  ret1) { perror ( "send error" ) ; exit ( - 1 ) ; } memset ( buf, 0 , sizeof ( buf) ) ; int  ret2 =  recv ( sock_fd, buf, sizeof ( buf) , 0 ) ; if ( - 1  ==  ret2) { perror ( "recv error" ) ; exit ( - 1 ) ; } printf ( "服务器的应答消息[%s]\n" , buf) ; 
  
 
	close ( sock_fd) ; 
  
 
	# include  <stdio.h> # include  <string.h> # include  <stdlib.h> # include  <unistd.h> # include  <sys/socket.h> # include  <sys/types.h> # include  <netinet/ip.h> # include  <sys/un.h> # include  <arpa/inet.h> # include  <stdbool.h> int  main ( int  argc,  char  const  * argv[ ] ) { int  sock_fd =  socket ( AF_LOCAL,  SOCK_STREAM,  0 ) ; if  ( - 1  ==  sock_fd) { perror ( "socket error" ) ; exit ( - 1 ) ; } struct  sockaddr_un  serveraddr,  clientaddr; socklen_t  serveraddr_len =  sizeof ( serveraddr) ; socklen_t  clientaddr_len =  sizeof ( clientaddr) ; memset ( & serveraddr,  0 ,  serveraddr_len) ; memset ( & clientaddr,  0 ,  clientaddr_len) ; serveraddr. sun_family =  AF_LOCAL; clientaddr. sun_family =  AF_LOCAL; strcpy ( serveraddr. sun_path,  "./tcpserver" ) ;   strcpy ( clientaddr. sun_path,  "./tcpclient" ) ;  if  ( - 1  ==  bind ( sock_fd,  ( struct  sockaddr  * ) & clientaddr,  clientaddr_len) ) { perror ( "bind error" ) ; exit ( - 1 ) ; } if  ( - 1  ==  connect ( sock_fd,  ( struct  sockaddr  * ) & serveraddr,  serveraddr_len) ) { perror ( "connect error" ) ; exit ( - 1 ) ; } char  buf[ 128 ]  =  { 0 } ; int  ret1 =  0 ; int  ret2 =  0 ; printf ( "本地通信之TCP客户机成功连接服务器!!!\n" ) ; while  ( true) { memset ( buf,  0 ,  sizeof ( buf) ) ; fgets ( buf, sizeof ( buf) , stdin ) ; buf[ strlen ( buf)  -  1 ]  =  '\0' ; int  ret1 =  send ( sock_fd, buf, sizeof ( buf) , 0 ) ; if ( - 1  ==  ret1) { perror ( "send error" ) ; exit ( - 1 ) ; } memset ( buf, 0 , sizeof ( buf) ) ; int  ret2 =  recv ( sock_fd, buf, sizeof ( buf) , 0 ) ; if ( - 1  ==  ret2) { perror ( "recv error" ) ; exit ( - 1 ) ; } printf ( "服务器的应答消息[%s]\n" , buf) ; } close ( sock_fd) ; return  0 ; }   
 
	本地通信之TCP客户机成功连接服务器! ! ! hello服务器的应答消息[ hello-- -- -- - k] hi服务器的应答消息[ hi-- -- -- - k] I Love China! ! ! 服务器的应答消息[ I Love China! ! ! -- -- -- - k]   
特别注意: strcpy(serveraddr.sun_path,"./tcpserver");和strcpy(clientaddr.sun_path, "./tcpclient");代码段中的tcpserver文件、tcpclient文件是套接字文件;如下所示:   
	srwxrwxr- x 1  linux linux     0  11 月 10  05 : 58  tcpserversrwxrwxr- x 1  linux linux     0  11 月 10  06 : 00  tcpclient