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

📄 codeexm1.txt

📁 《计算机网络与因特网》(原书第4版) 作者:Douglas Comer博士 出版社:机械工业出版社 书中例子的代码
💻 TXT
字号:
/* chatclient.c */#include <stdlib.h>#include <stdio.h>#include <cnaiapi.h>#define BUFFSIZE		256#define INPUT_PROMPT		"Input   > "#define RECEIVED_PROMPT		"Received> "int recvln(connection, char *, int);int readln(char *, int);/*----------------------------------------------------------------------- * * Program: chatclient * Purpose: contact a chatserver and allow users to chat * Usage:   chatclient <compname> <appnum> * *----------------------------------------------------------------------- */intmain(int argc, char *argv[]){	computer	comp;	connection	conn;	char		buff[BUFFSIZE];	int		len;	if (argc != 3) {		(void) fprintf(stderr, "usage: %s <compname> <appnum>\n",			       argv[0]);		exit(1);	}	/* convert the compname to binary form comp */	comp = cname_to_comp(argv[1]);	if (comp == -1)		exit(1);	/* make a connection to the chatserver */	conn = make_contact(comp, (appnum) atoi(argv[2]));	if (conn < 0) 		exit(1);	(void) printf("Chat Connection Established.\n");	(void) printf(INPUT_PROMPT);	(void) fflush(stdout);	/* iterate, reading from local user and then from chatserver */	while((len = readln(buff, BUFFSIZE)) > 0) {		buff[len - 1] = '\n';		(void) send(conn, buff, len, 0);				/* receive and print a line from the chatserver */		if ((len = recvln(conn, buff, BUFFSIZE)) < 1)			break;		(void) printf(RECEIVED_PROMPT);		(void) fflush(stdout);		(void) write(STDOUT_FILENO, buff, len);		(void) printf(INPUT_PROMPT);		(void) fflush(stdout);	}	/* iteration ends when stdin or the connection indicates EOF */	(void) printf("\nChat Connection Closed.\n");	(void) send_eof(conn);	exit(0);}

⌨️ 快捷键说明

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