I inherited some TCP code that called:
bind(tcpSocket, (struct sockaddr*)&server_addr, sizeof(server_addr));
before the call to
setsockopt(tcpSocket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
Not surprisingly, this lead to the message: "Address already in use". Simply swapping the order of the calls resolved the problem.
This brings up a question: In general, should any calls to setsockopt()
be made before a call to bind()
? Before a call to connect()
?