一、针对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. systemtap 安装试用

    1. 安装 yum install -y systemtap systemtap-runtime 2. 环境准备    a. 自动安装依赖 stap-prep b. 手动安装依赖 kernel-deb ...

  2. UML基本架构建模--类的术语和概念

     Terms and Concepts 术语和概念   A classis a description of a set of objects that share the same attrib ...

  3. C#类、方法的访问修饰符

    这篇文章主要介绍了C#类的访问修饰符用法,较为详细的分析了C#类的访问修饰符概念与用法,具有一定的参考借鉴价值,需要的朋友可以参考下 本文详细分析了C#类的访问修饰符用法,分享给大家供大家参考.具体用 ...

  4. vi配置

    1.配置文件的位置在目录 /etc/ 下面,有个名为vimrc的文件,这是系统中公共的vim配置文件,对所有用户都有效.而在每个用户的主目录下,都可以自己建立私有的配置文件,命名为:“.vimrc”. ...

  5. 自定义DelegatingHandler为ASP.NET Web Api添加压缩与解压的功能

    HTTP协议中的压缩 Http协议中使用Accept-Encoding和Content-Encoding头来表示期望Response内容的编码和当前Request的内容编码.而Http内容的压缩其实是 ...

  6. vs2012加载T4MVC模板

    (1)在工程项目上右键点击“管理NuGet程序包”,在线搜索T4MVC模板,选择并安装,安装成功后,项目中会添加T4MVC.tt文件及子文件. (2)如果添加了新的控制器,则右击T4MVC.tt文件点 ...

  7. cowboy页面重定向的例子

    创建工程 rebar-creator create-app testCowboy testCowboy_app.erl -module(testCowboy_app). -behaviour(appl ...

  8. 杂项:Hadoop

    ylbtech-杂项:Hadoop Hadoop是一个由Apache基金会所开发的分布式系统基础架构.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储.Ha ...

  9. 第十一章 Helm-kubernetes的包管理器(下)

    11.5.5 开发自己的chart k8s提供了大连官方的chart, 不过要部署微服务,还是需要开发自己的chart: 1  创建chart    Helm会帮助创建目录mychart,并生成各类c ...

  10. Java中的构造方法总结

    Java中的构造方法总结 时间: 2013-11-05 08:49 来源: 未知 作者: 站长部落 点击: 1752 次   今天写代码突然发现Java的构造方法也有不少说法呢,闲来无事,总结一下: ...