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

Ò»¸öÓйØUNIXÌ×½Ó×ÖµÄÎÊÌâ

·¢²¼Ê±¼ä:2007-04-24 15:32:56À´Ô´:ºìÁª×÷Õß:tiantianxian
¸÷λ´óϺ,СµÜ¸Õ¸Õ¿ªÊ¼½Ó´¥linux±à³Ì,ÏÖÔÚҪдһ¸ö½ø³Ì¼äͨÐŵijÌÐò,ÒªÇóʹÓÃUNIXÓòÌ×½Ó×Ö£¬²¢Ê¹ÓãգģлúÖÆ£¬ÎÒÓÐÈçϵÄ˼·£¬³ÌÐòÎÒҲдÁË£¬µ«ÊǽÓÊÕ²¿·Ö×ÜÊDz»Õý³££¬ÈçÄ£¿é£ÁÊÕµ½ÏûϢΪchar from client B =5,¿ÉÊÇʵ¼ÊÓ¦¸ÃÊÇÊÕµ½Ò»¸öcharÐÍ×Ö·û£®Çë´ó¼Ò°ïÎÒ¿´¿´ÎҵijÌÐò£¬Ð»Ð»ÁË£®

ʵÏÖ˼·£º±¾³ÌÐòÊÇͨ¹ýUDPЭÒéÀ´·¢ËÍÏûÏ¢£¬ÏûÏ¢µÄÄÚÈÝÒÔ¼°´«µÝµÄµØÖ·ÊÇÓÉmainº¯ÊýµÄ²ÎÊýÀ´´«µÝ¸ø½ø³ÌµÄ¡£²ÎÊýʾÀýÈ磺 AB5hello
ÿÌõÏûÏ¢µÄ³¤¶ÌΪ8×Ö½Ú
ÏûÏ¢µÄ½á¹¹ÈçÏ£º
typedef struct
{
char self_module_id;//×Ô¼ºµÄID£¬¸øÿÌõÏûÏ¢ÌùÉÏ×Ô¼ºµÄ±ê¼Ç£¬Ê¹½ÓÊÕ·½ÖªµÀÊÇË­·¢Ë͹ýÀ´µÄ
char dst_module_id;//Ä¿µÄID£¬¾ö¶¨Ã¿ÌõÏûÏ¢·¢Ë͵ÄÄ¿µÄ£¬Í¨¹ý´ËID¾ö¶¨Ä¿µÄ·¾¶
int length;//ÏûÏ¢³¤¶Ì
char msg[5];//ÏûÏ¢ÄÚÈÝ
}msg_struct;
·¢ËÍʵÏÖ¹ý³Ì£º1 ͨ¹ý¶ÔÏûÏ¢×ܳ¤¶È£¨¼´mainº¯Êý²ÎÊý×ܳ¤¶È£©³ý8£¬ÅжÏÓжàÉÙÌõÍêÕûµÄÏûÏ¢
2 ͨ¹ýµ÷ÓÃÒ»´Îsendtoº¯Êý¾Í·¢ËÍÒ»ÌõÏûÏ¢£¬¸ù¾ÝÊý¾Ý½á¹¹È¡µÃÏàÓ¦µÄÄ¿µÄµØÖ·À´¾ö¶¨·¢Ë͵Ä·¾¶£¬Ò²¾ÍÊÇÿ8¸ö×Ö½Ú¾ÍÅжÏÒ»´Î£¬ÏûÏ¢ÊÇ·¢¸øÄĸöÄ¿µÄµØÖ·µÄ¡£Ö±µ½·¢ÍêËùÓеÄÍêÕûµÄÏûÏ¢¡£
3 ×îºóÒ»ÌõÏûÏ¢»òÐíÊDz»Âú8¸ö×ֽڵģ¬Í¨¹ý¶ÔÏûÏ¢×ܳ¤¶ÈÈ¡Ä££¬»ñµÃµ½Õâ¸ö²»ÍêÕûÏûÏ¢µÄ³¤¶È£¬²¢·¢ËÍÕâÌõÏûÏ¢¡£
¶ÁȡʵÏÖ¹ý³Ì£º1 ͨ¹ýrecvfrom»ñµÃÈ«²¿µÄÔÚÃèÊö·ûÉϵÄÏûÏ¢£¬²¢½«Æä´æÈ뻺³åÇørch
2 ¿ªÊ¼´¦Àí´æÈërchµÄÏûÏ¢£¬Í¨¹ý·Ö°ü»úÖÆ£¬°ÑËüÖØдò°ü³ÉÔ¤Ïȶ¨ÒåµÄÏûÏ¢½á¹¹Ìå
3 Ïȱ£´æÍêÕûµÄÏûÏ¢£¬ÔÙ±£´æ²»ÍêÕûµÄÏûÏ¢¡£

³ÌÐò´úÂëÈçÏÂ
/*-------------------------------------------------------------------------
This is a client program
-------------------------------------------------------------------------*/
#include
#include
#include
#include
#include
#include
#include
#include
#define SIZE 32
#define MAXTEXT 256

int main(int argc,char **argv)
{
int src_sockfd;
int dst_sockfd;
int self_sockfd;
int n_read,n_write,len,rcd,i,o,k;
struct sockaddr_un src_sockaddr,dst_sockaddr,self_sockaddr;
//int result;
//char argv[1][MAXTEXT];
char rch[MAXTEXT];
//char dst_module_id;
char src_module_id;
//char self_module_id='A';
fd_set read_fd_set,write_fd_set ;
fd_set watchset ;
int maxfd;

typedef struct
{
char self_module_id;
char dst_module_id;
int length;
char msg[5];
}msg_struct;
msg_struct dst_msg;
dst_msg.self_module_id='A';
msg_struct src_msg;
char *ptr_rch,*ptr_sch;
msg_struct *ptr_rmsg,*ptr_smsg;
ptr_rmsg=&src_msg;
ptr_rch=rch;
ptr_smsg=&dst_msg;
ptr_sch=argv[1];

len=sizeof(src_sockaddr);

if(argc<2)
{
printf(" Aerror1 happen\n");
exit(1);
}


unlink("/home/wewe/code/temp/A");
self_sockfd=socket(AF_LOCAL,SOCK_DGRAM,0);
if(src_sockfd <0)
{
printf("sokcet err, %s \n", strerror(errno));
return -1;
}
bzero(&self_sockaddr,sizeof(self_sockaddr));
self_sockaddr.sun_family=AF_LOCAL;
strcpy(self_sockaddr.sun_path,"/home/wewe/code/temp/A");
bind(self_sockfd,(struct sockaddr*)&self_sockaddr,sizeof(self_sockaddr));



while(1)
//while(1)
{
FD_ZERO( &write_fd_set) ;//Çå³ýwatchset½á¹¹Ìå
FD_SET( self_sockfd, &write_fd_set) ;//½«sever_sockfd¼ÓÈëµ½watchsetÖÐ?
FD_ZERO(&read_fd_set);
FD_SET(self_sockfd,&read_fd_set);
maxfd=self_sockfd;


if(rcd = select( maxfd+1, &read_fd_set,&write_fd_set, NULL, (struct timeval *)0 )>0)
{

if (FD_ISSET( self_sockfd, &read_fd_set ))
{
printf("nengduA\n");
bzero(&src_sockaddr,len);
src_sockaddr.sun_family=AF_LOCAL;
//recvfrom(self_sockfd,&src_module_id,1,0,(struct sockaddr*)&src_sockaddr,&len);

n_read=recvfrom(self_sockfd,rch,10,0,(struct sockaddr*)&src_sockaddr,&len);
rch[n_read]=0;
for(i=0;i {
ptr_rmsg->self_module_id=rch[8*i+0];
ptr_rmsg->dst_module_id=rch[8*i+1];
ptr_rmsg->length=rch[8*i+2];
ptr_rmsg->msg[0]=rch[8*i+3];
ptr_rmsg->msg[1]=rch[8*i+4];
ptr_rmsg->msg[2]=rch[8*i+5];
ptr_rmsg->msg[3]=rch[8*i+6];
ptr_rmsg->msg[4]=rch[8*i+7];
printf("ClientA::char from Client %c =%c%c%c%c%c\n",ptr_rmsg->self_module_id,ptr_rmsg->msg[0],ptr_rmsg->msg[1],ptr_rmsg->msg[2],ptr_rmsg->msg[3],ptr_rmsg->msg[4]);

}
o=n_read%8;
if(o==4)
{
ptr_rmsg->self_module_id=rch[8*i+0];
ptr_rmsg->dst_module_id=rch[8*i+1];
ptr_rmsg->length=rch[8*i+2];
ptr_rmsg->msg[0]=rch[8*i+3];
printf("ClientA::char from Client %c =%c\n",ptr_rmsg->self_module_id,ptr_rmsg->msg[0]);
}
else if(o==5)
{
ptr_rmsg->self_module_id=rch[8*i+0];
ptr_rmsg->dst_module_id=rch[8*i+1];
ptr_rmsg->length=rch[8*i+2];
ptr_rmsg->msg[0]=rch[8*i+3];
ptr_rmsg->msg[1]=rch[8*i+4];
printf("ClientA::char from Client %c =%c%c\n",ptr_rmsg->self_module_id,ptr_rmsg->msg[0],ptr_rmsg->msg[1]);
}
else if(o==6)
{
ptr_rmsg->self_module_id=rch[8*i+0];
ptr_rmsg->dst_module_id=rch[8*i+1];
ptr_rmsg->length=rch[8*i+2];
ptr_rmsg->msg[0]=rch[8*i+3];
ptr_rmsg->msg[1]=rch[8*i+4];
ptr_rmsg->msg[2]=rch[8*i+5];
printf("ClientA::char from Client %c =%c%c%c\n",ptr_rmsg->self_module_id,ptr_rmsg->msg[0],ptr_rmsg->msg[1],ptr_rmsg->msg[2]);
}
else if(o==7)
{
ptr_rmsg->self_module_id=rch[8*i+0];
ptr_rmsg->dst_module_id=rch[8*i+1];
ptr_rmsg->length=rch[8*i+2];
ptr_rmsg->msg[0]=rch[8*i+3];
ptr_rmsg->msg[1]=rch[8*i+4];
ptr_rmsg->msg[2]=rch[8*i+5];
ptr_rmsg->msg[3]=rch[8*i+6];
printf("ClientA::char from Client %c =%c%c%c%c\n",ptr_rmsg->self_module_id,ptr_rmsg->msg[0],ptr_rmsg->msg[1],ptr_rmsg->msg[2],ptr_rmsg->msg[3]);
}
else
printf("recevie nothing\n");




sleep(3);
//printf("nengdu2\r\n");

}

if ( FD_ISSET( self_sockfd, &write_fd_set ) )
{ printf("nengxieA\n");

bzero(&dst_sockaddr,len);
dst_sockaddr.sun_family=AF_LOCAL;
n_write=strlen(argv[1]);
for(k=0;k {
ptr_smsg->self_module_id=argv[1][8*k+0];
ptr_smsg->dst_module_id=argv[1][8*k+1];
ptr_smsg->length=argv[1][8*k+2];
ptr_smsg->msg[0]=argv[1][8*k+3];
ptr_smsg->msg[1]=argv[1][8*k+4];
ptr_smsg->msg[2]=argv[1][8*k+5];
ptr_smsg->msg[3]=argv[1][8*k+6];
ptr_smsg->msg[4]=argv[1][8*k+7];
if(ptr_smsg->dst_module_id=='B')
strcpy(dst_sockaddr.sun_path,"/home/wewe/code/temp/B");
else
strcpy(dst_sockaddr.sun_path,"/home/wewe/code/temp/C");
sendto(self_sockfd,ptr_smsg,8,0,(struct sockaddr*)&dst_sockaddr,len);
printf("ClientA:: char to Client %c=%c%c%c%c%c\n",ptr_smsg->dst_module_id,argv[1][8*k+3],argv[1][8*k+4],argv[1][8*k+5],argv[1][8*k+6],argv[1][8*k+7]);
}
o=n_write%8;//ÅжÏ×îºÃÒ»Ìõ²»ÍêÕûÏûÏ¢ÓжàÉÙ×Ö½Ú
if(o==4)
{
ptr_smsg->self_module_id=argv[1][8*k+0];
ptr_smsg->dst_module_id=argv[1][8*k+1];
ptr_smsg->length=argv[1][8*k+2];
ptr_smsg->msg[0]=argv[1][8*k+3];
ptr_smsg->msg[1]='0';
ptr_smsg->msg[2]='0';
ptr_smsg->msg[3]='0';
ptr_smsg->msg[4]='0';
if(ptr_smsg->dst_module_id=='B')
strcpy(dst_sockaddr.sun_path,"/home/wewe/code/temp/B");
else
strcpy(dst_sockaddr.sun_path,"/home/wewe/code/temp/C");
sendto(self_sockfd,ptr_smsg,4,0,(struct sockaddr*)&dst_sockaddr,len);
printf("ClientA:: char to Client %c=%c\n",ptr_smsg->dst_module_id,argv[1][8*k+3]);
}
else if(o==5)
{
ptr_smsg->self_module_id=argv[1][8*k+0];
ptr_smsg->dst_module_id=argv[1][8*k+1];
ptr_smsg->length=argv[1][8*k+2];
ptr_smsg->msg[0]=argv[1][8*k+3];
ptr_smsg->msg[1]=argv[1][8*k+4];
ptr_smsg->msg[2]='0';
ptr_smsg->msg[3]='0';
ptr_smsg->msg[4]='0';
if(ptr_smsg->dst_module_id=='B')
strcpy(dst_sockaddr.sun_path,"/home/wewe/code/temp/B");
else
strcpy(dst_sockaddr.sun_path,"/home/wewe/code/temp/C");
sendto(self_sockfd,ptr_smsg,5,0,(struct sockaddr*)&dst_sockaddr,len);
printf("ClientA:: char to Client %c=%c%c\n",ptr_smsg->dst_module_id,argv[1][8*k+3],argv[1][8*k+4]);
}
else if(o==6)
{
ptr_smsg->self_module_id=argv[1][8*k+0];
ptr_smsg->dst_module_id=argv[1][8*k+1];
ptr_smsg->length=argv[1][8*k+2];
ptr_smsg->msg[0]=argv[1][8*k+3];
ptr_smsg->msg[1]=argv[1][8*k+4];
ptr_smsg->msg[2]=argv[1][8*k+5];
ptr_smsg->msg[3]='0';
ptr_smsg->msg[4]='0';
if(ptr_smsg->dst_module_id=='B')
strcpy(dst_sockaddr.sun_path,"/home/wewe/code/temp/B");
else
strcpy(dst_sockaddr.sun_path,"/home/wewe/code/temp/C");
sendto(self_sockfd,ptr_smsg,6,0,(struct sockaddr*)&dst_sockaddr,len);
printf("ClientA:: char to Client %c=%c%c%c\n",ptr_smsg->dst_module_id,argv[1][8*k+3],argv[1][8*k+4],argv[1][8*k+5]);
}
else if(o==7)
{
ptr_smsg->self_module_id=argv[1][8*k+0];
ptr_smsg->dst_module_id=argv[1][8*k+1];
ptr_smsg->length=argv[1][8*k+2];
ptr_smsg->msg[0]=argv[1][8*k+3];
ptr_smsg->msg[1]=argv[1][8*k+4];
ptr_smsg->msg[2]=argv[1][8*k+5];
ptr_smsg->msg[3]=argv[1][8*k+6];
ptr_smsg->msg[4]='0';
if(ptr_smsg->dst_module_id=='B')
strcpy(dst_sockaddr.sun_path,"/home/wewe/code/temp/B");
else
strcpy(dst_sockaddr.sun_path,"/home/wewe/code/temp/C");
sendto(self_sockfd,ptr_smsg,7,0,(struct sockaddr*)&dst_sockaddr,len);
printf("ClientA:: char to Client %c=%c%c%c%c\n",ptr_smsg->dst_module_id,argv[1][8*k+3],argv[1][8*k+4],argv[1][8*k+5],argv[1][8*k+6]);
}
else
printf("send nothing\n");

sleep(3);

//printf("nengxie\r\n");
}



}

//continue;

}





}
ÎÄÕÂÆÀÂÛ

¹²ÓÐ 46 ÌõÆÀÂÛ

  1. 211.205.15.* ÓÚ 2007-07-17 22:10:19·¢±í:

    http://10b0ddac277cb3d7832940814f08d605-t.xkktxb.org 10b0ddac277cb3d7832940814f08d605 http://10b0ddac277cb3d7832940814f08d605-b1.xkktxb.org 10b0ddac277cb3d7832940814f08d605 http://10b0ddac277cb3d7832940814f08d605-b3.xkktxb.org 8d1f2bfe3cbc5359328d95464cab8b7c