Im currently working with socket programming, but i am having problems connecting to my device when testing code on a computer with multiple ethernet devices.
How can i define which ethernet device it should be using (Mac OS X)?
Source:
int sock, bytes_recieved;
char send_data[1024],recv_data[1024];
struct hostent *host;
struct sockaddr_in server_addr;
host = gethostbyname("192.168.100.4");
cout << "Start" << endl;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
// perror("Socket");
cout << "Socket error" << endl;
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(30002);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);
if (connect(sock, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr)) == -1)
{
// perror("Connect");
cout << "Connect error" << endl;
exit(1);
}
Thanks in advance.
You can specify the listening address with the bind function.
An address of 0.0.0.0 is used to listen on all interfaces and IP addresses belonging to the computer, alternatively you can specify a specific IP address to listen on, which will select only the interface that address belongs to
EDIT: The MSDN example at the bottom of the page is easier to follow