Linux下echo与time服务的程序实现
1.网络拓扑结构:
2.源码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LINELEN 128
extern int errno;
int TCPecho(const char *host, const char *service);
int errexit(const char *format,…);
int connectsock(const char *host, const char *service, const char *transport );
int connectTCP(const char *host, const char *service);
int main(int argc, char *argv[]){
char *host= “localhost”;
char *service= “echo”;
switch(argc){
case 1:
host = “localhost”;
break;
case 3:
service = argv[2];
case 2:
host=argv[1];
break;
default:
fprintf(stderr,”usage:TCPecho[host[port]]n”);
exit(1);
}
TCPecho(host,service);
exit(0);
}
int TCPecho(const char *host,const char *service){
char buf[LINELEN+1];
int s,n;
int outchars, inchars;
s=connectTCP(host, service);
while(fgets(buf,sizeof(buf),stdin)){
buf[LINELEN]=’