home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / tests / nbconn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.9 KB  |  159 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /*
  20.  * A test for nonblocking connect.  Functions tested include PR_Connect,
  21.  * PR_Poll, and PR_GetConnectStatus.
  22.  *
  23.  * The test should be invoked with a host name, for example:
  24.  *     nbconn www.netscape.com
  25.  * It will do a nonblocking connect to port 80 (HTTP) on that host,
  26.  * and when connected, issue the "GET /" HTTP command.
  27.  *
  28.  * You should run this test in three ways:
  29.  * 1. To a known web site, such as www.netscape.com.  The HTML of the
  30.  *    top-level page at the web site should be printed.
  31.  * 2. To a machine not running a web server at port 80.  This test should
  32.  *    fail.  Ideally the error code should be PR_CONNECT_REFUSED_ERROR.
  33.  *    But it is possible to return PR_UNKNOWN_ERROR on certain platforms.
  34.  * 3. To an unreachable machine, for example, a machine that is off line.
  35.  *    The test should fail after the connect times out.  Ideally the
  36.  *    error code should be PR_IO_TIMEOUT_ERROR, but it is possible to
  37.  *    return PR_UNKNOWN_ERROR on certain platforms.
  38.  */
  39.  
  40. #include "nspr.h"
  41. #include <stdio.h>
  42.  
  43. #ifdef XP_MAC
  44. #define printf PR_LogPrint
  45. extern void SetupMacPrintfLog(char *logFile);
  46. static char *hosts[4] = {"cynic", "warp", "gandalf", "neon"};
  47. #endif
  48.  
  49.  
  50. int main(int argc, char **argv)
  51. {
  52.     PRHostEnt he;
  53.     char buf[1024];
  54.     PRNetAddr addr;
  55.     PRFileDesc *sock;
  56.     PRPollDesc pd;
  57.     PRStatus rv;
  58.     PRSocketOptionData optData;
  59.     PRIntn n;
  60.  
  61. #ifdef XP_MAC
  62.     int index;
  63.     PRIntervalTime timeout;
  64.     SetupMacPrintfLog("nbconn.log");
  65.     for (index=0; index<4; index++) {
  66.     argv[1] = hosts[index];
  67.     timeout = PR_INTERVAL_NO_TIMEOUT;
  68.     if (index == 3)
  69.         timeout = PR_SecondsToInterval(10UL);
  70. #endif
  71.  
  72.     PR_STDIO_INIT();
  73. #ifndef XP_MAC
  74.     if (argc != 2) {
  75.         fprintf(stderr, "Usage: nbconn <hostname>\n");
  76.         exit(1);
  77.     }
  78. #endif
  79.  
  80.     if (PR_GetHostByName(argv[1], buf, sizeof(buf), &he) == PR_FAILURE) {
  81.         printf( "Unknown host: %s\n", buf);
  82.         exit(1);
  83.     } else {
  84.         printf( "host: %s\n", buf);
  85.     }
  86.     PR_EnumerateHostEnt(0, &he, 80, &addr);
  87.  
  88.     sock = PR_NewTCPSocket();
  89.     optData.option = PR_SockOpt_Nonblocking;
  90.     optData.value.non_blocking = PR_TRUE;
  91.     PR_SetSocketOption(sock, &optData);
  92.     rv = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT);
  93.     if (rv == PR_FAILURE && PR_GetError() == PR_IN_PROGRESS_ERROR) {
  94.         printf( "Connect in progress\n");
  95.     }
  96.  
  97.     pd.fd = sock;
  98.     pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT;
  99. #ifndef XP_MAC
  100.     n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
  101. #else
  102.     n = PR_Poll(&pd, 1, timeout);
  103. #endif
  104.     if (n == -1) {
  105.         printf( "PR_Poll failed\n");
  106.         exit(1);
  107.     }
  108.     printf( "PR_Poll returns %d\n", n);
  109.     if (pd.out_flags & PR_POLL_READ) {
  110.         printf( "PR_POLL_READ\n");
  111.     }
  112.     if (pd.out_flags & PR_POLL_WRITE) {
  113.         printf( "PR_POLL_WRITE\n");
  114.     }
  115.     if (pd.out_flags & PR_POLL_EXCEPT) {
  116.         printf( "PR_POLL_EXCEPT\n");
  117.     }
  118.     if (pd.out_flags & PR_POLL_ERR) {
  119.         printf( "PR_POLL_ERR\n");
  120.     }
  121.     if (pd.out_flags & PR_POLL_NVAL) {
  122.         printf( "PR_POLL_NVAL\n");
  123.     }
  124.  
  125.     if (PR_GetConnectStatus(&pd) == PR_SUCCESS) {
  126.         printf("PR_GetConnectStatus: connect succeeded\n");
  127.         /* Mac and Win16 have trouble printing to the console. */
  128. #if !defined(XP_MAC) && !defined(WIN16)
  129.         PR_Write(sock, "GET /\r\n\r\n", 9);
  130.         PR_Shutdown(sock, PR_SHUTDOWN_SEND);
  131.         pd.in_flags = PR_POLL_READ;
  132.         while (1) {
  133.             n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);
  134.             printf( "poll returns %d\n", n);
  135.             n = PR_Read(sock, buf, sizeof(buf));
  136.             printf( "read returns %d\n", n);
  137.             if (n <= 0) {
  138.                 break;
  139.             }
  140.             PR_Write(PR_STDOUT, buf, n);
  141.         }
  142. #endif
  143.     } else {
  144.         if (PR_GetError() == PR_IN_PROGRESS_ERROR) {
  145.             printf( "PR_GetConnectStatus: connect still in progress\n");
  146.             exit(1);
  147.         }
  148.         printf( "PR_GetConnectStatus: connect failed: (%ld, %ld)\n",
  149.                 PR_GetError(), PR_GetOSError());
  150.     }
  151.     PR_Close(sock);
  152. #ifdef XP_MAC
  153.     } /* end of for loop */
  154. #endif
  155.  
  156.     printf( "PASS\n");
  157.     return 0;
  158. }
  159.