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
.
Asked
Active
Viewed 878 times
1

unalignedmemoryaccess
- 7,246
- 2
- 25
- 40

Yarmouk
- 13
- 2
-
2You can use `bind`. – user253751 Jun 06 '18 at 05:36
-
I can use bind to set Address for Server, but with client how can do this? – Yarmouk Jun 06 '18 at 05:41
-
@Yarmouk `bind()` works the same for both servers and clients. You `bind()` a server before listening. You can `bind()` a client before connecting. – Remy Lebeau Jun 06 '18 at 06:38
-
solved! Thanks god you are here!! – Yarmouk Jun 06 '18 at 07:00
1 Answers
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
-
-
@Yarmouk Glad it helped. You can *accept* my answer if it solves your problem. :-) – zhm Jun 06 '18 at 07:05
-
Rather than fixing te port to 27015, why not just set it to 0? – Antti Haapala -- Слава Україні Jun 06 '18 at 07:10
-