Article View: borland.public.cppbuilder.internet.socket
Article #3809Re: Creating sockets
From: Jonathan Benedicto
Date: Fri, 29 Apr 2005 21:17
Date: Fri, 29 Apr 2005 21:17
52 lines
2724 bytes
2724 bytes
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> wrote in message
news:42727b69$1@newsgroups.borland.com...
[snip]
> You should be checking for WSA_IO_PENDING, not ERROR_IO_PENDING.
[snip]
> Also, just for the record, your use of exception handling is
> horrible in
> these code snippets of yours. You are leaking a lot of resources
> when you
> decide to throw your exceptions because you are not closing/freeing
> resources you allocated beforehand.
I restructured the server so that when a exception does get thrown, no
memory is leaked. And I changed the CreateListenSocket code to this:
int nRet;
int nZero = 0;
struct sockaddr_in addrlocal = {0};
g_sdListen = WSASocket(AF_INET, SOCK_STREAM, IPPROTO_TCP, NULL, 0,
WSA_FLAG_OVERLAPPED);
if( g_sdListen == INVALID_SOCKET )
throw Exception(myprintf("WSASocket(g_sdListen) failed: %d\n",
WSAGetLastError()));
addrlocal.sin_family = AF_INET;
addrlocal.sin_port = 52877;
addrlocal.sin_addr.s_addr = INADDR_ANY;
nRet = bind(g_sdListen, (sockaddr*) &addrlocal, sizeof(addrlocal));
if( nRet == SOCKET_ERROR )
throw Exception(myprintf("bind() failed: %d\n", WSAGetLastError()));
nRet = listen(g_sdListen, 50);
if( nRet == SOCKET_ERROR )
throw Exception(myprintf("listen() failed: %d\n",
WSAGetLastError()));
nZero = 0;
nRet = setsockopt(g_sdListen, SOL_SOCKET, SO_SNDBUF, (char *)&nZero,
sizeof(nZero));
if( nRet == SOCKET_ERROR )
throw Exception(myprintf("setsockopt(SNDBUF) failed: %d\n",
WSAGetLastError()));
But now the server does not even work 50% of the time.
Jonathan
Technical Details
Message-ID:
<4272a47c$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fr.ip.ndsoftware.net!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<42717e9e$1@newsgroups.borland.com> <42718c45$1@newsgroups.borland.com> <4271b145$1@newsgroups.borland.com> <4271c6a8$1@newsgroups.borland.com> <4272519f$1@newsgroups.borland.com> <42727b69$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,890efe40447445cf X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Jonathan Benedicto" <incorrect@no.server> Newsgroups: borland.public.cppbuilder.internet.socket References: <42717e9e$1@newsgroups.borland.com> <42718c45$1@newsgroups.borland.com> <4271b145$1@newsgroups.borland.com> <4271c6a8$1@newsgroups.borland.com> <4272519f$1@newsgroups.borland.com> <42727b69$1@newsgroups.borland.com> Subject: Re: Creating sockets Date: Fri, 29 Apr 2005 17:17:49 -0400 Lines: 54 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.2527 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527 X-RFC2646: Format=Flowed; Original NNTP-Posting-Host: 69.158.104.124 Message-ID: <4272a47c$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1114809468 69.158.104.124 (29 Apr 2005 14:17:48 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fr.ip.ndsoftware.net!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!news-in.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail