hi,
i have the following code. When i run the code it bind the 2nd socket but not the first socket. It print "Bind failed". How can i bind both socket on same port and ip. Please help me.
i have the following code. When i run the code it bind the 2nd socket but not the first socket. It print "Bind failed". How can i bind both socket on same port and ip. Please help me.
Code:
struct sockaddr_in addr;
struct sockaddr_in addr1;
int sockfd;
int sockfd1;
// Create a socket
sockfd = socket( AF_INET, SOCK_DGRAM, 0 );
sockfd1 = socket( AF_INET, SOCK_DGRAM, 0 );
// Setup its address structure
// bzero( &addr, sizeof(struct sockaddr_in));
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl("192.168.3.5");
addr.sin_port = htons( 32005 );
addr1.sin_family = AF_INET;
addr1.sin_addr.s_addr = htonl("192.168.3.5");
addr1.sin_port = htons( 32005 );
// make address reusable
int yes=1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEPORT,&yes,sizeof(int));
// Bind it to an address and port
if( bind( sockfd, (struct sockaddr *)&addr, sizeof(struct sockaddr)) )
printf("\nBind failed\n");
else
printf("\nBind success\n");
// Bind it to an address and port
if( bind( sockfd1, (struct sockaddr *)&addr1, sizeof(struct sockaddr)) )
printf("\nBind 1 failed\n");
else
printf("\nBind 1 success\n");