codeexm2.txt

来自「《计算机网络与因特网》(原书第4版) 作者:Douglas Comer博士 」· 文本 代码 · 共 67 行

TXT
67
字号
/* chatserver.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: chatserver * Purpose: wait for a connection from a chatclient & allow users to chat * Usage:   chatserver <appnum> * *----------------------------------------------------------------------- */intmain(int argc, char *argv[]){	connection	conn;	int		len;	char		buff[BUFFSIZE];	if (argc != 2) {		(void) fprintf(stderr, "usage: %s <appnum>\n", argv[0]);		exit(1);	}	(void) printf("Chat Server Waiting For Connection.\n");	/* wait for a connection from a chatclient */	conn = await_contact((appnum) atoi(argv[1]));	if (conn < 0)		exit(1);		(void) printf("Chat Connection Established.\n");		/* iterate, reading from the client and the local user */	while((len = recvln(conn, buff, BUFFSIZE)) > 0) {		(void) printf(RECEIVED_PROMPT);		(void) fflush(stdout);		(void) write(STDOUT_FILENO, buff, len);				/* send a line to the chatclient */		(void) printf(INPUT_PROMPT);		(void) fflush(stdout);		if ((len = readln(buff, BUFFSIZE)) < 1)			break;		buff[len - 1] = '\n';		(void) send(conn, buff, len, 0);	}	/* iteration ends when EOF found on stdin or chat connection */	(void) send_eof(conn);	(void) printf("\nChat Connection Closed.\n\n");	return 0;}

⌨️ 快捷键说明

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