<Beginning Linux Programming_4th>  chapter 15 Sockets

1  A simple local client/server

1)  client.c

// 1) header files
// int socket(int domain, int type, int protocol);
#include <sys/types.h>
// int connect(int socket, const struct sockaddr*address,size_t address_len);
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
/* struct sockaddr_in{
short int sin_family; // AF_INET
unsigned short int sin_port; // Port number
struct in_addr sin_addr; //Internet address
}; */
#include <netinet/in.h>
#include <arpa/inet.h>
// size_t write(int fildes, const void *buf, size_t nbytes);
// size_t read(int fildes, const void *buf, size_t nbytes);
#include <unistd.h> // 2) socket
int main ()
{
// 127.0.0.1 loopback network
// 192.168.1.1 local area network via an Ethernet adaptor
// 158.152.x.x modem link to an Internet service provider
int sockfd;
int len;
struct sockaddr_in address;
int result;
char ch = 'A'; /* struct in_addr{
unsigned long int s_addr;
}; */
sockfd = socket(AF_INET, SOCK_STREAM, );
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons();
len = sizeof(address);
/* sizeof(变量/数据类型); // 结果是一个整数
变量或此种数据类型的变量所占的内存空间字节数
printf("the size of char is %d bytes\n", sizeof(char)); */
// 3) connect
result = connect(sockfd, (struct sockaddr*) &address, len);
if(result == -)
{
perror("oops: client");
exit();
} // 4) write, read and close
write(sockfd, &ch, );
read(sockfd, &ch, );
printf("char from server = %c\n", ch); close(sockfd);
exit();
} /* convert 16-bit/32-bit integers between host and network ordering
<netinet/in.h>
unsigned long int htol(unsigned long int hostlong);
unsigned short int htos(unsigned short int hostshort);
unsigned long int ntohl(unsigned long int netlong);
unsigned short int ntohs(unsigned short int netshort);

2)  server.c

// 1)  header files
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h> // 2) socket
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len; struct sockaddr_in server_address;
struct sockaddr_in client_address;
unlink("server_socket"); server_sockfd = socket(AF_INET, SOCK_STREAM, );
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons();
server_len = sizeof(server_address); // 3) bind, listen
bind(server_sockfd, (struct sockaddr*)&server_address, server_len);
listen(server_sockfd, ); // 4) while loop: accept - read/write - close
while()
{
char ch;
printf("server waiting\n");
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr*)&client_address, &client_len); read(client_sockfd, &ch, );
ch++;
write(client_sockfd, &ch,); close(client_sockfd);
}
}
// int bind(int socket, const struct sockaddr* address, size_t address_len);
// int listen(int socket, int backlog);
// int accept(int socket, struct sockaddr* address, size_t* address_len);

2  A server for multiple clients

server2.c

// 1)  header files
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h> // 2) socket
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len; struct sockaddr_in server_address;
struct sockaddr_in client_address; server_sockfd = socket(AF_INET, SOCK_STREAM, );
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons();
server_len = sizeof(server_address); // 3) bind, listen, signal
bind(server_sockfd, (struct sockaddr*)&server_address, server_len);
listen(server_sockfd, );
signal(SIGCHLD, SIG_IGN); // 4) while loop: accept - fork
while()
{
char ch;
printf("server waiting\n");
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr*)&client_address, &client_len); if(fork() == )
{
read(client_sockfd, &ch, );
sleep();
ch++;
write(client_sockfd, &ch,);
close(client_sockfd);
exit();
}
else
{
close(client_sockfd);
}
}
}

3  An improved server for multiple clients

server3.c

// header files
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <unistd.h> // 1) socket
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len; struct sockaddr_in server_address;
struct sockaddr_in client_address; int result;
fd_set readfds, testfds; server_sockfd = socket(AF_INET, SOCK_STREAM, );
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons();
server_len = sizeof(server_address); // 2) bind, listen
bind(server_sockfd, (struct sockaddr*)&server_address, server_len);
listen(server_sockfd, );
FD_ZERO(&readfds);
FD_SET(server_sockfd, &readfds); // 3) while loop: server activity, client activity
while()
{
char ch;
int fd;
int nread;
testfds = readfds;
printf("server waiting\n");
result = select(FD_SETSIZE, &testfds, (fd_set*), (fd_set*), (struct timeval*)); if(result<){
perror("server");
exit();
} for(fd=; fd<FD_SETSIZE; fd++)
{
if(FD_ISSET(fd, &testfds))
{
if(fd==server_sockfd)
{
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr*)&client_address, &client_len);
FD_SET(client_sockfd, &readfds);
printf("adding client an fd %d\n", client_sockfd);
}
else
{
ioctl(fd, FIONREAD, &nread); if(nread==)
{
close(fd);
FD_CLR(fd, &readfds);
printf("removing client on fd %d\n", fd);
}
else
{
read(fd, &ch, );
sleep();
printf("serving client on fd %d\n", fd);
ch++;
write(fd, &ch, );
}
} // 1st if
} // 2nd if
} //for
} // while
} // main

小结:

client: socket -- connect --r/w -- close

server: socket -- bind -- listen -- accept -- r/w -- close

while(1)

server2: socket -- bind -- listen -- signal -- accept -- fork

while(1)

Network client/server的更多相关文章

  1. Client–server model

    Client–server model From Wikipedia, the free encyclopedia The client–server model of computing ] Oft ...

  2. Latency Compensating Methods in Client/Server In-game Protocol Design and Optimization【转】

    https://developer.valvesoftware.com/wiki/Latency_Compensating_Methods_in_Client/Server_In-game_Proto ...

  3. 深入浅出 Redis client/server交互流程

    综述 最近笔者阅读并研究redis源码,在redis客户端与服务器端交互这个内容点上,需要参考网上一些文章,但是遗憾的是发现大部分文章都断断续续的非系统性的,不能给读者此交互流程的整体把握.所以这里我 ...

  4. AndroidAsync :异步Socket,http(client+server),websocket和socket.io的Android类库

    AndroidAsync是一个用于Android应用的异步Socket,http(client+server),websocket和socket.io的类库.基于NIO,没有线程.它使用java.ni ...

  5. Linux SocketCan client server demo hacking

    /*********************************************************************** * Linux SocketCan client se ...

  6. NetMQ(ZeroMQ)Client => Server => Client 模式的实现

    ØMQ (也拼写作ZeroMQ,0MQ或ZMQ)是一个为可伸缩的分布式或并发应用程序设计的高性能异步消息库.它提供一个消息队列, 但是与面向消息的中间件不同,ZeroMQ的运行不需要专门的消息代理(m ...

  7. Java Client/Server 基础知识

    Java的网络类库支持多种Internet协议,包括Telnet, FTP 和HTTP (WWW),与此相对应的Java网络类库的子类库为: Java.net  Java.net.ftp  Java. ...

  8. DTLS协议中client/server的认证过程和密钥协商过程

    我的总结:DTLS的握手就是协商出一个对称加密的秘钥(每个客户端的秘钥都会不一样),之后的通信就要这个秘钥进行加密通信.协商的过程要么使用非对称加密算法进行签名校验身份,要么通过客户端和服务器各自存对 ...

  9. [并发并行]_[线程模型]_[Pthread线程使用模型之三 客户端/服务端模型(Client/Server]

    Pthread线程使用模型之三 客户端/服务端模型(Client/Server) 场景 1.在客户端/服务端模型时,客户端向服务端请求一些数据集的操作. 服务端执行执行操作独立的(多进程或跨网络)– ...

随机推荐

  1. 用Perl编写Apache模块续 - SVNAuth

    目标:以整合各类项目管理系统中的用户体系及权限体系为目标,实现SVN的集成式动态鉴权 支持平台:usvn.禅道等 - 开发中,本文仅对前期的探索工作做点整理 开发环境: Windows.XAMPP 1 ...

  2. 启动Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误

    启动Eclipse 弹出"Failed to load the JNI shared library jvm.dll"错误,如下 原因:eclipse的版本与jre或者jdk版本不 ...

  3. Maven仓库分类

    MAVEN仓库分类 Maven仓库分为:本地仓库+远程仓库两大类 远程仓库又分为:中央仓库+私服+其它公共远程仓库 1,在Maven中,任何一个依赖.插件或者项目构建的输出,都可以称之为构件 2,Ma ...

  4. 基于 ANSIBLE 自动化运维实践

    摘要:运维这个话题很痛苦,你做任何的产品都离不开运维.不管你用什么语言.什么平台.什么技术,真正能够决定你产品成熟度的很有可能就是你运维的能力.取自 云巴 CEO 张虎在 ECUG 大会上的分享. 云 ...

  5. linux环形buff模拟多线程信号量操作

    互斥锁mutex变量的值非0即1,只能用来表示两种状态下的临界资源.而信号量是与之类似的,用来表示可用资源的,区别在于,信号量可以表示多个可用资源的. --值为2的信号量也就是特殊的互斥锁了. 那么下 ...

  6. css复习笔记

    margin: 1. 当有三个值时第一个值为上,第二个值为左右,第三个值为下. 2.margin外边距折叠,水平没有,垂直会折叠,且折叠后以两者最大值为准.另外,当一个元素包裹另一元素时也会发生折叠. ...

  7. 上传Android代码到Jcenter(解决了字符映射的问题)

    请先阅读:http://blog.saymagic.cn/2015/02/16/release-library-to-jcenter.html 最外面的build.gradle // Top-leve ...

  8. iOS之UI--关于modal

    modal的效果展示: 关于modal的涉及到的知识点: 1.当一个控制器被销毁的时候,它里面所有子控制器的业务逻辑都不能够处理 2.当一个控制器被销毁的时候,它里面所有子控件的业务逻辑都不能够处理 ...

  9. 用Leangoo做敏捷需求管理-敏捷团队协作

    传统的瀑布工作模式使用详细的需求说明书来表达需求,需求人员负责做需求调研,根据调研情况编制详细的需求说明书,进行需求评审,评审之后签字确认交给研发团队设计开发.在这样的环境下,需求文档是信息传递的主体 ...

  10. C语言指针学习

    C语言学过好久了,对于其中的指针却没有非常明确的认识,趁着有机会来好好学习一下,总结一下学过的知识,知识来自C语言指针详解一文 一:指针的概念 指针是一个特殊的变量,里面存储的数值是内存里的一个地址. ...