-
SO_BINDTODEVICE problem
Hi everybody;
I try to generate a server client code. What i try to do is sending video streams from eth0 and eth1 to the other server programs' eth0 and eth1. In order to do that, i decided to use SO_BINDTODEVICE. But the code is not working. Am i misunderstood the usage of SO_BINDTODEVICE.
1-Defining two ports
2-Defining two sockets
3-Assigning host ips on them
4-Bind two sockets to two ethernet out
5-Send them to two differnet ethernet on server
Thanks in advance.
Code:
//CLIENT
#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 <netdb.h>
#define MYPORT 4965 // the port users will be connecting to
#define MYPORT2 4962
int main(int argc, char *argv[])
{
int sockfd,i,result2;
int sockfd2;
struct sockaddr_in their_addr; // connector's address information
struct hostent *he,*he2;
struct sockaddr_in their_addr2; // connector's address information
int numbytes;
int numbytes2;
if (argc != 3) {
fprintf(stderr,"%d usage: talker hostname message\n",argc);
exit(1);
}
if ((he=gethostbyname(argv[1])) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((he2=gethostbyname(argv[2])) == NULL) { // get the host info
perror("gethostbyname");
exit(1);
}
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
if ((sockfd2 = socket(AF_INET, SOCK_DGRAM, 0)) == -1) {
perror("socket");
exit(1);
}
// bind a socket to a device name (might not work on all systems):
char *optval2;
optval2 = "eth1"; // 4 bytes long, so 4, below:
setsockopt(sockfd, IPPROTO_UDP, SO_BINDTODEVICE, optval2, 4);
char *optval1;
optval1 = "eth2"; // 4 bytes long, so 4, below:
setsockopt(sockfd2, IPPROTO_UDP, SO_BINDTODEVICE, optval1, 4);
their_addr.sin_family = AF_INET; // host byte order
their_addr.sin_port = htons(MYPORT); // short, network byte order
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
their_addr2.sin_family = AF_INET; // host byte order
their_addr2.sin_port = htons(MYPORT2); // short, network byte order
their_addr2.sin_addr = *((struct in_addr *)he2->h_addr);
memset(&(their_addr2.sin_zero), '\0', 8); // zero the rest of the struct
FILE *file_path;
file_path=fopen("/home/fatih/Desktop/MoDeo/Underworld.Evolution.1080p.Blu-Ray.MPEG-2.HRT_Sample.ts","r");
char * ch, * ch2;
int count=188;
long lSize;
size_t result;
fseek (file_path , 0 , SEEK_END);
lSize = ftell (file_path);
rewind (file_path);
// allocate memory to contain the whole file:
ch = (char*) malloc (sizeof(char)*lSize);
if (ch == NULL) {fputs ("Memory error",stderr); exit (2);}
ch2 = (char*) malloc (sizeof(char)*lSize);
if (ch2 == NULL) {fputs ("Memory error",stderr); exit (2);}
// copy the file into the buffer:
result = fread (ch,1,lSize,file_path);
printf("%ld",lSize);
int b=0;
long k=0;
while(k<=lSize){
for(i=0;i<188;i++){
ch2[i]=ch[k];
k++;
} if ((numbytes=sendto(sockfd,ch2, 188, 0,
(struct sockaddr *)&their_addr, sizeof(struct sockaddr))) == -1) {
perror("sendto");
exit(1);
}
for(i=0;i<188;i++){
ch2[i]=ch[k];
k++;
}
if ((numbytes2=sendto(sockfd2,ch2, 188, 0,
(struct sockaddr *)&their_addr2, sizeof(struct sockaddr))) == -1) {
perror("sendto");
exit(1);
}
}