Network Programming(1)

It is important to realize that clients and servers are processes and not ma- chines, or hosts as they are often called in this context.

Data received from the network is copied from the adapter across the I/O and memory buses into memory, typically by a DMA transfer.

Similarly, data can also be copied from memory to the network.

IP Addresses

An IP address is an unsigned 32-bit integer. Network programs store IP addresses in the IP address structure shown in Figure 11.9.

#TODO order

Con- ceptually, the DNS database consists of millions of the host entry structures shown in Figure 11.11, each of which defines the mapping between a set of domain names (an official name and a list of aliases) and a set of IP addresses.

The gethostbyname function returns the host entry associated with the do- main name name.

The gethostbyaddr function returns the host entry associated with the IP address addr. The second argument gives the length in bytes of an IP address, which for the current Internet is always 4 bytes.

Internet Connections

A connection is point-to-point in the sense that it connects a pair of processes. It is full-duplex in the sense that data can flow in both directions at the same time.

The port in the client’s socket address is assigned automatically by the kernel when the client makes a connection request, and is known as an ephemeral port

However, the port in the server’s socket address is typically some well-known port that is associated with the service.

On Unix machines, the file /etc/services contains a comprehensive list of the services provided on that machine, along with their well-known ports.

The Sockets Interface

Socket Address Structures

Internet socket addresses are stored in 16-byte structures of the type sockaddr_in, shown in Figure 11.15.

For Internet applications, the sin_family member is AF_INET, the sin_port member is a 16-bit port number, and the sin_ addr member is a 32-bit IP address. The IP address and port number are always stored in network (big-endian) byte order.

The socket Function

Clients and servers use the socket function to create a socket descriptor.

In our codes, we will always call the socket function with the arguments:

clientfd = Socket(AF_INET, SOCK_STREAM, 0);

where AF_INET indicates that we are using the Internet, and SOCK_STREAM indicates that the socket will be an end point for an Internet connection.

The clientfd descriptor returned by socket is only partially opened and cannot yet be used for reading and writing.

The connect Function

A client establishes a connection with a server by calling the connect function.

The connect function blocks until either the connection is successfully established or an error occurs.

If successful, the sockfd descriptor is now ready for reading and writing.

The bind Function

The remaining sockets functions—bind, listen, and accept—are used by servers to establish connections with clients.

The bind function tells the kernel to associate the server’s socket address in my_addr with the socket descriptor sockfd.

The addrlen argument is sizeof(sockaddr_in).

The listen Function

By default, the kernel assumes that a descriptor created by the socket function corresponds to an active socket that will live on the client end of a connection.

A server calls the listen function to tell the kernel that the descriptor will be used by a server instead of a client.

The listen function converts sockfd from an active socket to a listening socket that can accept connection requests from clients.

The backlog argument is a hint about the number of outstanding connection requests that the kernel should queue up before it starts to refuse requests.

The exact meaning of the backlog argument requires an understanding of TCP/IP that is beyond our scope. We will typically set it to a large value, such as 1024.

The open_listenfd(customized) Function

After we create the listenfd socket descrip- tor, we use the setsockopt function (not described here) to configure the server so that it can be terminated and restarted immediately.

By default, a restarted server will deny connection requests from clients for approximately 30 seconds, which seriously hinders debugging.

diffs when format sockaddr_in between client and server:

client:

server:

The accept Function

The accept function waits for a connection request from a client to arrive on the listening descriptor listenfd, then fills in the client’s socket address in addr,

and returns a connected descriptor that can be used to communicate with the client using Unix I/O functions.

The listening descriptor serves as an end point for client connection requests. It is typically created once and exists for the lifetime of the server.

The connected descriptor is the end point of the connection that is established between the client and the server.

It is created each time the server accepts a connection request and exists only as long as it takes the server to service a client.

Example Echo Client and Server

What does EOF on a connection mean?

First, we need to understand that there is no such thing as an EOF character. Rather, EOF is a condition that is detected by the kernel.

An application finds out about the EOF condition when it receives a zero return code from the read function.

For disk files, EOF occurs when the current file position exceeds the file length.

For Internet connections, EOF occurs when a process closes its end of the connection. The process at the other end of the connection detects the EOF when it attempts to read past the last byte in the stream.

The loop(fget loop) terminates when fgets encounters EOF on standard input, either because the user typed ctrl-d at the keyboard or because it has exhausted the text lines in a redirected input file.

After the loop terminates, the client closes the descriptor. This results in an EOF notification being sent to the server, which it detects when it receives a return code of zero from its rio_readlineb function.

After closing its descrip- tor, the client terminates.

Since the client’s kernel automatically closes all open descriptors when a process terminates, the close in line 24 is not necessary. How- ever, it is good programming practice to explicitly close any descriptors we have opened.

Notice that our simple echo server can only handle one client at a time. A server of this type that iterates through clients, one at a time, is called an iterative server.

原文地址:https://www.cnblogs.com/geeklove01/p/9359157.html

时间: 2024-08-30 06:56:34

Network Programming(1)的相关文章

Professional iOS Network Programming Connecting the Enterprise to the iPhone and iPad

Book Description Learn to develop iPhone and iPad applications for networked enterprise environments The iPhone and iPad have made a powerful impact on the business world. Developers creating iOS apps for the enterprise face unique challenges involvi

《Python Network Programming Cookbook》读书笔记0---前言

最近打算学习python网络开发方面的内容,于是网上找来一本<Python Network Programming Cookbook>电子书做教材,顺便边学边将学到的这容通过博客的形式总结起来与大家分享. 这本书不长,英文原版也只有234页,但涉及面很广,TCP/UDP HTTP/HTTPS FTP SMTP POP3 IMAP CGI均有介绍,依照章节,博文会按如下9个部分介绍: 套接字, IPv4,  简单的Client/Server程序 更好的性能:复用套接字 I/O IPv6, UNI

分享:Foundations of Python Network Programming(3rd) 英文版pdf 下载

Foundations of Python Network Programming Third Edition 下载http://www.amazon.com/Foundations-Python-Network-Programming-Brandon/dp/1430258543这本书是2014年底出版的,基于最新的 python3.4 版本.配书源码链接https://github.com/brandon-rhodes/fopnp 目录 Chapter 1: Introduction to C

Boost.Asio C++ Network Programming翻译的校对稿

Boost.Asio C++ Network Programming翻译的校对稿将发布在gitbook上: 大家有兴趣可以一起来校对 目前校对稿仍然在持续不断的添加中,预计明天会全部校对完毕,届时我会将校对稿以pdf的形式发布到csdn的下载频道中,以方便大家更好的阅读全书. 附上链接地址: https://www.gitbook.com/book/mmoaay/boost-asio-cpp-network-programming-chinese

【learning log】Linux network programming

DNS host entry 包含 DNS database 中有关某一 domin name 或 ip address 的 DNS 信息. 1 struct hostent{ 2 char *h_name; 3 char *h_aliases; 4 int h_addrtype; 5 int h_length; 6 char **h_addr_list; 7 }; hostinfo 程序, 用来从 ip 或 domin name 解析 DNS info. 1 /*This program is

TCP 、UDP网络编程作业代写、代写C 语言 TCP程序 Network Programming using C

TCP .UDP网络编程作业代写.代写C 语言 TCP程序Network Programming using COverview? This homework is due by 11:59:59 PM on Thursday, April 26, 2018.? This homework will count as 8% of your final course grade.? This homework is to be completed individually. Do not shar

《Python Network Programming Cookbook》读书笔记1---套接字, IPv4, 简单的Client/Server程序

这一部分主要介绍python中socket模块的相关内容,socket即套接字. socket是使用TCP/IP协议的应用程序通常采用的应用编程接口,它位于运输层和应用层之间,起源于UNIX,由于遵从UNIX“一切皆文件的”思想故socket可看作一种特殊的文件,对其的操作基本可以视为读写I/O.打开.关闭.关于套接字的基本概念@吴秦的Linux Socket编程(不限Linux)写的很详细,大家可以参考. 在下面列出的各个部分中我将先贴出代码,然后对其进行解释. 通过python3获得本机名和

Binary Order function In Unix Network Programming

ByteOrder: 1 #include <stdio.h> 2 3 int main(int argc, char**argv) 4 { 5 union { 6 short s; 7 char c[sizeof(short)]; 8 } un; 9 un.s = 0x0102; 10 11 if ( sizeof(short) == 2 ) 12 { 13 if ( (un.c[0] == 1) && (un.c[1] == 2) ) 14 { 15 printf(&quo

Unix NetWork Programming(unix环境编程)——环境搭建(解决unp.h等源码编译问题)

此配置实例亲测成功,共勉,有问题大家留言. 环境:VMware 10 + unbuntu 14.04 为了unix进行网络编程,编程第一个unix程序时遇到的问题,不能包含unp.h文件,这个感觉和apue.h差不多,不过这里需要编译源代码,为了以后方便,现在整理如下: 主要有两点一是生成libunp.a这个库,二是得到unp.h.config.h这两个个头文件. 1,安装编译器,为了齐全还是安装一下build-essential sudo apt-get install build-essen