📄 nbconn.c
字号:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- *//* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is the Netscape Portable Runtime (NSPR). * * The Initial Developer of the Original Code is Netscape * Communications Corporation. Portions created by Netscape are * Copyright (C) 1998-2000 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. *//* * A test for nonblocking connect. Functions tested include PR_Connect, * PR_Poll, and PR_GetConnectStatus. * * The test should be invoked with a host name, for example: * nbconn www.netscape.com * It will do a nonblocking connect to port 80 (HTTP) on that host, * and when connected, issue the "GET /" HTTP command. * * You should run this test in three ways: * 1. To a known web site, such as www.netscape.com. The HTML of the * top-level page at the web site should be printed. * 2. To a machine not running a web server at port 80. This test should * fail. Ideally the error code should be PR_CONNECT_REFUSED_ERROR. * But it is possible to return PR_UNKNOWN_ERROR on certain platforms. * 3. To an unreachable machine, for example, a machine that is off line. * The test should fail after the connect times out. Ideally the * error code should be PR_IO_TIMEOUT_ERROR, but it is possible to * return PR_UNKNOWN_ERROR on certain platforms. */#include "nspr.h"#include "plgetopt.h"#include <stdio.h>#ifdef XP_MAC#define printf PR_LogPrintextern void SetupMacPrintfLog(char *logFile);static char *hosts[4] = {"cynic", "warp", "gandalf", "neon"};#endif#define SERVER_MAX_BIND_COUNT 100#define DATA_BUF_SIZE 256#define TCP_SERVER_PORT 10000#define TCP_UNUSED_PORT 211typedef struct Server_Param { PRFileDesc *sp_fd; /* server port */} Server_Param;static void PR_CALLBACK TCP_Server(void *arg);int _debug_on;#define DPRINTF(arg) if (_debug_on) printf argstatic PRIntn connection_success_test();static PRIntn connection_failure_test();int main(int argc, char **argv){ PRHostEnt he; char buf[1024]; PRNetAddr addr; PRPollDesc pd; PRStatus rv; PRSocketOptionData optData; const char *hostname = NULL; PRIntn default_case, n, bytes_read, bytes_sent; PRInt32 failed_already = 0;#ifdef XP_MAC int index; PRIntervalTime timeout;#endif /* * -d debug mode */ PLOptStatus os; PLOptState *opt = PL_CreateOptState(argc, argv, "d"); while (PL_OPT_EOL != (os = PL_GetNextOpt(opt))) { if (PL_OPT_BAD == os) continue; switch (opt->option) { case 0: /* debug mode */ hostname = opt->value; break; case 'd': /* debug mode */ _debug_on = 1; break; default: break; } } PL_DestroyOptState(opt);#ifdef XP_MAC SetupMacPrintfLog("nbconn.log"); for (index=0; index<4; index++) { argv[1] = hosts[index]; timeout = PR_INTERVAL_NO_TIMEOUT; if (index == 3) timeout = PR_SecondsToInterval(10UL);#endif PR_STDIO_INIT();#ifndef XP_MAC if (hostname) default_case = 0; else default_case = 1;#endif if (default_case) { /* * In the default case the following tests are executed: * 1. successful connection: a server thread accepts a connection * from the main thread * 2. unsuccessful connection: the main thread tries to connect to a * non-existent port and expects to get an error */ rv = connection_success_test(); if (rv == 0) rv = connection_failure_test(); return rv; } else { PRFileDesc *sock; if (PR_GetHostByName(argv[1], buf, sizeof(buf), &he) == PR_FAILURE) { printf( "Unknown host: %s\n", argv[1]); exit(1); } else { printf( "host: %s\n", buf); } PR_EnumerateHostEnt(0, &he, 80, &addr); sock = PR_NewTCPSocket(); optData.option = PR_SockOpt_Nonblocking; optData.value.non_blocking = PR_TRUE; PR_SetSocketOption(sock, &optData); rv = PR_Connect(sock, &addr, PR_INTERVAL_NO_TIMEOUT); if (rv == PR_FAILURE && PR_GetError() == PR_IN_PROGRESS_ERROR) { printf( "Connect in progress\n"); } pd.fd = sock; pd.in_flags = PR_POLL_WRITE | PR_POLL_EXCEPT;#ifndef XP_MAC n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT);#else n = PR_Poll(&pd, 1, timeout);#endif if (n == -1) { printf( "PR_Poll failed\n"); exit(1); } printf( "PR_Poll returns %d\n", n); if (pd.out_flags & PR_POLL_READ) { printf( "PR_POLL_READ\n"); } if (pd.out_flags & PR_POLL_WRITE) { printf( "PR_POLL_WRITE\n"); } if (pd.out_flags & PR_POLL_EXCEPT) { printf( "PR_POLL_EXCEPT\n"); } if (pd.out_flags & PR_POLL_ERR) { printf( "PR_POLL_ERR\n"); } if (pd.out_flags & PR_POLL_NVAL) { printf( "PR_POLL_NVAL\n"); } if (PR_GetConnectStatus(&pd) == PR_SUCCESS) { printf("PR_GetConnectStatus: connect succeeded\n"); /* Mac and Win16 have trouble printing to the console. */#if !defined(XP_MAC) && !defined(WIN16) PR_Write(sock, "GET /\r\n\r\n", 9); PR_Shutdown(sock, PR_SHUTDOWN_SEND); pd.in_flags = PR_POLL_READ; while (1) { n = PR_Poll(&pd, 1, PR_INTERVAL_NO_TIMEOUT); printf( "poll returns %d\n", n); n = PR_Read(sock, buf, sizeof(buf)); printf( "read returns %d\n", n); if (n <= 0) { break; } PR_Write(PR_STDOUT, buf, n); }#endif } else { if (PR_GetError() == PR_IN_PROGRESS_ERROR) { printf( "PR_GetConnectStatus: connect still in progress\n"); exit(1); } printf( "PR_GetConnectStatus: connect failed: (%ld, %ld)\n", PR_GetError(), PR_GetOSError()); } PR_Close(sock);#ifdef XP_MAC } /* end of for loop */#endif printf( "PASS\n"); return 0; }}/* * TCP Server * Server Thread * Accept a connection from the client and write some data */static void PR_CALLBACKTCP_Server(void *arg){ Server_Param *sp = (Server_Param *) arg; PRFileDesc *sockfd, *newsockfd; char data_buf[DATA_BUF_SIZE]; PRIntn rv, bytes_read; sockfd = sp->sp_fd; if ((newsockfd = PR_Accept(sockfd, NULL, PR_INTERVAL_NO_TIMEOUT)) == NULL) { fprintf(stderr,"ERROR - PR_Accept failed: (%d,%d)\n", PR_GetError(), PR_GetOSError()); return; } bytes_read = 0; while (bytes_read != DATA_BUF_SIZE) { rv = PR_Read(newsockfd, data_buf + bytes_read , DATA_BUF_SIZE - bytes_read); if (rv < 0) { fprintf(stderr,"Error - PR_Read failed: (%d, %d)\n", PR_GetError(), PR_GetOSError()); PR_Close(newsockfd); return; } PR_ASSERT(rv != 0); bytes_read += rv; } DPRINTF(("Bytes read from client - %d\n",bytes_read)); rv = PR_Write(newsockfd, data_buf,DATA_BUF_SIZE); if (rv < 0) { fprintf(stderr,"Error - PR_Write failed: (%d, %d)\n", PR_GetError(), PR_GetOSError()); PR_Close(newsockfd); return; } PR_ASSERT(rv == DATA_BUF_SIZE); DPRINTF(("Bytes written to client - %d\n",rv)); PR_Close(newsockfd);}/* * test for successful connection using a non-blocking socket */static PRIntnconnection_success_test(){ PRFileDesc *sockfd = NULL, *conn_fd = NULL; PRNetAddr netaddr; PRInt32 i, rv; PRPollDesc pd; PRSocketOptionData optData; PRThread *thr = NULL;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -