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

📄 udpecho.c

📁 tcp/ip原理的第三卷
💻 C
字号:
/* UDPecho.c - main, UDPecho */

#include <stdio.h>
#include <tiuser.h>

extern int	errno, t_errno;
extern char	*sys_errlist[], *t_errlist[];

/*------------------------------------------------------------------------
 * main - UDP client for ECHO service
 *------------------------------------------------------------------------
 */
int
main(argc, argv)
int	argc;
char	*argv[];
{
	char	*host = "localhost";
	char	*service = "echo";

	switch (argc) {
	case 1:
		host = "localhost";
		break;
	case 3:
		service = argv[2];
		/* FALL THROUGH */
	case 2:
		host = argv[1];
		break;
	default:
		fprintf(stderr, "usage: UDPecho [host [port]]\n");
		exit(1);
	}
	UDPecho(host, service);
	exit(0);
}

/*------------------------------------------------------------------------
 * UDPecho - send input to ECHO service on specified host and print reply
 *------------------------------------------------------------------------
 */
int
UDPecho(host, service)
char	*host;
char	*service;
{
	int	d, cc;			/* descriptor, character count	*/
	struct t_unitdata	*pud;
	int	flags;

	d = clientUDP(host, service, &pud);

	while (fgets(pud->udata.buf, pud->udata.maxlen, stdin)) {
		pud->udata.len = strlen(pud->udata.buf);
		if (t_sndudata(d, pud) < 0)
			errexit("t_sndudata: %s\n", t_errlist[t_errno]);

		cc = t_rcvudata(d, pud, &flags);
		if (cc < 0)
			errexit("TLI receive failed: %s\n",
					t_errlist[t_errno]);
		if (pud->udata.len == pud->udata.maxlen)
			--pud->udata.len;
		pud->udata.buf[pud->udata.len] = '\0';
		fputs(pud->udata.buf, stdout);
	}
}

⌨️ 快捷键说明

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