¡¡¡¡Ì×½Ó¿Ú£¨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 ) ;
liqianfeng317 ÓÚ 2007-09-10 13:21:40·¢±í:
ÓÐûÓйØÓÚLinux½ø³ÌµÄºÃÊéÍƼöÒ»ÏÂ,лл
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';¼È¿É¡£
¡¡¡¡ÏÂÃæÊÇÑù±¾³ÌÐòµÄÖ´Ðнá¹û
download ÓÚ 2006-11-14 10:20:21·¢±í:
¡¡¡¡ÔÚÉÏÃæµÄ³ÌÐòÖУ¬ServerÔËÓÃϵͳµ÷Óú¯Êý selectÀ´ÊµÊ±¼ì²éÊÇ·ñÓÐÊý¾Ýµ½´ïSocket¼àÊÓ¼¯ºÏÖеÄÈκÎÒ»¸ösocket¡£
¡¡¡¡ÔÚÉÏÃæµÄ³ÌÐòÖУ¬ServerÔËÓÃϵͳµ÷Óú¯ÊýFD_ISSETÀ´¼ì²éÊÇ·ñÓпͻ§¶ËµÄÁ¬½ÓÇëÇóµ½´ïListening Socket, Èç¹û·µ»ØÖµ´óÓÚ0£¬ServerÉú³ÉÒ»¸öеÄͨÐÅSocket £¨new_cli_fd£©Óë¿Í»§¶ËÁ¬½Ó¡£½«ÐÂÉú³ÉµÄͨÐÅSocket·ÅÈëSocket¼àÊÓ¼¯ºÏÖУ¨FD_SET£©¡£½«¿Í»§¶ËµÄÐÅÏ¢£¨IDºÅºÍSocketÃèÊöºÅ£©±£´æÔÚ×¢²á±ícli_info_tÖÐ