一、针对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. dict 没有 key 的情况

    如何处理, 请参考下文,  主要是要理解思路, https://www.polarxiong.com/archives/Python-%E6%93%8D%E4%BD%9Cdict%E6%97%B6%E ...

  2. smarty缓存的使用

    <?php require './smarty/Smarty.class.php'; $sm = new Smarty; //$sm->force_compile = true; $sm- ...

  3. xml获取属性值的方法

    sSqlstr += string.Format(@" and businessattr.value('(/Arch/Field[@Name=""发文单位$$" ...

  4. NetCore 下集成SignalR并进行分组处理

    Tips: 1.注意跟普通版Net.MVC的前端处理方式不一样,以前可以connection.start()后直接done里面再做逻辑处理,现在不行了 建议做法是在具体的业务Hub里重写OnConne ...

  5. DS03--栈和队列

    一.学习总结 1 关键词: 逻辑结构,存储结构,抽象数据类型,顺序存储类型,链式存储类型,线性表应用 栈和队列 2 使用思维导图将这些关键词组织起来. 二.PTA实验作业 2.1题目1:符号配对 请编 ...

  6. (转)Inno Setup入门(十)——操作注册表

    本文转载自:http://blog.csdn.net/yushanddddfenghailin/article/details/17250871 有些程序需要随系统启动,或者需要建立某些文件关联等问题 ...

  7. Bootstrap组件系列之福利篇几款好用的组件(推荐)

    引用 :http://www.jb51.net/article/87189.htm 一.时间组件 bootstrap风格的时间组件非常多,你可以在github上面随便搜索“datepicker”关键字 ...

  8. java代码---I/O文件内容复制

    总结:主要是弄清输入流,输出流 输入流和输出流都是针对程序而言,把文件内容读取到程序是输入流 从程序把内容写入文件是输出流 package com.a.b; import java.io.*; imp ...

  9. 同步机制之--java CyclicBarrier 循环栅栏

    CyclicBarrier介绍一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point).在涉及一组固定大小的线程的程序中,这些线程必须不时地互相等待 ...

  10. 启动tomcat服务器,配置CATALINA_HOME和JAVA_HOME

    遇到很多次运行startup.bat后,一个窗口一闪而过的问题,但是从来没去纠正怎样修改配置才是正确的,现在从网上查阅的资料整理如下:tomcat在启动时,会读取环境变量的信息,需要一个CATALIN ...