[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
MULTIPLEXING USING SOCKET OPTION
/* Program using socket option to multiplex */
#include<stdlib.h>
#include<netinet/in.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<sys/select.h>
#include<sys/time.h>
#include<errno.h>
#define max(a,b) ((a) > (b) ? (a) : (b))
#define PORTA 6000
#define PORTB 8000
struct sockaddr_in serv_addr1,serv_addr2;
int main()
{
int sockfd1,sockfd2,count=0,length=0,maxfd1;
char buff1[128],buff2[128];
struct timeval *test = (struct timeval *)malloc(sizeof(struct timeval));
struct timeval time;
socklen_t *optlen = (socklen_t *)malloc(sizeof(struct timeval));
time.tv_sec = 2000;
time.tv_usec = 0;
printf("reciever running\n");
serv_addr1.sin_family=AF_INET;
serv_addr1.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr1.sin_port=htons(PORTA);
if((sockfd1=socket(AF_INET,SOCK_DGRAM,0))<0)
write("I/O socket error\n");
if((bind(sockfd1,(struct sockaddr *)&serv_addr1,sizeof(serv_addr1)))<0)
printf("bind error\n");
setsockopt(sockfd1,SOL_SOCKET,SO_RCVTIMEO,&time,sizeof(time));
getsockopt(sockfd1,SOL_SOCKET,SO_RCVTIMEO,test,optlen);
printf("\n sec = %d",test->tv_sec);
printf("\n msec = %d",test->tv_usec);
serv_addr2.sin_family=AF_INET;
serv_addr2.sin_addr.s_addr=inet_addr("127.0.0.1");
serv_addr2.sin_port=htons(PORTB);
if((sockfd2=socket(AF_INET,SOCK_DGRAM,0))<0)
write("I/O socket error\n");
if((bind(sockfd2,(struct sockaddr *)&serv_addr2,sizeof(serv_addr2)))<0)
printf("bind error\n");
setsockopt(sockfd2,SOL_SOCKET,SO_RCVTIMEO,&time,sizeof(time));
printf("\n ok");
for(;;)
{
count = recvfrom(sockfd1,(char *)buff1,sizeof(buff1),0,NULL,NULL);
if(count < 0)
{
if (errno == EWOULDBLOCK)
{
printf("\n blcok");
continue;
}
else
printf("\n recvfrom error\n");
}
else
printf("\n %s ",buff1);
count = recvfrom(sockfd2,(char *)buff2,sizeof(buff2),0,NULL,NULL);
if(count < 0)
{
if (errno == EWOULDBLOCK)
continue;
else
printf("\n recvfrom error \n");
}
else
printf("\n %s ",buff2);
}
}