不怎麼會弄這個部落格的排版,就直接将代碼附上:
主要是使用多線程去等待接受資料和發送資料。以下是client的代碼:
tcpsed.h檔案
| #ifndef RTPSED_H #define RTPSED_H #define BUFFSIZE 512 int runcond ; int socketfd ; typedef struct TCP_send_arg { char * tcpserver_addr ; int tcp_port ; } TCP_send_arg_t ; void tcp_stophandler ( int signum ); void * tcppacketsend ( void * arg ); void quit ( int signum ); #endif |
tcpsed.c檔案
| #include "tcpsed.h" #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <sys/param.h> #include <arpa/inet.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/mman.h> #include <pthread.h> int socketfd ; void tcp_stophandler ( int signum ) { runcond = 0 ; } void quit ( int signum ) { close ( socketfd ); printf ( "Bye~Bye! \n " ); pthread_exit ( NULL ); } void * pth_read ( void * arg ) { int runcond = 1 ; int result ; char pth_buf [ BUFFSIZE ]; while ( runcond ) { if ( 0 > ( result = read ( socketfd , pth_buf , BUFFSIZE - 1 ))) { fprintf ( stderr , "Read Error:%s \n " , strerror ( errno )); } pth_buf [ result ] = '\0' ; printf ( "%s \n " , pth_buf ); } pthread_exit ( NULL ); } void * tcppacketsend ( void * arg ) { runcond = 1 ; struct sockaddr_in server_addr ; char buf [ BUFFSIZE ] = { 0 }; int connfd ; pthread_t tid ; TCP_send_arg_t * send_arg = ( TCP_send_arg_t * ) arg ; memset ( & server_addr , 0 , sizeof ( server_addr )); server_addr . sin_family = AF_INET ; server_addr . sin_port = htons (( * send_arg ). tcp_port ); server_addr . sin_addr . s_addr = inet_addr (( * send_arg ). tcpserver_addr ); if ( - 1 == ( socketfd = socket ( AF_INET , SOCK_STREAM , 0 ))) { fprintf ( stderr , "Socket Error:%s \n " , strerror ( errno )); pthread_exit ( NULL ); } if ( - 1 == ( connfd = connect ( socketfd ,( struct sockaddr * ) & server_addr , sizeof ( server_addr )))) { fprintf ( stderr , "Connect Error:%s \n " , strerror ( errno )); pthread_exit ( NULL ); } printf ( "You can use \" Ctrl + D \" to quit. \n " ); if ( SIG_ERR == signal ( SIGUSR1 , quit )) { fprintf ( stderr , "Can't set SIGUSR1 signal action! \n " ); pthread_exit ( NULL ); } if ( 0 != pthread_create ( & tid , NULL ,( void * ) pth_read ,( void * ) buf )) { fprintf ( stderr , "Create pthread Error:%s \n " , strerror ( errno )); pthread_exit ( NULL ); } while ( NULL != ( fgets ( buf , BUFFSIZE , stdin ))) { write ( socketfd , buf , strlen ( buf ) - 1 ); } pthread_cancel ( tid ); pthread_exit ( NULL ); } |
client.c檔案
| #include "tcpsed.h" #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <sys/param.h> #include <arpa/inet.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/mman.h> #include <pthread.h> int socketfd ; void tcp_stophandler ( int signum ) { runcond = 0 ; } void quit ( int signum ) { close ( socketfd ); printf ( "Bye~Bye! \n " ); pthread_exit ( NULL ); } void * pth_read ( void * arg ) { int runcond = 1 ; int result ; char pth_buf [ BUFFSIZE ]; while ( runcond ) { if ( 0 > ( result = read ( socketfd , pth_buf , BUFFSIZE - 1 ))) { fprintf ( stderr , "Read Error:%s \n " , strerror ( errno )); } pth_buf [ result ] = '\0' ; printf ( "%s \n " , pth_buf ); } pthread_exit ( NULL ); } void * tcppacketsend ( void * arg ) { runcond = 1 ; struct sockaddr_in server_addr ; char buf [ BUFFSIZE ] = { 0 }; int connfd ; pthread_t tid ; TCP_send_arg_t * send_arg = ( TCP_send_arg_t * ) arg ; memset ( & server_addr , 0 , sizeof ( server_addr )); server_addr . sin_family = AF_INET ; server_addr . sin_port = htons (( * send_arg ). tcp_port ); server_addr . sin_addr . s_addr = inet_addr (( * send_arg ). tcpserver_addr ); if ( - 1 == ( socketfd = socket ( AF_INET , SOCK_STREAM , 0 ))) { fprintf ( stderr , "Socket Error:%s \n " , strerror ( errno )); pthread_exit ( NULL ); } if ( - 1 == ( connfd = connect ( socketfd ,( struct sockaddr * ) & server_addr , sizeof ( server_addr )))) { fprintf ( stderr , "Connect Error:%s \n " , strerror ( errno )); pthread_exit ( NULL ); } printf ( "You can use \" Ctrl + D \" to quit. \n " ); if ( SIG_ERR == signal ( SIGUSR1 , quit )) { fprintf ( stderr , "Can't set SIGUSR1 signal action! \n " ); pthread_exit ( NULL ); } if ( 0 != pthread_create ( & tid , NULL ,( void * ) pth_read ,( void * ) buf )) { fprintf ( stderr , "Create pthread Error:%s \n " , strerror ( errno )); pthread_exit ( NULL ); } while ( NULL != ( fgets ( buf , BUFFSIZE , stdin ))) { write ( socketfd , buf , strlen ( buf ) - 1 ); } pthread_cancel ( tid ); pthread_exit ( NULL ); } |
| #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <errno.h> #include "tcpsed.h" #define PORT 8888 #define IP "192.168.1.220" TCP_send_arg_t send_arg ; int main ( int argc , char * argv []) { pthread_t tid ; void * tret ; send_arg . tcp_port = PORT ; send_arg . tcpserver_addr = IP ; if ( 0 != pthread_create ( & tid , NULL ,( void * ) tcppacketsend ,( void * ) & send_arg )) { fprintf ( stderr , "Create pthread error:%s \n " , strerror ( errno )); pthread_exit ( NULL ); } printf ( "OK! \n " ); if ( 0 != pthread_join ( tid , & tret )) { fprintf ( stderr , "Join thread error:%s \n " , strerror ( errno )); pthread_exit ( NULL ); } return 0 ; } |
| #include "tcpsed.h" #include <sys/types.h> #include <sys/socket.h> #include <sys/ioctl.h> #include <sys/param.h> #include <arpa/inet.h> #include <errno.h> #include <signal.h> #include <fcntl.h> #include <ctype.h> #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h> #include <sys/mman.h> #include <pthread.h> int socketfd ; void tcp_stophandler ( int signum ) { runcond = 0 ; } void quit ( int signum ) { close ( socketfd ); printf ( "Bye~Bye! \n " ); pthread_exit ( NULL ); } void * pth_read ( void * arg ) { int runcond = 1 ; int result ; char pth_buf [ BUFFSIZE ]; while ( runcond ) { if ( 0 > ( result = read ( socketfd , pth_buf , BUFFSIZE - 1 ))) { fprintf ( stderr , "Read Error:%s \n " , strerror ( errno )); } pth_buf [ result ] = '\0' ; printf ( "%s \n " , pth_buf ); } pthread_exit ( NULL ); } void * tcppacketsend ( void * arg ) { runcond = 1 ; struct sockaddr_in server_addr ; char buf [ BUFFSIZE ] = { 0 }; int connfd ; pthread_t tid ; TCP_send_arg_t * send_arg = ( TCP_send_arg_t * ) arg ; memset ( & server_addr , 0 , sizeof ( server_addr )); server_addr . sin_family = AF_INET ; server_addr . sin_port = htons (( * send_arg ). tcp_port ); server_addr . sin_addr . s_addr = inet_addr (( * send_arg ). tcpserver_addr ); if ( - 1 == ( socketfd = socket ( AF_INET , SOCK_STREAM , 0 ))) { fprintf ( stderr , "Socket Error:%s \n " , strerror ( errno )); pthread_exit ( NULL ); } if ( - 1 == ( connfd = connect ( socketfd ,( struct sockaddr * ) & server_addr , sizeof ( server_addr )))) { fprintf ( stderr , "Connect Error:%s \n " , strerror ( errno )); pthread_exit ( NULL ); } printf ( "You can use \" Ctrl + D \" to quit. \n " ); if ( SIG_ERR == signal ( SIGUSR1 , quit )) { fprintf ( stderr , "Can't set SIGUSR1 signal action! \n " ); pthread_exit ( NULL ); } if ( 0 != pthread_create ( & tid , NULL ,( void * ) pth_read ,( void * ) buf )) { fprintf ( stderr , "Create pthread Error:%s \n " , strerror ( errno )); pthread_exit ( NULL ); } while ( NULL != ( fgets ( buf , BUFFSIZE , stdin ))) { write ( socketfd , buf , strlen ( buf ) - 1 ); } pthread_cancel ( tid ); pthread_exit ( NULL ); } |
轉載于:https://www.cnblogs.com/lxjshuju/p/7025849.html