Posted on 2006-08-22 17:23
Enjoy Life 阅读(1338)
评论(0) 编辑 收藏 引用 所属分类:
Linux Programming Doc
该client主要是通过消息队列发送消息到server端
而server端则监听client的消息队列发送来的消息
在每次收到一个新的消息,都创建一个新的线程
该线程将主要用于将来吧client发过来的ha的消息来组合形成
acct request的消息发送到server端去。
client.c
同前面一篇blog中记载
server.c
#include "local.h"
#include <pthread.h>
void *
func(void *arg){
pid_t pid;
pthread_t tid;
pid = getpid();
tid = pthread_self();
printf("pid is %u\n",(unsigned int)pid);
printf("thread id is %u\n",(unsigned int)tid);
struct mip6_args *rad_args;
rad_args = (struct mip6_args *)arg;
printf("----msg buff in args is %s\n", rad_args->buff);
return((void *)0);
}
main(void)
{
//about new pthread
pthread_t radius_thread;
pthread_attr_t attrs;
struct mip6_args rad_thread_args;
int err;
//rad_thread_args.argv[1]="Password = test";
// rad_thread_args.argv[0]="User-Name = test";
int mid,n;
key_t key;
MESSAGE msg;
//thread
pthread_attr_init(&attrs);
//pthread_attr_setdetachstate(&attrs, PTHREAD_CREATE_DETACHED);
//Use the Same key with Client
if((key = ftok(".", SEED)) == -1){
perror("Client : key generation");
exit(1);
}
mid = msgget(key, IPC_CREAT | 0660);
while(1){
if((n = msgrcv(mid, &msg, sizeof(msg), SERVER,0)) == -1){
perror("SERVER:msgrcv");
exit(2);
}
else if (n==0)//client have finished
break;
else{
write(fileno(stdout),msg.buffer, strlen(msg.buffer));
rad_thread_args.buff = msg.buffer;
//when recv msg from HA, should send acct to server
err = pthread_create(&radius_thread, &attrs, func,&rad_thread_args);
if(err != 0){
perror("thread create:");
exit(6);
}
/*
printf("----about main is------\n");
printf("main processid is %d\n",getpid());
printf("main threadid is %d\n",pthread_self());
*/
}
}
msgctl(mid, IPC_RMID, (struct msqid_ds *)0);
exit(0);
}