Article View: borland.public.cppbuilder.internet.socket
Article #3799Creating sockets
From: Jonathan Benedicto
Date: Fri, 29 Apr 2005 00:24
Date: Fri, 29 Apr 2005 00:24
53 lines
2502 bytes
2502 bytes
I'm trying to create a server socket with the following code, but it
seems to have a 50% chance of not working. By this, I mean that no
errors are reported, but Telnet and my own app can't connect to it,
they just report that the connection was refused, and netstat does not
show the listening socket.
int nRet;
int nZero = 0;
struct addrinfo hints;
struct addrinfo *addrlocal = NULL;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_PASSIVE;
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
if( getaddrinfo(NULL, g_Port, &hints, &addrlocal) != 0 )
throw Exception(myprintf("getaddrinfo() failed with error %d\n",
WSAGetLastError()));
if( addrlocal == NULL )
throw Exception(myprintf("getaddrinfo() failed to resolve/convert the
interface\n"));
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()));
nRet = bind(g_sdListen, addrlocal->ai_addr, addrlocal->ai_addrlen);
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()));
freeaddrinfo(addrlocal);
--
Jonathan
Technical Details
Message-ID:
<42717e9e$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!solnet.ch!solnet.ch!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 11508e,890efe40447445cf,start X-Google-Attributes: gid11508e,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Jonathan Benedicto" <incorrect@no.server> Newsgroups: borland.public.cppbuilder.internet.socket Subject: Creating sockets Date: Thu, 28 Apr 2005 20:24:00 -0400 Lines: 55 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: <42717e9e$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1114734238 69.158.104.124 (28 Apr 2005 17:23:58 -0700) Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!solnet.ch!solnet.ch!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail