📄 codeexm3.htm
字号:
<HTML><HEAD><TITLE>NETBOOK - Code Sample EXM3</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 EXM3</H1></CENTER><PRE>/* echoclient.c */#include <stdlib.h>#include <stdio.h>#include <cnaiapi.h>#define BUFFSIZE 256#define INPUT_PROMPT "Input > "#define RECEIVED_PROMPT "Received> "int readln(char *, int);/*----------------------------------------------------------------------- * * Program: echoclient * Purpose: contact echoserver, send user input and print server response * Usage: echoclient <compname> [appnum] * Note: Appnum is optional. If not specified the standard echo appnum * (7) is used. * *----------------------------------------------------------------------- */intmain(int argc, char *argv[]){ computer comp; appnum app; connection conn; char buff[BUFFSIZE]; int expect, received, len; if (argc < 2 || argc > 3) { (void) fprintf(stderr, "usage: %s <compname> [appnum]\n", argv[0]); exit(1); } /* convert the arguments to binary format comp and appnum */ comp = cname_to_comp(argv[1]); if (comp == -1) exit(1); if (argc == 3) app = (appnum) atoi(argv[2]); else if ((app = appname_to_appnum("echo")) == -1) exit(1); /* form a connection with the echoserver */ conn = make_contact(comp, app); if (conn < 0) exit(1); (void) printf(INPUT_PROMPT); (void) fflush(stdout); /* iterate: read input from the user, send to the server, */ /* receive reply from the server, and display for user */ while((len = readln(buff, BUFFSIZE)) > 0) { /* send the input to the echoserver */ (void) send(conn, buff, len, 0); (void) printf(RECEIVED_PROMPT); (void) fflush(stdout); /* read and print same no. of bytes from echo server */ expect = len; for (received = 0; received < expect;) { len = recv(conn, buff, (expect - received) < BUFFSIZE ? (expect - received) : BUFFSIZE, 0); if (len < 0) { send_eof(conn); return 1; } (void) write(STDOUT_FILENO, buff, len); received += len; } (void) printf("\n"); (void) printf(INPUT_PROMPT); (void) fflush(stdout); } /* iteration ends when EOF found on stdin */ (void) send_eof(conn); (void) printf("\n"); return 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"></TD><TR><TD ALIGN="LEFT">Code sample:</TD><TD ALIGN="LEFT">EXM3</td><tr><TD ALIGN="LEFT">Type:</TD><TD ALIGN="LEFT">code sample</TD><TR><TD ALIGN="LEFT">Item:</TD><TD ALIGN="LEFT"><A HREF="./codeEXM3.txt">Code sample EXM3</A> without caption</TD><TR><TD ALIGN="LEFT">Keywords:</TD><TD ALIGN="LEFT">network API</TR></TABLE></tr></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -