🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Non-blocking Sockets

#13134
From: Mike King
Date: Tue, 28 Oct 2003 15:22
67 lines
2924 bytes
I'm have a problem with Sockets.  The following code reads the first email
message from a POP3 server.  The problem is I have to use a wait for it to
work.  I don't consider using a wait as being a acceptable way to write
programs.  It seems that this code is using non-blocking sockets but I'm not
sure.  Is this code using non-blocking sockets?  And if so how can I change
it to use blocking sockets.


#include <stdio.h>
#include <winsock2.h>

int main(int argc, char* argv[])
{
   char user[] = "someone";
   char pass[] = "password";
   char server[] = "mail.domain.com";
   int portNo = 110;
   int ret;
   WSADATA      WSData;
   LPHOSTENT    lpHostEntry;
   SOCKADDR_IN  SockAddr;
   SOCKET       hServer;
   char         szBuffer[4096], szMsgLine[255];

   const    VERSION_MAJOR  = 1;
   const    VERSION_MINOR  = 1;

   WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData);
   lpHostEntry = gethostbyname(server);
   hServer = socket(PF_INET, SOCK_STREAM, 0);

   SockAddr.sin_family = AF_INET;
   SockAddr.sin_port = htons(portNo);
   SockAddr.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);

   connect( hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr));

   recv( hServer, szBuffer, sizeof(szBuffer), 0);

   wsprintf(szMsgLine, "USER %s\r\n",user);
   send( hServer, szMsgLine, strlen(szMsgLine), 0);
   recv( hServer, szBuffer, sizeof(szBuffer), 0);

   wsprintf(szMsgLine, "PASS %s\r\n",pass);
   send( hServer, szMsgLine, strlen(szMsgLine), 0);
   recv( hServer, szBuffer, sizeof(szBuffer), 0);

   strcpy(szMsgLine, "RETR 1\r\n");
   send( hServer, szMsgLine, strlen(szMsgLine), 0);
   Sleep(300);  // waiting
   ret = recv( hServer, szBuffer, sizeof(szBuffer), 0);
   szBuffer[ret-2]='\0';

   printf(szBuffer);

   lstrcpy(szMsgLine, "QUIT\r\n");
   send( hServer, szMsgLine, strlen(szMsgLine), 0);
   ret = recv( hServer, szBuffer, sizeof(szBuffer), 0);
   szBuffer[ret-2]='\0';

   closesocket( hServer );
   WSACleanup();

   return 0;
}
//--------------------------------------------------------------------------
-
Technical Details
Message-ID: <3f9e89cd$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,bb15a7a8b7638d84,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-10-28 07:40:29 PST
From: "Mike King" <emailMK@excite.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Non-blocking Sockets
Date: Tue, 28 Oct 2003 10:22:52 -0500
Lines: 69
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 64.45.143.248
Message-ID: <3f9e89cd$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1067354573 64.45.143.248 (28 Oct 2003 07:22:53 -0700)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!fu-berlin.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail