1

My computer has 2 IP addresses for 2 Ethernet cards. 192.168.0.1 is a server, but how to set 192.168.0.2 as client in C with winsock2. By default client is always 192.168.0.1.

unalignedmemoryaccess
  • 7,246
  • 2
  • 25
  • 40
Yarmouk
  • 13
  • 2

1 Answers1

2

You can call bind before connect, to give client a specific IP address or port.

sockaddr_in service;
service.sin_family = AF_INET;
service.sin_addr.s_addr = inet_addr("192.168.0.2"); // bind to specific IP address
service.sin_port = 0; // not to specify port number

iResult = bind(ListenSocket, (SOCKADDR *) &service, sizeof (service));

// then call connect()...
zhm
  • 3,513
  • 3
  • 34
  • 55