首页 > 代码库 > socket.草稿。
socket.草稿。
1)Socket Descriptors
A socket is an abstraction of a communication endpoint. Just as they would use file descriptors to access files, applications use socket descriptors to access sockets. Socket descriptors are implemented as file descriptors in the UNIX System. Indeed, many of the functions that deal with file descriptors, such as read and write, will work with a socket descriptor. To create a socket, we call the socket function.
#include <sys/socket.h>
int socket(int domain, int type, int protocol);
Returns: file (socket) descriptor if OK, ?1 on error
2)Addressing
we need to learn how to identify the process with which we wish to communicate. Identifying the process has two components. The machine’s network address helps us identify the computer on the network we wish to contact, and the service, represented by a port number, helps us identify the particular process on the computer.
3)Associating Addresses with Sockets
The address associated with a client’s socket is of little interest, and we can let the system choose a default address for us. For a server, however, we need to associate a well-known address with the server’s socket on which client requests will arrive. Clients need a way to discover the address to use to contact a server, and the simplest scheme is for a server to reserve an address and register it in /etc/services or with a name service. We use the bind function to associate an address with a socket.
#include <sys/socket.h>
int bind(int sockfd, const struct sockaddr *addr, socklen_t len);
Returns: 0 if OK, ?1 on error
4)Connection Establishment
If we’re dealing with a connection-oriented network service (SOCK_STREAM or SOCK_SEQPACKET), then before we can exchange data, we need to create a connection between the socket of the process requesting the service (the client) and the process providing the service (the server). We use the connect function to create a connection
int connect(int sockfd, const struct sockaddr *addr, socklen_t len); Returns: 0 if OK, ?1 on error
5)Data Transfer
Since a socket endpoint is represented as a file descriptor, we can use read and write to communicate with a socket, as long as it is connected.
Three functions are available for sending data, and three are available for receiving data. First, we’ll look at the ones used to send data. The simplest one is send. It is similar to write, but allows us to specify flags to change how the data we want to transmit is treated.
1) socket 是一个抽象的通讯端。
2)那么首先要定义个抽象的通讯端出来。函数原型:int socket(int domain, int type, int protocol);
3)连和被连的对象,都需要使用地址和端口来定义。
4)再把socket和address关联起来。函数原型:int bind(int sockfd, const struct sockaddr *addr, socklen_t len);
至此,本地的一个通讯端已经建立。
5)通讯就必须和远程的通讯端链接。用connection。函数原型:int connect(int sockfd, const struct sockaddr *addr, socklen_t len); Returns: 0 if OK, ?1 on error
如果地址使用默认。说明不想连接到确定的远程地址,表明可能是一个被连端。
6)连接之后。就可以使用read或write方法来进行操作。或者使用其他函数。
因为把抽象的通讯端,也就是socket,在系统内部抽象为一个文件。所以socket建立后返回的是一个文件描述符。我们可以使用大部分文件描述符通用的函数。write,read,shutdown.等。
刚看socket时,总和文件联系起来,会入误区,先理清socket的目的,就是通讯,通讯就需要定义自己和对方的地址和端口,以及协议。连接之后的操作类似文件的操作,对于系统来说是一样的。都是i/o。
socket.草稿。