linux开启echo服务,Linux下echo与time服务的程序实现

一、针对ECHO服务的TCP客户软件的实现 1.网络拓扑结构: 2.源码: #include lt;stdio.hgt;#include lt;stdlib.hgt;#include

一、针对ECHO服务的TCP客户软件的实现

1.网络拓扑结构:

bc3addfed24f0ae615298c075c876247.png

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]='\0';

outchars=strlen(buf);

(void)write(s,buf,outchars);

for(inchars=0;inchars

n=read(s,&buf[inchars],outchars-inchars);

if(n<0)

errexit("socker read failed: %s\n",strerror(errno));

}

fputs(buf,stdout);

}

}

int errexit(const char *format,...){

va_list arg;

va_start(arg, format);

vfprintf(stderr,format,arg);

va_end(arg);

exit(1);

}

int connectsock(const char *host, const char *service, const char *transport )

/*

* Arguments:

*      host      - name of host to which connection is desired

*      service  - service associated with the desired port

*      transport - name of transport protocol to use ("tcp" or "udp")

*/

{

struct hostent  *phe;  /* pointer to host information entry    */

struct servent  *pse;  /* pointer to service information entry */

struct protoent *ppe;  /* pointer to protocol information entry*/

struct sockaddr_in sin; /* an Internet endpoint address    */

int s, type;    /* socket descriptor and socket type    */

memset(&sin, 0, sizeof(sin));

sin.sin_family = AF_INET;

/* Map service name to port number */

if ( pse = getservbyname(service, transport) )

sin.sin_port = pse->s_port;

else if ((sin.sin_port=htons((unsigned short)atoi(service))) == 0)

errexit("can't get \"%s\" service entry\n", service);

/* Map host name to IP address, allowing for dotted decimal */

if ( phe = gethostbyname(host) )

memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);

else if ( (sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE )

errexit("can't get \"%s\" host entry\n", host);

/* Map transport protocol name to protocol number */

if ( (ppe = getprotobyname(transport)) == 0)

errexit("can't get \"%s\" protocol entry\n", transport);

/* Use protocol to choose a socket type */

if (strcmp(transport, "udp") == 0)

type = SOCK_DGRAM;

else

type = SOCK_STREAM;

/* Allocate a socket */

s = socket(PF_INET, type, ppe->p_proto);

if (s < 0)

errexit("can't create socket: %s\n", strerror(errno));

/* Connect the socket */

if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)

errexit("can't connect to %s.%s: %s\n", host, service,

strerror(errno));

return s;

}

int connectTCP(const char *host, const char *service){

return connectsock(host,service,"tcp");

}

二、针对echo服务的并发的面向连接的服务器软件的实现

1.网络拓扑结构:

a379bcaedaef140e84e6149c02a5cf46.png

2.源码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值