멀티플렉싱 sender 테스트(udp socket, fifo, select)

[참고 : 멀티플렉싱 receiver 테스트]


멀티플렉싱 sender 테스트를 위한 코드로 udp socket, select, 그리고 IPC(fifo) 를 썼다.

--------------------------------------------------------
sender.c

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h> // for socket(), bind(), connet()
#include <arpa/inet.h> // for sockaddr_in, inet_ntoa()

#include <pthread.h>

typedef int SOCKET;
#define NET_INVALID_SOCKET -1
#define NET_SOCKET_ERROR -1

#define MAXCONN   10
#define MAX_BUF_SIZE 500

#define FIFO_NAME "my_test"


int main(void)
{
    char s[300] = "FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_\
FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_\
FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_FIFO_TEST_MSG_";
    int num, fd;
int input;
int pid;
int run =1;

int l_ii, l_i;

SOCKET ServerSocket;
SOCKET numbytes;

struct sockaddr_in ServerAddress;
unsigned short ServerPort = 5060;

SOCKET ClientSocket;//sockfd;
    int servlen;
    int state;

char buf[MAX_BUF_SIZE] = "SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG\
SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_\
SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_SOCKET_TEST_MSG_\n";



    servlen = sizeof(ServerAddress);
    ClientSocket = socket(AF_INET, SOCK_DGRAM, 0);

if(ClientSocket == NET_INVALID_SOCKET)
{
printf("Fail to create the socket\n");
close(ClientSocket);
exit(0);        
    }

    bzero(&ServerAddress, sizeof(ServerAddress));
    ServerAddress.sin_family = AF_INET;
    ServerAddress.sin_addr.s_addr = inet_addr("127.0.0.1");
    ServerAddress.sin_port = htons(ServerPort);

#if 1


for(l_ii=0; l_ii<50 ; l_ii++){
printf("[FIFO] send msg\n");
mknod(FIFO_NAME, S_IFIFO | 0666, 0);

printf("waiting for readers...\n");
fd = open(FIFO_NAME, O_WRONLY);
printf("got a reader--type some stuff\n");

#if 0
while (gets(s), !feof(stdin)) {
#endif
if ((num = write(fd, s, strlen(s))) == -1)
perror("write error");
else
printf("speak: wrote %d bytes\n", num);
#if 0
}
#endif
close(fd);
usleep(200000);
}
printf("[%d]FIFO FINISHED well]\n\n", l_ii);

for(l_i=0; l_i<50 ; l_i++){
printf("[SOCKET][send][%d bytes]\n%s\n",strlen(buf),buf);
sendto(ClientSocket, (void *)&buf, strlen(buf), 0, (struct sockaddr *)&ServerAddress, servlen);    

memset(buf, 0x00, sizeof(buf));
recvfrom(ClientSocket, (void *)&buf, sizeof(buf), 0, (struct sockaddr *)&ServerAddress, &servlen);

printf("[recv][%d bytes]\n%s\n",strlen(buf),buf);

usleep(110000);
}
printf("[%d]SOCKET FINISHED well]", l_i);
close(ClientSocket);

#else

while(run){
printf("Start : ");
scanf("%d",&input);

switch(input){
case 1:
printf("[FIFO] send msg\n");
mknod(FIFO_NAME, S_IFIFO | 0666, 0);

printf("waiting for readers...\n");
fd = open(FIFO_NAME, O_WRONLY);
printf("got a reader--type some stuff\n");

//while (gets(s), !feof(stdin)) {

if ((num = write(fd, s, strlen(s))) == -1)
perror("write error");
else
printf("speak: wrote %d bytes\n", num);
//}
close(fd);
break;

case 2:
printf("[SOCKET] send msg\n");
sendto(ClientSocket, (void *)&buf, sizeof(buf), 0, (struct sockaddr *)&ServerAddress, servlen);    
printf("[send][%d bytes]\n%s\n",strlen(buf),buf);

memset(buf, 0x00, sizeof(buf));

recvfrom(ClientSocket, (void *)&buf, sizeof(buf), 0, (struct sockaddr *)&ServerAddress, &servlen);

printf("[recv][%d bytes]\n%s\n",strlen(buf),buf);
//

break;

case 3:
run = 0;
break;
}
close(ClientSocket);

#endif
    return 0;
}


[참고 : 멀티플렉싱 receiver 테스트]

댓글 없음:

댓글 쓰기

안녕하세요 :)