Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: socket close properly #609

Merged
merged 3 commits into from
Mar 9, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/benchUtil.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*/

#include <bench.h>

char resEncodingChunk[] = "Encoding: chunked";
Expand Down Expand Up @@ -1161,6 +1160,45 @@ int createSockFd() {
}

void destroySockFd(int sockfd) {
// shutdown the connection since no more data will be sent
int result, how;
#ifdef WINDOWS
how = SD_SEND;
#else
how = SHUT_WR;
#define SOCKET_ERROR -1
#endif
result = shutdown(sockfd, how);
if (SOCKET_ERROR == result) {
errorPrint("Socket shutdown failed with error: %d\n",
#ifdef WINDOWS
WSAGetLastError());
closesocket(sockfd);
WSACleanup();
#else
result);
close(sockfd);
#endif
return;
}
// Receive until the peer closes the connection
do {
int recvbuflen = LARGE_BUFF_LEN;
char recvbuf[LARGE_BUFF_LEN];
result = recv(sockfd, recvbuf, recvbuflen, 0);
if ( result > 0 )
debugPrint("Socket bytes received: %d\n", result);
else if ( result == 0 )
infoPrint("Connection closed with result %d\n", result);
else
errorPrint("Socket recv failed with error: %d\n",
#ifdef WINDOWS
WSAGetLastError()
#else
result
#endif
);
} while( result > 0 );
#ifdef WINDOWS
closesocket(sockfd);
WSACleanup();
Expand Down