前言

大病初愈,感谢某人的陪伴,感谢王乐庆同学和赵攀同学的细心照顾。原以为过了第八周就不忙了,却没想到还有明天的党章考试。还是写代码比背党章有意思~趁着服务器还没过期,赶紧把 echo 完成了。关于错误提示和连接 socket 的代码就不贴出来了。

服务器配置

vi /etc/xinetd.d/echo

disable = yes 改成 disable = no ,类似 time 服务,如果没有写的权限,就要 chmod,然后重启 xinetd 服务。

service xinetd restart

Client

UDPecho.c

/* UDPecho.c - main */
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h> #define LINELEN 128 extern int errno; int UDPecho(const char *host, const char *service);
int connectUDP(const char *host, const char *service);
int errexit(const char *format, ...); /*---------------------------------------------------------------------------
* main - UDP client for ECHO service
*---------------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
char *host = "localhost"; /* host to use if none supplied */
char *service = "echo"; /* default service name */
int s, n; /* socket descriptor, read count*/
char buf[LINELEN+1];
int outchars, inchars; switch (argc) {
case 1:
host = "localhost";
break;
case 3:
service = argv[2];
/* FALL THROUGH */
case 2:
host = argv[1];
break;
default:
fprintf(stderr, "usage: UDPecho [host [port]]\n");
exit(1);
}
UDPecho(host, service);
exit(0);
}
/*---------------------------------------------------------------------------
* UDPecho - send input to ECHO service on specified host and print and reply
*---------------------------------------------------------------------------
*/
int UDPecho(const char *host, const char *service)
{
char buf[LINELEN+1]; /* buffer for one line of text */
int s, nchars; /* socket descriptor, read count */ s = connectUDP(host, service); while (fgets(buf, sizeof(buf), stdin)) {
//从命令行读入用户输入的字符
buf[LINELEN] = '\0'; /* insure null-terminated */
nchars = strlen(buf);
(void) write(s, buf, nchars);
//向网络中发送用户所输入的字符
memset(buf, 0, LINELEN);
if (read(s, buf, nchars) < 0) {
//从网络中读取服务器所返回的的字符
errexit("socket read failed: %s\n", strerror(errno));
} fputs(buf, stdout);
}
}

编译运行

然后不使用 xinetd 的自带服务,自己写服务器端的程序:

Server

UDPechod.c

/* UDPechod.c - main */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <errno.h> extern int errno;
#define LINELEN 128 int UDPechod(const char *service);
int passiveUDP(const char *service);
int errexit(const char *format, ...); /*------------------------------------------------------------------------
* main - Iterative UDP server for ECHO service
*------------------------------------------------------------------------
*/
int main(int argc, char *argv[])
{
char *service = "echo"; /* service name or port number */ switch (argc) {
case 1:
break;
case 2:
service = argv[1];
break;
default:
errexit("usage: UDPechod [port]\n");
}
UDPechod(service);
exit(0);
} int UDPechod(const char *service)
{
struct sockaddr_in fsin; /* the from address of a client */
unsigned int alen; /* from-address length */
char buf[LINELEN+1]; /* "input" buffer */
int sock; /* server socket */ sock = passiveUDP(service); while (1) {
alen = sizeof(fsin); if (recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&fsin, &alen) < 0) {
//从已连接的 socket 中获取传递过来的信息
errexit("recvfrom: %s\n", strerror(errno));
} (void) sendto(sock, &buf, sizeof(buf), 0, (struct sockaddr *)&fsin, sizeof(fsin));
//将信息返回
}
}

编译运行

Linux 网络编程: echo Service的更多相关文章

  1. Linux 网络编程: daytime Service

    前言 如果你这段时间过得很舒服,那就证明你荒废以一段时间.如果你这段时间过得很辛苦,那么恭喜,你又进步了.最近入党的事情忙得焦头烂额,博客也拖了好久没写,主要也是因为要装 xinetd 服务一直没装好 ...

  2. Linux网络编程echo多线程服务器

    echo_server服务器多线程版本 #include <unistd.h> #include <stdlib.h> #include <stdio.h> #in ...

  3. linux网络编程echo多进程服务器

    echo_server 多进程版本 #include <unistd.h> #include <stdlib.h> #include <stdio.h> #incl ...

  4. Linux网络编程入门 (转载)

    (一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端         网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户 ...

  5. [转] - Linux网络编程 -- 网络知识介绍

    (一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端         网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户 ...

  6. 【转】Linux网络编程入门

    (一)Linux网络编程--网络知识介绍 Linux网络编程--网络知识介绍客户端和服务端         网络程序和普通的程序有一个最大的区别是网络程序是由两个部分组成的--客户端和服务器端. 客户 ...

  7. 《转》Linux网络编程入门

    原地址:http://www.cnblogs.com/duzouzhe/archive/2009/06/19/1506699.html (一)Linux网络编程--网络知识介绍 Linux网络编程-- ...

  8. Proxy源代码分析——谈谈如何学习Linux网络编程

    Linux是一个可靠性非常高的操作系统,但是所有用过Linux的朋友都会感觉到, Linux和Windows这样的"傻瓜"操作系统(这里丝毫没有贬低Windows的意思,相反这应该 ...

  9. Proxy源代码分析--谈谈如何学习Linux网络编程

    http://blog.csdn.net/cloudtech/article/details/1823531 Linux是一个可靠性非常高的操作系统,但是所有用过Linux的朋友都会感觉到,Linux ...

随机推荐

  1. ACdream群赛1112(Alice and Bob)

    题意:http://acdream.info/problem?pid=1112 Problem Description Here  is Alice and Bob again ! Alice and ...

  2. Android流式布局实现

    查看我的所有开源项目[开源实验室] 欢迎增加我的QQ群:[201055521],本博客client下载[请点击] 摘要 新项目用到了一种全新布局----Android标签流式布局的功能,正好一直说给大 ...

  3. Spring——jar包详解

    org.springframework.aop ——Spring的面向切面编程,提供AOP(面向切面编程)的实现 org.springframework.asm——spring 2.5.6的时候需要a ...

  4. Foundation 学习

    官网 Foundation是个跟bootstrap齐名的前端框架. 移动优先,响应式,最低支持IE8. html+css+jq构建 网格Grid Basic: .row父容器 子元素类.column  ...

  5. JS脚本验证大全

    /** * 2009-10-01 * 贺  臣 * 情  缘 * js各种表单数据验证 *//***************************************************** ...

  6. As Easy As A+B

    Problem Description These days, I am thinking about a question, how can I get a problem as easy as A ...

  7. git代码提交流程

    1.进入我的项目文件夹所在目录: 2.git status 查看我修改过的文件: 3.git add -A 将修改的文件全部添加, git add 文件名  只添加指定的文件名: 4.git comm ...

  8. git创建分支与合并分支

    git branch myfeture 创建分支 git checkout myfeture git add --all git commit -m git push origin myfeture ...

  9. ORA-01045: user XXZY lacks CREATE SESSION privilege; logon denied

    在创建用户时,一般我们都分配connect.dba.resource 角色,但是,为什么登陆时还报错呢 原因:用户角色没有激动 解决:ALTER USER XXXX DEFAULT ROLE &quo ...

  10. 1)phpmyadmin导入数据库大小限制修改

    phpmyadmin默认导入数据库文件大小为2M,但一般网站的数据库导出的文件都会超出这个限制,要导入超过2M的数据库文件就需要手动修改php.ini配置文件!最近做项目遇到这个问题,顺便记录下了解决 ...