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

1.网络拓扑结构:

2.源码:

 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h> #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 :
host = "localhost";
break;
case :
service = argv[];
case :
host=argv[];
break;
default:
fprintf(stderr,"usage:TCPecho[host[port]]\n");
exit();
}
TCPecho(host,service);
exit();
}
int TCPecho(const char *host,const char *service){
char buf[LINELEN+];
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=;inchars<outchars;inchars+=n){
n=read(s,&buf[inchars],outchars-inchars);
if(n<)
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();
}
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, , 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))) == )
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)) == )
errexit("can't get \"%s\" protocol entry\n", transport); /* Use protocol to choose a socket type */
if (strcmp(transport, "udp") == )
type = SOCK_DGRAM;
else
type = SOCK_STREAM; /* Allocate a socket */
s = socket(PF_INET, type, ppe->p_proto);
if (s < )
errexit("can't create socket: %s\n", strerror(errno)); /* Connect the socket */
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < )
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.网络拓扑结构:

2.源码:

 #define _USE_BSD
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <signal.h>
#include <errno.h> #define QLEN 32
#define BUFSIZE 4096
extern int errno;
unsigned short portbase = ; void reaper(int);
int TCPechod(int fd);
int errexit(const char *format,...);
int passivesock(const char *service, const char *transport, int qlen);
int passiveTCP(const char *service,int qlen); int main(int argc, char *argv[]){
char *service= "echo";
struct sockaddr_in fsin;
unsigned int alen;
int msock,ssock;
switch(argc){
case :
break;
case :
service=argv[];
break;
default:
errexit("usage: TCPechod [port]\n");
} msock=passiveTCP(service,QLEN);
(void)signal(SIGCHLD,(__sighandler_t)QLEN); while(){
alen=sizeof(fsin);
ssock=accept(msock,(struct sockaddr *)&fsin,&alen);
if(ssock<){
if(errno==EINTR) continue;
errexit("accept: %s\n",strerror(errno));
}
switch(fork()){
case :
(void)close(msock);
exit(TCPechod(ssock));
default:
(void)close(ssock);
break;
case -:
errexit("fork: %s\n",strerror(errno));
}
}
} void reaper(int sig){
int status;
while(wait3(&status,WNOHANG,(struct rusage *))>=) ;
}
int TCPechod(int fd){
char buf[BUFSIZ];
int cc; while(cc=read(fd,buf,sizeof(buf))){
if(cc<)
errexit("echo read: %s\n",strerror(errno));
if(write(fd,buf,cc)<)
errexit("echo write: %s\n",strerror(errno));
}
return ;
}
int errexit(const char *format,...){
va_list arg;
va_start(arg, format);
vfprintf(stderr,format,arg);
va_end(arg);
exit();
}
int passivesock(const char *service, const char *transport, int qlen)
/*
* Arguments:
* service - service associated with the desired port
* transport - transport protocol to use ("tcp" or "udp")
* qlen - maximum server request queue length
*/
{ 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, , sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY; /* Map service name to port number */
if ( pse = getservbyname(service, transport) )
sin.sin_port = htons(ntohs((unsigned short)pse->s_port)+ portbase);
else
if ((sin.sin_port=htons((unsigned short)atoi(service)+portbase)) == )
errexit("can't create passive service %d \n",sin.sin_port); /* Map protocol name to protocol number */
if ( (ppe = getprotobyname(transport)) == )
errexit("can't get \"%s\" protocol entry\n", transport); /* Use protocol to choose a socket type */
if (strcmp(transport, "udp") == )
type = SOCK_DGRAM;
else
type = SOCK_STREAM; /* Allocate a socket */
s = socket(PF_INET, type, ppe->p_proto);
if (s < )
errexit("can't create socket: %s\n", strerror(errno)); /* Bind the socket */
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < )
errexit("can't bind to %s port: %s\n", service,
strerror(errno));
if (type == SOCK_STREAM && listen(s, qlen) < )
errexit("can't listen on %s port: %s\n", service,
strerror(errno));
return s;
}
int passiveTCP(const char *service,int qlen){
return passivesock(service,"tcp",qlen);
}

三、针对TIME服务的UDP客户软件的实现

1.网络拓扑结构:

2.源码:

 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <errno.h> #define BUFSIZE 64
#define UNIXEPOCH 2208988800UL
#define MSG "what time is it?\n"
extern int errno; int errexit(const char *format,...);
int connectsock(const char *host, const char *service, const char *transport );
int connectUDP(const char *host, const char *service); int main(int argc, char *argv[]){
char *host= "localhost";
char *service= "time";
time_t now;
int s,n; switch(argc){
case :
host = "localhost";
break;
case :
service = argv[];
case :
host=argv[];
break;
default:
fprintf(stderr,"usage: UDPtime[host[port]]\n");
exit();
} s=connectUDP(host,service);
(void)write(s,MSG,strlen(MSG)); n=read(s,(char *)&now,sizeof(now));
if(n<) errexit("read failed: %s\n",strerror(errno));
now=ntohl((unsigned long)now);
now-=UNIXEPOCH;
printf("%s",ctime(&now));
exit();
}
int errexit(const char *format,...){
va_list arg;
va_start(arg, format);
vfprintf(stderr,format,arg);
va_end(arg);
exit();
}
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, , 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))) == )
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)) == )
errexit("can't get \"%s\" protocol entry\n", transport); /* Use protocol to choose a socket type */
if (strcmp(transport, "udp") == )
type = SOCK_DGRAM;
else
type = SOCK_STREAM; /* Allocate a socket */
s = socket(PF_INET, type, ppe->p_proto);
if (s < )
errexit("can't create socket: %s\n", strerror(errno)); /* Connect the socket */
if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < )
errexit("can't connect to %s.%s: %s\n", host, service,
strerror(errno));
return s;
}
int connectUDP(const char *host, const char *service){
return connectsock(host,service,"udp");
}

四、针对TIME服务的UDP服务器端软件的实现

1.网络拓扑结构:

2.源码:

 #include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h> #define UNIXEPOCH 2208988800UL extern int errno;
unsigned short portbase = ; int errexit(const char *format,...);
int passivesock(const char *service, const char *transport, int qlen);
int passiveUDP(const char *service); int main(int argc, char *argv[]){
char *service= "time";
struct sockaddr_in fsin;
char buf[];
int sock;
time_t now;
unsigned int alen; switch(argc){
case :
break;
case :
service=argv[];
break;
default:
errexit("usage: UDPtimed [port]\n");
} sock=passiveUDP(service); while(){
alen=sizeof(fsin);
if(recvfrom(sock,buf,sizeof(buf),,(struct sockaddr *)&fsin,&alen)<)
errexit("recvfrom: %s\n",strerror(errno));
(void)time(&now);
now=htonl((unsigned long)(now+UNIXEPOCH));
(void)sendto(sock,(char*)&now,sizeof(now),,(struct sockaddr *)&fsin,sizeof(fsin));
}
} int errexit(const char *format,...){
va_list arg;
va_start(arg, format);
vfprintf(stderr,format,arg);
va_end(arg);
exit();
}
int passivesock(const char *service, const char *transport, int qlen)
/*
* Arguments:
* service - service associated with the desired port
* transport - transport protocol to use ("tcp" or "udp")
* qlen - maximum server request queue length
*/
{ 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, , sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY; /* Map service name to port number */
if ( pse = getservbyname(service, transport) )
sin.sin_port = htons(ntohs((unsigned short)pse->s_port)+ portbase);
else
if ((sin.sin_port=htons((unsigned short)atoi(service)+portbase)) == )
errexit("can't create passive service %d \n",sin.sin_port); /* Map protocol name to protocol number */
if ( (ppe = getprotobyname(transport)) == )
errexit("can't get \"%s\" protocol entry\n", transport); /* Use protocol to choose a socket type */
if (strcmp(transport, "udp") == )
type = SOCK_DGRAM;
else
type = SOCK_STREAM; /* Allocate a socket */
s = socket(PF_INET, type, ppe->p_proto);
if (s < )
errexit("can't create socket: %s\n", strerror(errno)); /* Bind the socket */
if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < )
errexit("can't bind to %s port: %s\n", service,
strerror(errno));
if (type == SOCK_STREAM && listen(s, qlen) < )
errexit("can't listen on %s port: %s\n", service,
strerror(errno));
return s;
}
int passiveUDP(const char *service){
return passivesock(service,"udp",);
}

这里是用的我实验时的代码,前两个是关于echo服务的客户端与服务器端,有下面运行截图:

后两个是关于time服务的,有下面运行截图:

实验时由于多次运行验证,总会出现端口占用的情况,于是这里每次运行时都输入程序的入口参数(就是main函数里的形参),自选端口,方便至极。还有就是代码里多个函数可以写入多个cpp里,这里偷懒了。

linux下echo与time服务的程序实现的更多相关文章

  1. Linux下rsyslog日志收集服务环境部署记录【转】

    rsyslog 可以理解为多线程增强版的syslog. 在syslog的基础上扩展了很多其他功能,如数据库支持(MySQL.PostgreSQL.Oracle等).日志内容筛选.定义日志格式模板等.目 ...

  2. linux下syslog-ng日志集中管理服务部署记录

    syslog是Linux系统默认的日志守护进程,默认的syslog配置文件是/etc/syslog.conf文件.syslog守护进程是可配置的,它允许人们为每一种类型的系统信息精确地指定一个存放地点 ...

  3. Linux下使用Eclipse开发Hadoop应用程序

    在前面一篇文章中介绍了如果在完全分布式的环境下搭建Hadoop0.20.2,现在就再利用这个环境完成开发. 首先用hadoop这个用户登录linux系统(hadoop用户在前面一篇文章中创建的),然后 ...

  4. Linux下通过源码编译安装程序

    本文简单的记录了下,在linux下如何通过源码安装程序,以及相关的知识.(大神勿喷^_^) 一.程序的组成部分 Linux下程序大都是由以下几部分组成: 二进制文件:也就是可以运行的程序文件 库文件: ...

  5. Linux下简单的取点阵字模程序

    源:Linux下简单的取点阵字模程序 Linux操作系统下进行简单的图形开发,经常会用到取字模的软件,但是Linux并没有像Windows下的小工具可用,我们也并不希望为了取字模而频繁地切换操作系统. ...

  6. 【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。

    下面程序演示了在嵌入式Linux和PC机Linux下使用popen函数时,程序的运行结果是有差异的. 两个程序 atest.c 和 btest.c,atest 检查是否有 btest 进程运行,如果没 ...

  7. linux下,一个运行中的程序,究竟占用了多少内存

    linux下,一个运行中的程序,究竟占用了多少内存 1. 在linux下,查看一个运行中的程序, 占用了多少内存, 一般的命令有 (1). ps aux: 其中  VSZ(或VSS)列 表示,程序占用 ...

  8. Linux下如何不停止服务,清空nohup.out文件

    tips:最近发现有不少人在百度这个问题,当初如易我也是初学者,随便从网上搜了一下,就转过来了,不过为了避免搜索结果同质化,为大家提供更翔实的参考,我将nohup.out相关知识整理汇总如下: 1.n ...

  9. linux下echo命令详解(转)

      linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法 echo命令的功能是在显示器上显示一段文字,一般起到一个 ...

随机推荐

  1. tomcat  nginx  证书切换

    1. 导出公钥 keytool -export -alias tomcat -keystore <you jks>wsriakey.keystore -file <outputfil ...

  2. 让nodejs 支持 es6 import

    备注:    尽管nodejs 新版本已经支持es6 的好多特性了,但是还是有部分不支持,为了使用,实际上我们有一个 比较强大工具 bable,下面介绍几个比较简单的用法. 1. bable-cli ...

  3. 在linux修改文件夹及其子文件夹的权限。

    加入-R 参数,就可以将读写权限传递给子文件夹例如chmod -R 777 /home/mypackage那么mypackage 文件夹和它下面的所有子文件夹的属性都变成了777.777是读.写.执行 ...

  4. Oracle11g的服务

    成功安装Oracle 11g数据库后,你会发现自己电脑运行速度会变慢,配置较低的电脑甚至出现非常卡的状况,通过禁止非必须开启的Oracle服务可以提升电脑的运行速度.那么,具体该怎么做呢.按照win7 ...

  5. RabbitMQ消息的消费与持久化

    作为消费者的客户端要消费Rabbitmq的消息,首先要建立与它某个队列的连接,具体连接时可指定队列的BindingKey和关系的exchange标识,Rabbitmq判断若已有队列通过BindingK ...

  6. centos7下安装oracle11gR2

    Centos7安装oracle11gR2说明 Centos7安装oracle11gR2说明 环境准备 安装Oracle前准备 创建运行oracle数据库的系统用户和用户组 创建oracle数据库安装目 ...

  7. tar 打包处理文件

    基本格式:tar [Options] file_archive //注意tar的第一参数必须为命令选项,即不能直接接待处理文件 Option是由三部分组成,分别是操作类型(创建,查看,解压),压缩处理 ...

  8. bzoj 2039 [2009国家集训队]employ人员雇佣——二元关系

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2039 用最小割看.对于一组关系 i , j ,如果都选,收益 2*Ei,j,可以看作0,作为 ...

  9. 对SQLite数据库操作 操作db文件

    sqlite数据库就是一个DB文件.  程序每操作一次数据库都要读一次 .DB  文件 .  这个文件就是这个SQLite数据库. 如果需要依赖包的可以联系我 工具类: package com.hot ...

  10. 问题排查-JVM堆外内存问题排查

    首先确认堆占用 jmap 查看heap内存使用情况 jmap -heap pid 1 可以查看到MetaspaceSize,CompressedClassSpaceSize,MaxMetaSize j ...