天天看点

Linux以下基于TCP多线程聊天室(client)

    不怎么会弄这个博客的排版,就直接将代码附上:

主要是使用多线程去等待接受数据和发送数据。以下是client的代码:

tcpsed.h文件

1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22      
#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文件

1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99      
#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文件

1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99      
#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 ); }
1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35      
#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 ; }
1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99      
#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

继续阅读