⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 code30_1.htm

📁 《计算机网络与因特网》(原书第4版) 作者:Douglas Comer博士 出版社:机械工业出版社 书中例子的代码
💻 HTM
字号:
<HTML><HEAD><TITLE>NETBOOK - Code Sample 30_1</TITLE></HEAD><BODY BGCOLOR="#FFFFFF"><P><TABLE BORDER="0" CELLPADDING="5" CELLSPACING="0" WIDTH="600"><TR>  <TD WIDTH="112" VALIGN="TOP" BGCOLOR="#759AC4" CELLPADDING="0"><img src="../pgmatrls/sidebar.gif" width="112" height="410" usemap="#sidebar" ismap alt="" border="0" ><map name="sidebar"><area shape="rect" coords="0,325,100,364"  href="../othrpags/page22.htm" ><area shape="rect" coords="0,287,100,326"  href="../othrpags/page17.htm" ><area shape="rect" coords="0,248,100,287"  href="../othrpags/page18.htm" ><area shape="rect" coords="0,364,100,404"  href="../othrpags/code.htm" ><area shape="rect" coords="0,210,100,248"  href="../othrpags/page15.htm" ><area shape="rect" coords="0,170,100,210"  href="../othrpags/page03.htm" ><area shape="rect" coords="0,132,100,170"  href="../othrpags/page10.htm" ><area shape="rect" coords="0,93,100,132"   href="../index.htm" ><AREA SHAPE="DEFAULT" NOHREF></map>  </TD><TD WIDTH="420" VALIGN="TOP" BGCOLOR="#FFFFFF"><CENTER><H1>Code Sample 30_1</H1></CENTER><PRE>/* client.c - code for example client program that uses TCP */#ifndef unix#define WIN32#include &lt;windows.h&gt;#include &lt;winsock.h&gt;#else#define closesocket close#include &lt;sys/types.h&gt;#include &lt;sys/socket.h&gt;#include &lt;netinet/in.h&gt;#include &lt;arpa/inet.h&gt;#include &lt;netdb.h&gt;#endif#include &lt;stdio.h&gt;#include &lt;string.h&gt;#define PROTOPORT       5193            /* default protocol port number */extern  int             errno;char    localhost[] =   "localhost";    /* default host name            *//*------------------------------------------------------------------------ * Program:   client * * Purpose:   allocate a socket, connect to a server, and print all output * * Syntax:    client [ host [port] ] * *               host  - name of a computer on which server is executing *               port  - protocol port number server is using * * Note:      Both arguments are optional.  If no host name is specified, *            the client uses "localhost"; if no protocol port is *            specified, the client uses the default given by PROTOPORT. * *------------------------------------------------------------------------ */main(argc, argv)int     argc;char    *argv[];{        struct  hostent  *ptrh;  /* pointer to a host table entry       */        struct  protoent *ptrp;  /* pointer to a protocol table entry   */        struct  sockaddr_in sad; /* structure to hold an IP address     */        int     sd;              /* socket descriptor                   */        int     port;            /* protocol port number                */        char    *host;           /* pointer to host name                */        int     n;               /* number of characters read           */        char    buf[1000];       /* buffer for data from the server     */#ifdef WIN32        WSADATA wsaData;        WSAStartup(0x0101, &wsaData);#endif        memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */        sad.sin_family = AF_INET;         /* set family to Internet     */        /* Check command-line argument for protocol port and extract    */        /* port number if one is specified.  Otherwise, use the default */        /* port value given by constant PROTOPORT                       */        if (argc &gt; 2) {                 /* if protocol port specified   */                port = atoi(argv[2]);   /* convert to binary            */        } else {                port = PROTOPORT;       /* use default port number      */        }        if (port &gt; 0)                   /* test for legal value         */                sad.sin_port = htons((u_short)port);        else {                          /* print error message and exit */                fprintf(stderr,"bad port number %s\n",argv[2]);                exit(1);        }        /* Check host argument and assign host name. */        if (argc &gt; 1) {                host = argv[1];         /* if host argument specified   */        } else {                host = localhost;        }        /* Convert host name to equivalent IP address and copy to sad. */        ptrh = gethostbyname(host);        if ( ((char *)ptrh) == NULL ) {                fprintf(stderr,"invalid host: %s\n", host);                exit(1);        }        memcpy(&sad.sin_addr, ptrh-&gt;h_addr, ptrh-&gt;h_length);        /* Map TCP transport protocol name to protocol number. */        if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) {                fprintf(stderr, "cannot map \"tcp\" to protocol number");                exit(1);        }        /* Create a socket. */        sd = socket(PF_INET, SOCK_STREAM, ptrp-&gt;p_proto);        if (sd &lt; 0) {                fprintf(stderr, "socket creation failed\n");                exit(1);        }        /* Connect the socket to the specified server. */        if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) &lt; 0) {                fprintf(stderr,"connect failed\n");                exit(1);        }        /* Repeatedly read data from socket and write to user's screen. */        n = recv(sd, buf, sizeof(buf), 0);        while (n &gt; 0) {                write(1,buf,n);                n = recv(sd, buf, sizeof(buf), 0);        }        /* Close the socket. */        closesocket(sd);        /* Terminate the client program gracefully. */        exit(0);}</PRE><p><p><p><HR WIDTH=89%  ALIGN=CENTER SIZE=2><center><h2>Item information</h2></center><p><TABLE CELLSPACING="0"><TD ALIGN="LEFT" VALIGN="TOP">Caption:</TD><TD ALIGN="LEFT">Code sample for client in section 30.6</TD><TR><TD ALIGN="LEFT">Code sample:</TD><TD ALIGN="LEFT">30_1</td><tr><TD ALIGN="LEFT">Type:</TD><TD ALIGN="LEFT">code sample</TD><TR><TD ALIGN="LEFT">Item:</TD><TD ALIGN="LEFT"><A HREF="./code30_1.txt">Code sample 30_1</A> without caption</TD><TR><TD ALIGN="LEFT">Keywords:</TD><TD ALIGN="LEFT">client, client code</TR></TABLE></tr></TABLE></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -