《TCP/IP Sockets 编程》笔记2

来源:互联网 发布:中英对照小说阅读软件 编辑:程序博客网 时间:2024/04/28 12:07

第2章 基本的TCP套接字
 

典型的TCP客户通信涉及4个基本步骤:

1.使用socket()创建TCP套接字

2.使用connect()建立到服务器的连接(需要提供一个sockaddr_in结构)

3.使用send()和recv()通信

4.使用close()关闭连接
 

TCP是一种字节流协议,这类协议的一种实现是不会保持send()边界。通过在连接一端调用send()发送的字节可能不会通过在另一端单独调用一次recv()而全都返回。

 

编写使用套接字的应用程序的基本原则是:对于网络和另一端的程序将要做什么,永远都不能做假设。
This is a basic principle of writing applications that use sockets:you must never assume anything about what the network and the program at the other end are going to do.

 

永远不应该做的一件事情是:把从网络接收到的文本作为第一个参数传递给printf()。它会引起严重的安全性问题,要代之以使用fputs()。
One thing that you should never do is to pass text received from the network as the first argument to printf(). It creates a serious security vulnerability. Use fputs() instead.

 

基本TCP服务器通信的4个基本步骤:

1.使用socket()创建TCP套接字
2.利用bind()给套接字绑定一个端口
3.使用listen()告诉系统允许对该端口发起的连接
4.反复执行以下操作
调用accept()为每个客户连接获取新的套接字
使用send()和recv()通过新的套接字与客户通信
使用close()关闭客户连接
 

防御性编程:你的代码绝对不能对通过网络接收到的任何信息做出假设。
Defensive Programming: your code must not make assumptions about anything received over the network.
 

创建和销毁套接字

int socket(int domain, int type, int protocol)
domain,确定套接字的通信领域(domain)
type,指定套接字的类型(type),类型决定了利用套接字进行的数据传输的语义。
protocol,指定要使用的特定的端到端协议(end-to-end protocol)。如果为0,系统为指定的协议族和类型选择默认的端到端协议。
返回非负值表示成功,-1表示失败。


int close(int socket)
返回0表示成功,-1表示失败。


指定地址

泛型数据类型--sockaddr结构

struct sockaddr {
    sa_family_t sa_family;  // Address family (e.g., AF_INET)
    char sa_data[14];        // Family-specific address information
};
 

IPv4地址

struct in_addr {
    uint32_t s_addr;    // Internet address (32 bits)
};
struct sockaddr_in {
    sa_family_t sin_family;    // Internet protocol (AF_INET)
    in_port_t sin_port;          // Address port (16 bits)
    struct in_addr sin_addr;  // IPv4 address (32 bits)
    char sin_zero[8];             // Not used
};


IPv6地址

struct in_addr {
    char s_addr[16];  // Internet address (128 bits)
};
struct sockaddr_in6 {
    sa_family_t sin6_family;      // Internet protocol (AF_INET6)
    in_port_t sin6_port;            // Address port (16 bits)
    uint32_t sin6_flowinfo;        // Flow information
    struct in6_addr sin6_addr;  // IPv6 address (128 bits)
    uint32_t sin6_scope_id;      // Scope identifier
};

 
通用地址存储器

struct sockaddr_storage {
    sa_family_t
    ...
    // Padding and fields to get correct length and alignment
    ...
};


二进制/字符串地址转换

可打印字符串转换为数字(pton = printable to numeric)
int inet_pton(int addressFamily, const char *src, void * dst)
addressFamily,要转换的地址的地址族。
src,null终止的字符串,包含要转换的地址。
dst,指向调用者空间中的内存块以存放结果。
成功返回1,dst指向网络字节顺序的地址;
src指向的字符串未被格式化为有效地址,则返回0;
指定地址族未知,则返回-1。
 

地址从数字转换为可打印的形式(ntop = numeric to printable)
const char * inet_ntop(int addressFamily, const void *src, char * dst, socklen_t dstBytes);
addressFamily,要转换的地址的地址族。
src,指向包含要转换的数字地址的内存块的第一个字节。
dst,指向调用者空间中的内存块,将把得到的字符串复制到其中,大小有dstBytes给定。
成功返回指向包含可打印地址的字符串指针(可就是第三个变量),
失败返回null。
系统定义的常量INET_ADDRSTRLEN和INET6_ADDRSTRLEN指示可能最长的结果字符串。

 
获取套接字的关联地址
 

获取套接字关联的本地地址和外部地址
int getpeername(int socket, struct sockaddr *remoteAddress, socklen_t *addressLength)
int getsockname(int socket, struct sockaddr *localAddress, socklen_t *addressLength)

 
连接套接字

与服务器建立连接。
int connect(int socket, const struct sockaddr *foreignAddress, socklen_t addressLength)

 
绑定到地址

客户与服务器在服务器的地址和端口处“聚会”。服务器把自己的地址指定给bind()。
int bind(int socket, struct sockaddr *localAddress, socklen_t addressSize)
成功返回0,失败返回-1。
 

一定要注意:虽然inaddr_any被定义成位于主机字节顺序中,因此在把它用作bind()的参数之前必须先转换为网络字节顺序,但是in6addr_any和in6addr_any_init已经位于网络字节顺序中。
Note well that while inaddr_any is defined to be in host byte order and, consequently, must be converted to network byte order with htonl() before being used as an argument to bind(), in6addr_any and in6addr_any_init are already in network byte order.


处理进入的连接

指示底层协议栈实现侦听来自客户的连接。listen()导致给定套接字的内部状态改变,因此进入的TCP连接请求将被处理然后排队以便程序接受它们。
int listen(int socket, int queueLimit)
queueLimit,任意时间,处于等待的进入连接的数目上限。 The queueLimit parameter specifies an upper bound on the number of incoming connections that can be waiting at any time.
成功返回0,失败返回-1。


获取客户连接的新套接字

int accept(int socket, struct sockaddr *clientAddress, socklen_t *addressLength)
成功返回连接到客户的新套接字的描述符,失败返回-1。

 
通信

ssize_t send(int socket, const void *msg, size_t msgLength, int flags)
ssize_t recv(int socket, void *rcvBuffer, size_t bufferLength, int flags)
flags,提供一种方式,用于改变套接字调用的默认行为的某些方面,设置为0指定默认的行为。

 

原创粉丝点击