I have a question about a connect() call of a TCP socket implementation. What does it mean for a connect() call to be non-blocking. The connect() call does a three-way handshake with some other socket, by sending a syn, waiting for a SYNACK and then send an ACK. The connect() call also returns true if the connection succeeded or false if it did not succeed.
If the call is non-blocking then I guess that means that the connect should return immediately, even if it is still waiting for a SYNACK, but in that case it can never return false when it fails to connect because by then it already returned.
So my questions: - What does it mean for a connect() call to be non-blocking. - How does a connect() call achieve this? Is this only possible using threads? - Im simulating a tcp stack in java, could you give a simplified example of how the non-blocking version would look? I included a sketch of what I think the blocking version roughly looks like (more psuedo code than actual java):
public boolean connect(IpAddress dst, int port){
// create a syn packet and send it
ipLayer.send(.....,<synpacket>);
try{
// wait for a synack and store it in receive_packet
ipLayer.receive(...., receivePacket,<timeout>);
} catch( TimeoutException e ){
// timed out.
return false;
}
// use information from a receivePacket to create an ack-packet then send it.
ipLayer.send(<ackpacket>);
return true;
}