if( strcmp( str, "NewAgent" ) == 0 ){
printf("Server get msg from Agent\n");
// get message from Agent
num_agent++;
table[ num_agent-1 ] = rcvid;
if( buf_rcvid > 0 ){
MsgReply( buf_rcvid, 0, &msg[0], sizeof( msg ) );
if( num_client > 0 ){
// send work to agent
rcvid = rcvid_tab[ num_client-1 ];
num_client--;
send_to_agent();
}// end if( num_client > 0 )
}//end if( buf_rcvid > 0 )
}else{
/*
//Message from Client
printf("SERVER!!!\n Get Message from client and send this to agent %d\n", num_agent );
itoa( rcvid, buf, 10 );
//strcpy( buf, "Hello Agent!!" );
MsgReply( table[num_agent-1], 0, &buf[0], sizeof( buf ) );
num_agent--;
*/
if( num_agent > 0 ) send_to_agent();
else{ num_client++; rcvid_tab[ num_client-1 ] = rcvid; }
}
}
}//end while(1)
name_detach(attach, 0);
return 0;
}
void send_to_agent(){
//Message from Client
printf("SERVER!!!\n Get Message from client and send this to agent %d\n", num_agent );
itoa( rcvid, buf, 10 );
//strcpy( buf, "Hello Agent!!" );
MsgReply( table[num_agent-1], 0, &buf[0], sizeof( buf ) );
num_agent--;
}
Client.c
#include <sys/neutrino.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/iofunc.h>
#include <sys/dispatch.h>
#define PATH "channel_lab1"
int main(){
int fd;
char sbuf[255], rbuf[255];
fd = name_open( PATH, 0 );
if( fd == -1 ){
printf("Client name open error ???\n" );
exit( EXIT_FAILURE );
}
strcpy( sbuf, "I am the client" );
MsgSend( fd, &sbuf[0], sizeof(sbuf), &rbuf[0], sizeof(rbuf) );
printf("CLIENT!!!\n Client get = %s \n", rbuf );
name_close( fd );
return 0;
}
Agent.c
#include <sys/neutrino.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/iofunc.h>
#include <sys/dispatch.h>
#define PATH "channel_lab1"
int main(){
int fd, i;
char sbuf[255], rbuf[255];
fd = name_open( PATH, 0 );
if( fd == -1 ){
printf("AGENT name open error ???\n" );
exit( EXIT_FAILURE );
}
strcpy( sbuf, "NewAgent 0 not_msg" );
printf("Agent Start\n");
MsgSend( fd, &sbuf[0], sizeof(sbuf), &rbuf[0], sizeof(rbuf) );
printf("Agent get %s\n", rbuf );
while(1){
sleep(10);
printf("Agent get %s\n", rbuf );
sprintf( sbuf, "NewAgent %s Hi_Client_from_Agent!", rbuf );
MsgSend( fd, &sbuf[0], sizeof(sbuf), &rbuf[0], sizeof(rbuf) );
}
name_close( fd );
return 0;
}
Результат выполнения программы:
# ./serv&
[1] 2416686
Agent Start
Agent Start
Agent Start
Agent Start
Server get message = NewAgent 0 not_msg
Server get msg from Agent
Server get message = NewAgent 0 not_msg
Server get msg from Agent
Server get message = NewAgent 0 not_msg
Server get msg from Agent
Server get message = NewAgent 0 not_msg
Server get msg from Agent
Agent Start
Server get message = NewAgent 0 not_msg
Server get msg from Agent
./client& ./client& ./client& ./client/_ _& ./client& ./client&
[2] 2420788
[3] 2420789
[4] 2420791
[5] 2420793
[6] 2420795
[7] 2420797
# Server get message = I am the client
SERVER!!!
Get Message from client and send this to agent 5
Agent get 8
Server get message = I am the client
SERVER!!!
Get Message from client and send this to agent 4
Agent get 9
Server get message = I am the client
SERVER!!!
Get Message from client and send this to agent 3
Agent get 10
Server get message = I am the client
SERVER!!!
Get Message from client and send this to agent 2
Agent get 11
Server get message = I am the client
SERVER!!!
Get Message from client and send this to agent 1
Server get message = I am the client
Agent get 12
Agent get 8
Server get message = NewAgent 8 Hi_Client_from_Agent!
Server get msg from Agent
SERVER!!!
Get Message from client and send this to agent 1
CLIENT!!!
Agent get 9
Client get = Hi_Client_from_Agent!
Уважаемый посетитель!
Чтобы распечатать файл, скачайте его (в формате Word).
Ссылка на скачивание - внизу страницы.