linux下echo与time服务的程序实现
一、针对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服务的程序实现的更多相关文章
- Linux下rsyslog日志收集服务环境部署记录【转】
rsyslog 可以理解为多线程增强版的syslog. 在syslog的基础上扩展了很多其他功能,如数据库支持(MySQL.PostgreSQL.Oracle等).日志内容筛选.定义日志格式模板等.目 ...
- linux下syslog-ng日志集中管理服务部署记录
syslog是Linux系统默认的日志守护进程,默认的syslog配置文件是/etc/syslog.conf文件.syslog守护进程是可配置的,它允许人们为每一种类型的系统信息精确地指定一个存放地点 ...
- Linux下使用Eclipse开发Hadoop应用程序
在前面一篇文章中介绍了如果在完全分布式的环境下搭建Hadoop0.20.2,现在就再利用这个环境完成开发. 首先用hadoop这个用户登录linux系统(hadoop用户在前面一篇文章中创建的),然后 ...
- Linux下通过源码编译安装程序
本文简单的记录了下,在linux下如何通过源码安装程序,以及相关的知识.(大神勿喷^_^) 一.程序的组成部分 Linux下程序大都是由以下几部分组成: 二进制文件:也就是可以运行的程序文件 库文件: ...
- Linux下简单的取点阵字模程序
源:Linux下简单的取点阵字模程序 Linux操作系统下进行简单的图形开发,经常会用到取字模的软件,但是Linux并没有像Windows下的小工具可用,我们也并不希望为了取字模而频繁地切换操作系统. ...
- 【转】在嵌入式Linux和PC机Linux下使用popen函数时,程序运行结果有差异。
下面程序演示了在嵌入式Linux和PC机Linux下使用popen函数时,程序的运行结果是有差异的. 两个程序 atest.c 和 btest.c,atest 检查是否有 btest 进程运行,如果没 ...
- linux下,一个运行中的程序,究竟占用了多少内存
linux下,一个运行中的程序,究竟占用了多少内存 1. 在linux下,查看一个运行中的程序, 占用了多少内存, 一般的命令有 (1). ps aux: 其中 VSZ(或VSS)列 表示,程序占用 ...
- Linux下如何不停止服务,清空nohup.out文件
tips:最近发现有不少人在百度这个问题,当初如易我也是初学者,随便从网上搜了一下,就转过来了,不过为了避免搜索结果同质化,为大家提供更翔实的参考,我将nohup.out相关知识整理汇总如下: 1.n ...
- linux下echo命令详解(转)
linux的echo命令, 在shell编程中极为常用, 在终端下打印变量value的时候也是常常用到的, 因此有必要了解下echo的用法 echo命令的功能是在显示器上显示一段文字,一般起到一个 ...
随机推荐
- java-文件流正确关闭资源
用文件流来拷贝一个文件,用到文件字节输入流(FileInputStream)和文件字节输出流(FileOutputStream),用输入流把字节文件读到缓冲数组中,然后将缓冲数组中的字节写到文件中,就 ...
- webpack libray 参考例子
备注: 使用webpack 进行模块导出,方便进行通信 1. 项目初始化 ├── main.js ├── package.json ├── show.js ├── webpack.config.j ...
- thinkphp 使每一个模板页都包括一个header文件和一个footer文件
在开发的过程中,常常遇到要使每一个模板页都包括一个header文件和一个footer文件.thinkPHP的模板布局为我们提供了一个叫全局配置方式可以解决问题. 1. 在配置文件里开启LAYOUT_O ...
- Ipython notebook 一些技巧
在模块后面输入:?,运行可以显示说明: 输入:??,运行可以显示源代码. 输入%matplotlib inline将matplotlib库导入,要显示的图片就可以嵌入到网页中了 %prun用于代码的执 ...
- Openfire源码使用Install4j打包
https://www.ej-technologies.com/download/install4j/files 下载并安装install4jhttps://www.ej-technologies.c ...
- textArea中的maxlength是无效的 解决办法
-------------------------------------------------------------------------------------- <s:texta ...
- C# datatable竖行转换的问题
这次在做项目中,遇到了这样一个问题:datable中列头的名字是不确定的,从数据库中动态查出来的,假设为typeDATA,行的数据中又包含了列头的信息,并按固定的字段分组,当查处行的数据之后用来填充每 ...
- 窗口点击模拟a
[问题描述] 在计算机屏幕上,有N 个窗口.窗口的边界上的点也属于该窗口.窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示位于顶层的窗口里的内容.当你用鼠标点击屏幕上一个点的时候,若其在窗口 ...
- java代码-------Runnable的用法
总结:主要是实现Runnable接口就必须重写run()方法,然后需要创建Thread类的对象,再调用start()方法 package com.s.x; public class testRunna ...
- JVM内存管理和问题简要分析学习
Java中我们基本上不会显式地调用分配内存的函数,分配内存和回收内存都由JVM自动完成了. 所谓物理内存就是我们通常说的RAM(随机存储器),计算机中还有一个存储单元叫做寄存器,用于存储计算单 ...