In my client code (Linux), I am following these steps to connect to a socket:
Creating a socket
sockDesc = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)
Connecting it (retry for 'x' time in case of failure)
connect(sockDesc, (sockaddr *) &destAddr, sizeof(destAddr))
(After filling the destAddr fields)
Using the socket for send/recv oepration:
send(sockDesc, buffer, bufferLen, 0) recv(sockDesc, buffer, bufferLen, 0)
close the socket descriptor and exit
close(sockDesc)
If during send/recv the connection was broken, I found that I could connect by returning to step 2.
Is this solution okay? ...or should I close the socket descriptor and return to step 1?
Another interesting thing observed which i am not able to understand. I stop my echo server and start the client. Socket is created (step 1) and connect fails (as expected) but the connect call is retried lets say 10 times. After 5 retries i have started the server and connect is successful. But during send() call it recives SIGPIPE error. I would like to know:-
1) Here also I need to create new Socket after each connect() call failure? As per my understanding as i have not issued any send/recv call on the socket it is as good as newly created socket so i can reuse the same fd for connect() call
2) I dont understand why SIGPIPE is received when the server is up and connect is successful.