[참고 : 멀티플렉싱 sender 테스트]
멀티플렉싱 receiver(서버 역할) 테스트를 위한 코드로 udp socket, select, 그리고 IPC(fifo) 를 썼다.
----------------------------------------------------------------------------
receiver.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/socket.h>
#include <netinet/in.h>
#define FIFO_NAME "my_test"
#define MAX_FD 2
int main()
{
//
int i;
int state;
// for fifo & select
int num;
int fd[MAX_FD];
char fifo_buf[300];
struct timeval tv; // not used
fd_set readfds, writefds;
// for socket
int n;
int sockfd;
int clilen;
char rcv_buf[255];
struct sockaddr_in serveraddr, clientaddr;
// for aging result
int cnt_skt=0, cnt_fifo=0;
int dbg_skt[100]={0,},dbg_fifo[100]={0,};
clilen = sizeof(clientaddr);
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (sockfd < 0)
{
perror("socket error : ");
exit(0);
}
bzero(&serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
serveraddr.sin_port = htons(5060);
state = bind(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
if (state == -1)
{
perror("bind error : ");
exit(0);
}
fd[0] = sockfd;
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
//fd[1] = open(FIFO_NAME, O_RDONLY);
//printf("waiting for FIFO writer...\n");
//fd[1] = open(FIFO_NAME, O_RDWR);
fd[1] = open(FIFO_NAME, O_RDONLY | O_NONBLOCK);
//printf("I'm not going to wait for a writer!!!\n");
//fd[1] = open(FIFO_NAME, O_RDONLY | O_NDELAY);
printf("[SOCKET AGING RESULT\n");
for(cnt_skt =1; cnt_skt<51; cnt_skt++){
printf("[%d]",dbg_skt[cnt_skt-1]);
if(cnt_skt%10 == 0)
printf("\n");
}
printf("\n[FIFO AGING RESULT\n");
for(cnt_fifo =1; cnt_fifo<51; cnt_fifo++){
printf("[%d]",dbg_fifo[cnt_fifo-1]);
if(cnt_fifo%10 == 0)
printf("\n");
}
cnt_skt = cnt_fifo = 0;
for(;;)
{
FD_SET(fd[0], &readfds);
FD_SET(fd[1], &readfds);
state = select(fd[1]+1, &readfds, NULL, NULL, NULL);
//if(state <= 0)
// continue;
switch(state)
{
case -1:
perror("select error : ");
exit(0);
break;
default :
if (FD_ISSET(fd[0], &readfds))
{
dbg_skt[cnt_skt++] = 1;
n =recvfrom(sockfd, (void *)&rcv_buf, sizeof(rcv_buf), 0, (struct sockaddr *)&clientaddr, &clilen);
printf("[%d][SOCKET][Recv][%d byte]\n%s\n",cnt_skt,n,rcv_buf);
printf("send %d\n",sendto(sockfd, (void *)&rcv_buf, strlen(rcv_buf), 0, (struct sockaddr *)&clientaddr, clilen));
}
if (FD_ISSET(fd[1], &readfds))
{
dbg_fifo[cnt_fifo++] = 1;
do {
if ((num = read(fd[1], fifo_buf, 300)) == -1)
perror("[FIFO]read error");
else {
fifo_buf[num] = '\0';
printf("[%d][FIFO][Recv][%d byte]\n%s\n",cnt_fifo, num, fifo_buf);
}
} while (num > 0);
//FD_CLR(fd[1],&readfds);
//close(fd[1]);
unlink(FIFO_NAME);
mknod(FIFO_NAME, S_IFIFO | 0666, 0);
fd[1] = open(FIFO_NAME, O_RDONLY | O_NONBLOCK);
}
break;
}
usleep(1000);
if(cnt_fifo == 50 && cnt_skt == 50){
printf("[SOCKET AGING RESULT\n");
for(cnt_skt =1; cnt_skt<51; cnt_skt++){
printf("[%d]",dbg_skt[cnt_skt-1]);
if(cnt_skt%10 == 0)
printf("\n");
}
printf("[FIFO AGING RESULT\n");
for(cnt_fifo =1; cnt_fifo<51; cnt_fifo++){
printf("[%d]",dbg_fifo[cnt_fifo-1]);
if(cnt_fifo%10 == 0)
printf("\n");
}
}
//break;
}
close(fd[0]); // close socket fd
close(sockfd); // close socket
close(fd[1]); // close fifo fd
unlink(FIFO_NAME); // close fifo
}
[참고 : 멀티플렉싱 sender 테스트]
댓글 없음:
댓글 쓰기
안녕하세요 :)