Q
hello I need to listen to two ports and process the received message.
Which way to do is better? I have a short program like this
but I am not sure whether it is good or not. Or I'd better open two
threads and process the message seperately.
Thanks a lot!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#define MYPORT1 3000 // the port users will be connecting to
#define MYPORT2 4000
#define MAXLEN 100
int main(void)
{
int udp_fd1, udp_fd2, maxfd; // listen on tcp_fd, new connection
on new_tcp_fd, udp_fd is for register
struct sockaddr_in udp_addr1, udp_addr2; // my address
information
struct sockaddr_in register_addr; //register client's address
fd_set rset;
char buf[MAXLEN];
int buf_size, numbytes, addr_len;
buf_size = sizeof(buf);
addr_len = sizeof(struct sockaddr);
struct timeval tv;
int nready;
//***************************************************
//open the UDP socket 1
if((udp_fd1 = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
fprintf(stderr,"\nsocket create error.\n");
exit(1);
}
udp_addr1.sin_family = AF_INET;
udp_addr1.sin_port = htons(MYPORT1);
udp_addr1.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(udp_addr1.sin_zero), '\0', 8);
/* Binding the created socket to the port specified */
if(bind(udp_fd1,(struct sockaddr *)&udp_addr1,sizeof(struct
sockaddr)) == -1)
{
fprintf(stderr, "\n Error in bind \n");
exit(1);
}
//***************************************************
//open the UDP socket 2
if((udp_fd2 = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
fprintf(stderr,"\nsocket create error.\n");
exit(1);
}
udp_addr2.sin_family = AF_INET;
udp_addr2.sin_port = htons(MYPORT2);
udp_addr2.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(udp_addr2.sin_zero), '\0', 8);
/* Binding the created socket to the port specified */
if(bind(udp_fd2,(struct sockaddr *)&udp_addr2,sizeof(struct
sockaddr)) == -1)
{
fprintf(stderr, "\n Error in bind \n");
exit(1);
}
FD_ZERO(&rset);
while(1){
/**********receive packet***************************************/
FD_SET(udp_fd1,&rset);
FD_SET(udp_fd2,&rset);
if(udp_fd1 > udp_fd2)
maxfd = udp_fd1 + 1;
else
maxfd = udp_fd2 + 1;
tv.tv_usec = 0;
tv.tv_sec = 2;
nready = select(maxfd,&rset,NULL,NULL,&tv);
switch(nready){
case -1:
printf("\n Error in select!!! \n");
break;
case 0:
printf("**************Did not receive anything\n");
break;
default:
printf("\n\n**************** Received packets ");
if (FD_ISSET(udp_fd1,&rset))
{
/* Receive packet from MYPORT1 */
printf("\n--------------------------------\n");
if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct
sockaddr*)®ister_addr, &addr_len))==-1){
fprintf(stderr, "error in recvfrom.\n");
exit(1);
}
printf("Got packet from IP address %s, port %d \n",
inet_ntoa(register_addr.sin_addr),ntohs(register_addr.sin_port));
printf("on port: %d\n", ntohs(udp_addr1.sin_port));
printf("Packet size: %u bytes \n", numbytes);
printf("Message: %s\n", buf);
}
if (FD_ISSET(udp_fd2,&rset)) /* new client connection */
{
/* Receive packet from MYPORT2 */
printf("\n--------------------------------\n");
if((numbytes = recvfrom(udp_fd2, buf, MAXLEN-1, 0,(struct
sockaddr*)®ister_addr, &addr_len))==-1){
fprintf(stderr, "error in recvfrom.\n");
exit(1);
}
printf("Got packet from IP address %s, port %d \n",
inet_ntoa(register_addr.sin_addr),ntohs(register_addr.sin_port));
printf("on port: %d\n", ntohs(udp_addr2.sin_port));
printf("Packet size: %u bytes \n", numbytes);
printf("Message: %s\n", buf);
}
}
}
return 0;
}
Which way to do is better? I have a short program like this
but I am not sure whether it is good or not. Or I'd better open two
threads and process the message seperately.
Thanks a lot!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#define MYPORT1 3000 // the port users will be connecting to
#define MYPORT2 4000
#define MAXLEN 100
int main(void)
{
int udp_fd1, udp_fd2, maxfd; // listen on tcp_fd, new connection
on new_tcp_fd, udp_fd is for register
struct sockaddr_in udp_addr1, udp_addr2; // my address
information
struct sockaddr_in register_addr; //register client's address
fd_set rset;
char buf[MAXLEN];
int buf_size, numbytes, addr_len;
buf_size = sizeof(buf);
addr_len = sizeof(struct sockaddr);
struct timeval tv;
int nready;
//***************************************************
//open the UDP socket 1
if((udp_fd1 = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
fprintf(stderr,"\nsocket create error.\n");
exit(1);
}
udp_addr1.sin_family = AF_INET;
udp_addr1.sin_port = htons(MYPORT1);
udp_addr1.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(udp_addr1.sin_zero), '\0', 8);
/* Binding the created socket to the port specified */
if(bind(udp_fd1,(struct sockaddr *)&udp_addr1,sizeof(struct
sockaddr)) == -1)
{
fprintf(stderr, "\n Error in bind \n");
exit(1);
}
//***************************************************
//open the UDP socket 2
if((udp_fd2 = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
fprintf(stderr,"\nsocket create error.\n");
exit(1);
}
udp_addr2.sin_family = AF_INET;
udp_addr2.sin_port = htons(MYPORT2);
udp_addr2.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(udp_addr2.sin_zero), '\0', 8);
/* Binding the created socket to the port specified */
if(bind(udp_fd2,(struct sockaddr *)&udp_addr2,sizeof(struct
sockaddr)) == -1)
{
fprintf(stderr, "\n Error in bind \n");
exit(1);
}
FD_ZERO(&rset);
while(1){
/**********receive packet***************************************/
FD_SET(udp_fd1,&rset);
FD_SET(udp_fd2,&rset);
if(udp_fd1 > udp_fd2)
maxfd = udp_fd1 + 1;
else
maxfd = udp_fd2 + 1;
tv.tv_usec = 0;
tv.tv_sec = 2;
nready = select(maxfd,&rset,NULL,NULL,&tv);
switch(nready){
case -1:
printf("\n Error in select!!! \n");
break;
case 0:
printf("**************Did not receive anything\n");
break;
default:
printf("\n\n**************** Received packets ");
if (FD_ISSET(udp_fd1,&rset))
{
/* Receive packet from MYPORT1 */
printf("\n--------------------------------\n");
if((numbytes = recvfrom(udp_fd1, buf, MAXLEN-1, 0,(struct
sockaddr*)®ister_addr, &addr_len))==-1){
fprintf(stderr, "error in recvfrom.\n");
exit(1);
}
printf("Got packet from IP address %s, port %d \n",
inet_ntoa(register_addr.sin_addr),ntohs(register_addr.sin_port));
printf("on port: %d\n", ntohs(udp_addr1.sin_port));
printf("Packet size: %u bytes \n", numbytes);
printf("Message: %s\n", buf);
}
if (FD_ISSET(udp_fd2,&rset)) /* new client connection */
{
/* Receive packet from MYPORT2 */
printf("\n--------------------------------\n");
if((numbytes = recvfrom(udp_fd2, buf, MAXLEN-1, 0,(struct
sockaddr*)®ister_addr, &addr_len))==-1){
fprintf(stderr, "error in recvfrom.\n");
exit(1);
}
printf("Got packet from IP address %s, port %d \n",
inet_ntoa(register_addr.sin_addr),ntohs(register_addr.sin_port));
printf("on port: %d\n", ntohs(udp_addr2.sin_port));
printf("Packet size: %u bytes \n", numbytes);
printf("Message: %s\n", buf);
}
}
}
return 0;
}