🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #8961

creating a non-blocking socket with WinSock?

#8961
From: Robert G. Hoover
Date: Mon, 23 Aug 2004 13:02
45 lines
2329 bytes
How can I create a non-blocking socket with windows sockets? I've been
crawling thought the documentation but haven't found a place to specify the
non-blocking vs thread-blocking which corresponds to
TServerSocket:::ServerType. Can anyone help me?

 // Windows Sockets implementation details structure
 WSADATA wsadata;

 // initiate use of Windows Sockets DLL (WS2_32.DLL)
WSAStartup(WSVERS, &wsadata);

 // create socket descriptor
 SOCKET ListenSocket = socket(PF_INET, SOCK_STREAM, 0);

 // initialize TCP/IP protocol structure with port number
 struct sockaddr_in sin;
 sin.sin_family      = PF_INET;
 sin.sin_addr.s_addr = INADDR_ANY;
 sin.sin_port        = htons(PortNumber);

 // Bind Server to the socket
bind(ListenSocket, (struct sockaddr *)&sin, sizeof(sin));

 // Setup Server to listen on this socket
listen(ListenSocket, MAX_CLIENTS);

  // TCP/IP protocol structure
  struct sockaddr_in fsin;

  // set address length
  int addlen = sizeof(fsin);

  // wait for a connection request, suspends execution!
  ClientSocket[NumClients] = accept(ListenSocket, (struct sockaddr *)&fsin,
&addlen);

So, when I call accept, my thread is suspended, waiting for a client to
connect. If no client tries to connect, then when I exit the program, the
thread is still active. If I can set my socket to non-blocking, then
accept() will return an error is no client is trying to connect and my
thread will be allowed to loop/exit, right?

Thanks,

Rob
Technical Details
Message-ID: <4129eac9@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,a7377a39e2ca7927,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Robert G. Hoover" <not.a.real.address@anywhere.sorry>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: creating a non-blocking socket with WinSock?
Date: Mon, 23 Aug 2004 09:02:12 -0400
Lines: 47
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
NNTP-Posting-Host: 67.142.36.34
Message-ID: <4129eac9@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1093266121 67.142.36.34 (23 Aug 2004 06:02:01 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail