1、将广播发送和接收端实现一遍,完成一个发送端发送信息,对应多个接收端接收信息实验。
接受端
# include <myhead.h>
# define handel_err ( res, val) if ( val== - 1 ) { perror ( res) ; return - 1 ; }
int main ( int argc, const char * argv[ ] )
{ int rfd= socket ( AF_INET, SOCK_DGRAM, 0 ) ; handel_err ( "socket" , rfd) ; struct sockaddr_in rin= { . sin_family= AF_INET, . sin_port= htons ( 6666 ) , . sin_addr. s_addr= inet_addr ( "255.255.255.255" ) } ; int res= bind ( rfd, ( struct sockaddr * ) & rin, sizeof ( rin) ) ; handel_err ( "bind" , res) ; struct sockaddr_in client; socklen_t len= sizeof ( client) ; char buf[ 1024 ] = "" ; while ( 1 ) { bzero ( buf, sizeof ( buf) ) ; recvfrom ( rfd, buf, sizeof ( buf) , 0 , ( struct sockaddr * ) & client, & len) ; printf ( "[%s:%d]->%s\n" , inet_ntoa ( client. sin_addr) , ntohs ( client. sin_port) , buf) ; } return 0 ;
}
发送端
# include <myhead.h>
# define handel_err ( res, val) if ( val== - 1 ) { perror ( res) ; return - 1 ; }
int main ( int argc, const char * argv[ ] )
{ int oldfd= socket ( AF_INET, SOCK_DGRAM, 0 ) ; handel_err ( "oldfd" , oldfd) ; int k= 1 ; setsockopt ( oldfd, SOL_SOCKET, SO_BROADCAST, & k, sizeof ( k) ) ; printf ( "广播设置成功\n" ) ; struct sockaddr_in broadcast= { . sin_family= AF_INET, . sin_port= htons ( 6666 ) , . sin_addr. s_addr= inet_addr ( "255.255.255.255" ) } ; char buf[ 1024 ] ; while ( 1 ) { fgets ( buf, sizeof ( buf) , stdin ) ; buf[ strlen ( buf) - 1 ] = 0 ; sendto ( oldfd, buf, sizeof ( buf) , 0 , ( struct sockaddr * ) & broadcast, sizeof ( broadcast) ) ; } close ( oldfd) ; return 0 ;
}
2、使用多线程基于TCP协议的并发执行,一个服务器对应多个客户端实现通信实验。
客户端
# include <myhead.h>
# define handel_err ( res, val) if ( val== - 1 ) { perror ( res) ; return - 1 ; }
int main ( int argc, const char * argv[ ] )
{ int cfd= socket ( AF_INET, SOCK_STREAM, 0 ) ; handel_err ( "socket" , cfd) ; struct sockaddr_in server= { . sin_family= AF_INET, . sin_port= htons ( 6666 ) , . sin_addr. s_addr= inet_addr ( "192.168.238.227" ) } ; int res= connect ( cfd, ( struct sockaddr * ) & server, sizeof ( server) ) ; handel_err ( "connect" , res) ; char buf[ 1024 ] = "" ; while ( 1 ) { printf ( "请输入>>" ) ; fgets ( buf, sizeof ( buf) , stdin ) ; buf[ strlen ( buf) - 1 ] = 0 ; send ( cfd, buf, sizeof ( buf) , 0 ) ; recv ( cfd, buf, sizeof ( buf) , 0 ) ; } return 0 ;
}
服务器
# include <myhead.h>
# define handel_err ( res, val) if ( val== - 1 ) { perror ( res) ; return - 1 ; }
struct Info { int cfd; struct sockaddr_in client; socklen_t client_len;
} ;
void * fun ( void * n) { struct sockaddr_in client= ( * ( struct Info * ) n) . client; socklen_t client_len= ( * ( struct Info * ) n) . client_len; int cfd= ( * ( struct Info * ) n) . cfd; char buf[ 1024 ] = "" ; while ( 1 ) { int res= recv ( cfd, buf, sizeof ( buf) , 0 ) ; if ( res== 0 ) { printf ( "下线\n" ) ; close ( cfd) ; break ; } printf ( "[%s:%d]->%s\n" , inet_ntoa ( client. sin_addr) , htons ( client. sin_port) , buf) ; send ( cfd, "收到\n" , sizeof ( "收到\n" ) , 0 ) ; }
}
int main ( int argc, const char * argv[ ] )
{ int sfd= socket ( AF_INET, SOCK_STREAM, 0 ) ; handel_err ( "socket" , sfd) ; int k= 1 ; setsockopt ( sfd, SOL_SOCKET, SO_REUSEADDR, & k, sizeof ( k) ) ; struct sockaddr_in server= { . sin_family= AF_INET, . sin_port= htons ( 6666 ) , . sin_addr. s_addr= inet_addr ( "192.168.238.227" ) } ; int res= bind ( sfd, ( struct sockaddr * ) & server, sizeof ( server) ) ; handel_err ( "bind " , res) ; res= listen ( sfd, 10 ) ; handel_err ( "listen" , res) ; struct sockaddr_in client; socklen_t client_len= sizeof ( client) ; while ( 1 ) { pthread_t tid; int cfd= accept ( sfd, ( struct sockaddr * ) & client, & client_len) ; printf ( "success\n" ) ; handel_err ( "accept" , cfd) ; struct Info client_info= { . cfd= cfd, . client= client, . client_len= client_len} ; pthread_create ( & tid, NULL , fun, & client_info) ; pthread_detach ( tid) ; } return 0 ;
}