08 Linux进程间通信网络套接字socket Demo
jim@ubuntu:/mnt/hgfs/git_win10/many-repositories/24_Linux套接字SocketDemo$ make
gcc -o server server.c
gcc -o client client.c
jim@ubuntu:/mnt/hgfs/git_win10/many-repositories/24_Linux套接字SocketDemo$ make run
./server &
sleep 1
waitting for client connect.. .
./client
waitting for client send data.. .
client--> server[ 24 ] : Client say hello world!
client<= = server[ 16 ] : server received
client--> server[ 18 ] : My name is client
client<= = server[ 16 ] : server received
client--> server[ 29 ] : e( exit) . I will close server
jim@ubuntu:/mnt/hgfs/git_win10/many-repositories/24_Linux套接字SocketDemo$
Makefile:
default:
gcc -o server server.c
gcc -o client client.c
run:
./server &
sleep 1
./client
clean:
rm server client
comm.h:
# ifndef _COMM_H_
# define _COMM_H_
# define SOCKET_FILE "/tmp/linux.socket"
# define RECV_BUF_SIZE 1024
# endif
server.c:
# include <sys/socket.h>
# include <sys/un.h>
# include <stdio.h>
# include <string.h>
# include <unistd.h>
# include "comm.h"
# define SERVER_STRING "server received"
static char recv_buf[ RECV_BUF_SIZE] ;
int main ( )
{
struct sockaddr_un srv_addr;
struct sockaddr_un clt_addr;
int listen_fd, connect_fd;
int ret;
listen_fd = socket ( AF_UNIX, SOCK_STREAM, 0 ) ;
if ( listen_fd < 0 ) {
perror ( "socket" ) ;
return - 1 ;
}
srv_addr. sun_family = AF_UNIX;
strcpy ( srv_addr. sun_path, SOCKET_FILE) ;
unlink ( SOCKET_FILE) ;
ret = bind ( listen_fd, ( struct sockaddr * ) & srv_addr, sizeof ( srv_addr) ) ;
if ( ret < 0 ) {
perror ( "bind" ) ;
goto exit1;
}
printf ( "waitting for client connect...\n" ) ;
# define LISTEN_MAX_NUM 8
ret = listen ( listen_fd, LISTEN_MAX_NUM) ;
if ( ret < 0 ) {
perror ( "listen" ) ;
goto exit1;
}
int clt_addr_len = sizeof ( clt_addr) ;
connect_fd = accept ( listen_fd, ( struct sockaddr * ) & clt_addr, & clt_addr_len) ;
if ( ret < 0 ) {
perror ( "accept" ) ;
goto exit1;
}
printf ( "waitting for client send data...\n" ) ;
int len;
while ( 1 ) {
len = read ( connect_fd, recv_buf, sizeof ( recv_buf) ) ;
if ( len > 0 ) {
recv_buf[ len] = '\0' ;
printf ( "client-->server[%d]: %s\n" , len, recv_buf) ;
write ( connect_fd, SERVER_STRING, sizeof ( SERVER_STRING) ) ;
if ( recv_buf[ 0 ] == 'e' )
goto exit2;
}
}
exit2:
close ( connect_fd) ;
exit1:
unlink ( SOCKET_FILE) ;
close ( listen_fd) ;
return 0 ;
}
client.c:
# include <sys/socket.h>
# include <sys/un.h>
# include <stdio.h>
# include <string.h>
# include <unistd.h>
# include "comm.h"
# define CLIENT_STRING1 "Client say hello world!"
# define CLIENT_STRING2 "My name is client"
# define CLIENT_STRING3 "e(exit). I will close server"
static char recv_buf[ RECV_BUF_SIZE] ;
int main ( void )
{
struct sockaddr_un srv_addr;
struct sockaddr_un clt_addr;
int connect_fd;
int ret, len;
connect_fd = socket ( AF_UNIX, SOCK_STREAM, 0 ) ;
if ( connect_fd < 0 ) {
perror ( "socket" ) ;
return - 1 ;
}
srv_addr. sun_family = AF_UNIX;
strcpy ( srv_addr. sun_path, SOCKET_FILE) ;
ret = connect ( connect_fd, ( struct sockaddr * ) & srv_addr, sizeof ( srv_addr) ) ;
if ( ret < 0 ) {
perror ( "bind" ) ;
goto exit;
}
write ( connect_fd, CLIENT_STRING1, sizeof ( CLIENT_STRING1) ) ;
len = read ( connect_fd, recv_buf, sizeof ( recv_buf) ) ;
if ( len > 0 ) {
recv_buf[ len] = '\0' ;
printf ( "client<==server[%d]: %s\n" , len, recv_buf) ;
}
write ( connect_fd, CLIENT_STRING2, sizeof ( CLIENT_STRING2) ) ;
len = read ( connect_fd, recv_buf, sizeof ( recv_buf) ) ;
if ( len > 0 ) {
recv_buf[ len] = '\0' ;
printf ( "client<==server[%d]: %s\n" , len, recv_buf) ;
}
write ( connect_fd, CLIENT_STRING3, sizeof ( CLIENT_STRING3) ) ;
exit:
close ( connect_fd) ;
return 0 ;
}