Hello together,
this is my first thread here, but i think there will be some more
Ok, here's my problem:
I've written a simple client programm, which sends data to a server every 5 minutes or so.
Because of this small amount of data, i always reopen a socket. But this socket doesn't close propertly, the connection stays in state TIME_WAIT.
Every time i reopen a Socket, the programm also allocates about 8-12 kB of memory, and never frees it again.
I will post my network code here, and i hope some of you guys are able to help me.
I have this problem since a few days so i hope somebody has a solution for this. Thank you.Code:#include "stdafx.h" #include "NetworkHandler.h" #define NETWORK_ERROR -1 #define NETWORK_OK 0 #define SD_SEND 1 #define SD_RECEIVE 1 /* Prototypes */ void ReportError(int errorCode, const char *whichFunc); char buffer[256]; //Buffer for the message char errorMsg[92]; //Buffer for the errorMessage WORD sockVersion; WSADATA wsaData; LPHOSTENT hostEntry; SOCKADDR_IN serverInfo; SOCKET theSocket; LINGER theLinger; int nret; int connState; /* * Main function, calls the other functions. */ int connectToServer(void) { connState = 0; connState = initConnection(); connState = sendMessage(); closeConnection(); return connState; } /* * Initialize the connection, by setting the server name and port * Open the connection */ int initConnection(){ sockVersion = MAKEWORD(1, 1); WSAStartup(sockVersion, &wsaData); hostEntry = gethostbyname("127.0.0.1"); if (!hostEntry) { nret = WSAGetLastError(); ReportError(nret, "gethostbyname()"); WSACleanup(); return NETWORK_ERROR; } theSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); theLinger.l_onoff = 1; theLinger.l_linger = 0; setsockopt(theSocket, SOL_SOCKET, SO_LINGER, (char *) &theLinger, sizeof(LINGER)); if (theSocket == INVALID_SOCKET) { nret = WSAGetLastError(); ReportError(nret, "socket()"); WSACleanup(); return NETWORK_ERROR; } serverInfo.sin_family = AF_INET; serverInfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list); serverInfo.sin_port = htons(13); nret = connect(theSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr)); if (nret == SOCKET_ERROR) { nret = WSAGetLastError(); ReportError(nret, "connect()"); closesocket(theSocket); WSACleanup(); return NETWORK_ERROR; } return 0; } /* * Send data method */ int sendMessage(){ ZeroMemory(buffer, 256); wsprintf(buffer, "Some message"); nret = send(theSocket, buffer, strlen(buffer), 0); if (nret == SOCKET_ERROR) { closeConnection(); return NETWORK_ERROR; } return 0; } /* * Close Socket */ void closeConnection(){ int b; char buf[256]; WSACancelBlockingCall(); shutdown(theSocket, SD_SEND); while ((b = recv(theSocket, buf, 256, 0)) != 0) if (b == SOCKET_ERROR) break; shutdown(theSocket, SD_RECEIVE); closesocket(theSocket); WSACleanup(); } void ReportError(int errorCode, const char *whichFunc) { ZeroMemory(errorMsg, 92); sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode); MessageBox(NULL, errorMsg, "NetworkError", MB_OK); }



LinkBack URL
About LinkBacks




