ºìÁªLinuxÃÅ»§
Linux°ïÖú

LinuxÉÏʵÏÖSocketµÄ¶à½ø³ÌʵʱͨÐÅ

·¢²¼Ê±¼ä:2006-11-14 10:19:31À´Ô´:ºìÁª×÷Õß:download
¡¡¡¡Ì×½Ó¿Ú£¨Socket£©ÎªÄ¿Ç° Linux ÉÏ×îΪ¹ã·ºÊ¹ÓõÄÒ»ÖֵĽø³Ì¼äͨÐÅ»úÖÆ¡£µ«ÊÇËü²»ÄÜÖ±½ÓÓÃÀ´¶à½ø³ÌÖ®¼äµÄÏ໥ʵʱͨÐÅ¡£±¾ÎÄÌá³öÒ»¸ö»ùÓÚ Socket µÄ¶à½ø³ÌÖ®¼äͨÐŵÄʵÏÖ·½·¨£¬²¢¸ø³öÑù±¾³ÌÐòµÄʵÏÖºÍ˵Ã÷¡£

¡¡¡¡Ì×½Ó¿Ú£¨Socket£©ÎªÄ¿Ç°LinuxÉÏ×îΪ¹ã·ºÊ¹ÓõÄÒ»ÖֵĽø³Ì¼äͨÐÅ»úÖÆ£¬ÓëÆäËûµÄLinuxͨÐÅ»úÖƲ»Í¬Ö®´¦ÔÚÓÚ³ýÁËËü¿ÉÓÃÓÚµ¥»úÄڵĽø³Ì¼äͨÐÅÒÔÍ⣬»¹¿ÉÓÃÓÚ²»Í¬»úÆ÷Ö®¼äµÄ½ø³Ì¼äͨÐÅ¡£µ«ÊÇÓÉÓÚSocket±¾Éí²»Ö§³ÖͬʱµÈ´ýºÍ³¬Ê±´¦Àí£¬ËùÒÔËü²»ÄÜÖ±½ÓÓÃÀ´¶à½ø³ÌÖ®¼äµÄÏ໥ʵʱͨÐÅ¡£

¡¡¡¡±¾ÎÄÌá³öÒ»¸ö»ùÓÚSocketµÄ¶à½ø³ÌÖ®¼äͨÐŵÄʵÏÖ·½·¨¡£Ô­ÀíÊǽ¨Á¢Ò»¸ö½ø³ÌרÃÅÓÃÀ´×öΪͨÐÅ·þÎñÆ÷(server)À´ÖÐת¸÷¸ö½ø³ÌÖ®¼äµÄͨÐÅ¡£ËüÊ×ÏÈÆô¶¯Ò»¸öÓÃÀ´¼àÊÓÁ¬½ÓÒªÇóµÄlistening Socket£¬²¢°ÑËüµÄÃèÊö£¨Descriptor£©ºÅ¼ÓÈëµ½Ò»¸öÊÂÏȶ¨ÒåºÃµÄfd_setµÄ¼¯ºÏÖУ¬Õâ¸öfd_setµÄ¼¯ºÏÓÃÀ´´æ·Ålistening SocketºÍºóÀ´Éú³ÉµÄͨÐÅSocketµÄÃèÊöºÅ¡£ServerÔËÓÃsystem call selectÀ´ÊµÊ±¼ì²éÊÇ·ñÓÐÊý¾Ýµ½´ïÕâ¸ö¼¯ºÏÖеÄÈκÎÒ»¸ösocket,Èç¹ûÓÐÊý¾Ýµ½´ïlistening Socket£¬ÔòÕâÒ»¶¨ÊÇ¿Í»§¶Ë·¢ÆðµÄÁ¬½ÓÇëÇó£¬ÓÚÊÇÉú³ÉÒ»¸öеÄͨÐÅSocketÓë¸Ã¿Í»§¶ËÁ¬½Ó£¬½«Éú³ÉµÄSocketÃèÊöºÅ¼ÓÈëµ½fd_setµÄ¼¯ºÏÖУ¬½«¿Í»§¶ËµÄIDºÅºÍÓëÖ®¶ÔÓ¦µÄSocketµÄÃèÊöºÅ¼Ç¼ÔÚIDµÇ¼Ç±íÖС£Èç¹ûÓÐÊý¾Ýµ½´ïij¸öͨÐÅSocket£¬ÔòÕâÒ»¶¨ÊÇij¸ö¿Í»§¶Ë·¢ÆðµÄͨÐÅÇëÇ󣬶Á³öÊý¾Ý²¢È¡³öÊÕÐÅ¿Í»§¶ËIDºÅ£¬ÔÚIDµÇ¼Ç±íÖÐÕÒµ½ÓëÖ®¶ÔÓ¦µÄSocketÃèÊöºÅ£¬½«Êý¾Ýͨ¹ý¶ÔÓ¦Socket´«Ë͵½ÊÕÐÅ¿Í»§¶Ë¡£

¡¡¡¡ÆäËû¸÷½ø³Ì×÷Ϊ¿Í»§¶Ë(client)¡£¿Í»§¶ËµÄ¶¯×÷ÊÇÊ×ÏȽ¨Á¢Í¨ÐÅSocketÁ¬½Ó·þÎñÆ÷¶Ë£¬È»ºóͨ¹ýͨÐÅSocket½øÐÐËÍÐźÍÊÕÐÅ¡£

¡¡¡¡ÏÂÃæ¸ø³ö¾ßÌåµÄ³ÌÐòʵÏÖºÍ˵Ã÷£¬

¡¡¡¡Ê×Ïȸø³öServer¶ËµÄ³ÌÐò£¬ÔÚÕâÀï¼ÙÉèÓÐÁ½¸ö¿Í»§¶ËÒª½øÐÐʵʱͨÐÅ£¬ClientAÏòClientB·¢ËÍ×Ö·û1£¬ClientBÏòClientA·¢ËÍ×Ö·û2¡£

ÒýÓÃ:
#include
#include
#include
#include
#include
#include
#include
#include

int main()
{
int rcd ;
struct sockaddr_un server_sockaddr ;
int backlog ;
ushort ci ;
int watch_fd_list[3] ;
fd_set catch_fd_set ;
fd_set watchset ;
int new_cli_fd ;
int maxfd;
int socklen ,server_len;
struct sockaddr_un cli_sockaddr ;
struct {
char module_id ; /* Module ID */
int cli_sock_fd ; /* Socket ID */
} cli_info_t[2] ;

for (ci=0;ci<=1;ci++)
cli_info_t[ci].cli_sock_fd=-1;

for (ci=0;ci<=2;ci++)
watch_fd_list[ci]=-1;

int server_sockfd,client_sockfd;

server_sockfd = socket( AF_UNIX, SOCK_STREAM, 0 ) ;
server_sockaddr.sun_family = AF_UNIX ;
strcpy( server_sockaddr.sun_path, "server_socket" ) ;
server_len=sizeof(server_sockaddr);
rcd = bind( server_sockfd, ( struct sockaddr * )&server_sockaddr, server_len ) ;

backlog = 5 ;
rcd = listen( server_sockfd, backlog ) ;
printf("SERVER::Server is waitting on socket=%d \n",server_sockfd);

watch_fd_list[0]=server_sockfd;
FD_ZERO( &watchset ) ;
FD_SET( server_sockfd, &watchset ) ;
maxfd=watch_fd_list[0];


¡¡¡¡ÔÚÉÏÃæµÄ³ÌÐòÖУ¬ServerÉú³Élistening Socket£¨server_sockfd£©,³õʼ»¯Socket¼àÊÓ¼¯ºÏ£¨watchset£©£¬²¢½«listening Socket·ÅÈëSocket¼àÊÓ¼¯ºÏÖС£

ÒýÓÃ:
while (1){
char ch;
int fd;
int nread;

catch_fd_set=watchset;
rcd = select( maxfd+1, &catch_fd_set, NULL, NULL, (struct timeval *)0 ) ;
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 3 ÌõÆÀÂÛ

  1. liqianfeng317 ÓÚ 2007-09-10 13:21:40·¢±í:

    ÓÐûÓйØÓÚLinux½ø³ÌµÄºÃÊéÍƼöÒ»ÏÂ,лл

  2. download ÓÚ 2006-11-14 10:21:01·¢±í:

    ¡¡¡¡ÔÚÉÏÃæµÄ³ÌÐòÖУ¬Èç¹ûÓÐÊý¾Ýµ½´ïij¸öͨÐÅSocket£¬ServerÔò¶Á³öÊý¾Ý²¢È¡³öÊÕÐÅ¿Í»§¶ËIDºÅ¡£ÔÚIDµÇ¼Ç±íÖÐÕÒµ½ÊÕÐÅ¿Í»§¶Ë¶ÔÓ¦µÄSocketÃèÊöºÅ¡£²¢½«Êý¾Ýͨ¹ý¶ÔÓ¦Socket´«Ë͵½ÊÕÐÅ¿Í»§¶Ë,¸ø³ö¿Í»§¶Ë ClientAµÄ³ÌÐò¡£

    ¡¡¡¡ClientBµÄ³ÌÐòÖ»Ð轫 char dst_module_id='B'; ¸ÄΪchar dst_module_id='A'; char ch='1'; ¸ÄΪchar char ch='2';¼È¿É¡£

    ÒýÓÃ:
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    int main(){

    int client_sockfd;
    int len;
    struct sockaddr_un server_sockaddr,cli_sockaddr;
    int result;
    char dst_module_id='B';
    char ch='1';
    char src_module_id;


    client_sockfd= socket(AF_UNIX,SOCK_STREAM,0);

    cli_sockaddr.sun_family = AF_UNIX ;
    strcpy( cli_sockaddr.sun_path, "A" ) ;
    bind(client_sockfd,(struct sockaddr * )&cli_sockaddr, sizeof( cli_sockaddr ) ) ;

    server_sockaddr.sun_family=AF_UNIX;

    strcpy( server_sockaddr.sun_path, "server_socket" ) ;
    len=sizeof(server_sockaddr);

    result = connect(client_sockfd,( struct sockaddr * )&server_sockaddr,len);
    if (result <0){
    printf("ClientA::error on connecting \n");
    exit(1);
    }

    printf("ClientA::succeed in connecting with server\n");
    sleep(10);
    write(client_sockfd,&dst_module_id,1);
    write(client_sockfd,&ch,1);
    read (client_sockfd,&src_module_id,1);
    read (client_sockfd,&ch,1);
    printf("ClientA::char from Client %c =%c\n", src_module_id,ch);
    close (client_sockfd);

    }


    ¡¡¡¡ÏÂÃæÊÇÑù±¾³ÌÐòµÄÖ´Ðнá¹û

    ÒýÓÃ:
    [root@zhou test]# ./server &
    [3] 4301
    [root@zhou test]# SERVER::Server is waitting on socket=3
    ./clientA & ./clientB &
    [4] 4302
    [5] 4303
    ClientA::succeed in connecting with server
    SERVER::open communication with Client A on socket 4
    [root@zhou test]# SERVER::open communication with Client B on socket 5
    ClientB::succeed in connecting with server
    SERVER::char=1 to Client B on socket5
    ClientB::char from Client A =1
    SERVER::char=2 to Client A on socket4
    ClientA::char from Client B =2

  3. download ÓÚ 2006-11-14 10:20:21·¢±í:

    ¡¡¡¡ÔÚÉÏÃæµÄ³ÌÐòÖУ¬ServerÔËÓÃϵͳµ÷Óú¯Êý selectÀ´ÊµÊ±¼ì²éÊÇ·ñÓÐÊý¾Ýµ½´ïSocket¼àÊÓ¼¯ºÏÖеÄÈκÎÒ»¸ösocket¡£

    ÒýÓÃ:
    if ( rcd < 0 ) {
    printf("SERVER::Server 5 \n");
    exit(1);
    }

    if ( FD_ISSET( server_sockfd, &catch_fd_set ) ) {
    socklen = sizeof( cli_sockaddr ) ;
    new_cli_fd = accept( server_sockfd, ( struct sockaddr * )
    &( cli_sockaddr ), &socklen ) ;
    printf(" SERVER::open communication
    with Client %s on socket %d\n", cli_sockaddr.sun_path,new_cli_fd);

    for (ci=1;ci<=2;ci++){
    if(watch_fd_list[ci] != -1) continue;
    else{
    watch_fd_list[ci] = new_cli_fd;
    break;
    }
    }
    FD_SET(new_cli_fd , &watchset ) ;
    if ( maxfd < new_cli_fd ) {
    maxfd = new_cli_fd ;
    }

    for ( ci=0;ci<=1;ci++){
    if(cli_info_t[ci].cli_sock_fd == -1) {
    cli_info_t[ci].module_id=cli_sockaddr.sun_path[0];
    cli_info_t[ci].cli_sock_fd=new_cli_fd;
    break;
    }
    }

    continue;
    }


    ¡¡¡¡ÔÚÉÏÃæµÄ³ÌÐòÖУ¬ServerÔËÓÃϵͳµ÷Óú¯ÊýFD_ISSETÀ´¼ì²éÊÇ·ñÓпͻ§¶ËµÄÁ¬½ÓÇëÇóµ½´ïListening Socket, Èç¹û·µ»ØÖµ´óÓÚ0£¬ServerÉú³ÉÒ»¸öеÄͨÐÅSocket £¨new_cli_fd£©Óë¿Í»§¶ËÁ¬½Ó¡£½«ÐÂÉú³ÉµÄͨÐÅSocket·ÅÈëSocket¼àÊÓ¼¯ºÏÖУ¨FD_SET£©¡£½«¿Í»§¶ËµÄÐÅÏ¢£¨IDºÅºÍSocketÃèÊöºÅ£©±£´æÔÚ×¢²á±ícli_info_tÖÐ

    ÒýÓÃ:
    for ( ci = 1; ci<=2 ; ci++ ) {
    int dst_fd = -1 ;
    char dst_module_id;
    char src_module_id;
    int i;

    if (watch_fd_list[ ci ]==-1) continue;
    if ( !FD_ISSET( watch_fd_list[ ci ], &catch_fd_set ) ) {
    continue ;
    }
    ioctl(watch_fd_list[ ci ],FIONREAD,&nread);
    if (nread==0){
    continue;
    }
    read( watch_fd_list[ ci ], &dst_module_id, 1 ) ;
    for (i=0;i<=1;i++){
    if(cli_info_t.module_id == dst_module_id)
    dst_fd= cli_info_t.cli_sock_fd;
    if(cli_info_t.cli_sock_fd==watch_fd_list[ ci ])
    src_module_id= cli_info_t.module_id;
    }
    read( watch_fd_list[ ci ], &ch, 1 ) ;
    printf("SERVER::char=%c to Client %c on socket%d\n",ch, dst_module_id,dst_fd);
    write(dst_fd,&src_module_id, 1 ) ;
    write(dst_fd,&ch, 1 ) ;

    }
    }
    }