tailieunhanh - Embedded FreeBSD Cookbook phần 6

Các yếu tố sin_len đại diện cho chiều dài của cấu trúc. Sin_family mô tả các địa chỉ gia đình, AF_INET. Sin_port là 16-bit số cổng để kết nối. Các yếu tố sin_addr là một địa chỉ IP 32-bit đại diện cho các máy chủ để kết nối. Tham số thứ ba là chiều dài của cấu trúc thông qua như là tham số sockaddr. Bind trả về 0 nếu thành công, -1 nếu có một lỗi. Các cuộc gọi hệ thống gần | 111 Chapter Six Daemons struct sockaddr in u char sin len u char sin family u short sin port struct in addr sin addr char sin zero 8 The sin_len element represents the length of the structure. The sin_family describes the address family AF_INET. The sin_port is the 16-bit port number to bind. The sin_addr element is a 32-bit IP address that represents the host to connect. The third parameter is the length of the structure passed as the sockaddr parameter. Bind returns 0 if successful and -1 if there is an error. The close System Call When a network service is shut down proper cleanup is recommended. The socket descriptor should be closed using the close system call the same as a file descriptor. include int close int fd The descriptor passed is the socket descriptor returned by the socket system call. The close system call terminates the socket. Close returns 0 on success -1 on error. Server The DIO server we are developing is a connection-based server. A connectionbased application is similar to file IO. A connection is opened with another process and that connection remains with the same process and host for the remainder of the transfer operation. The DIO server operations consist of requests to perform data acquisition operations. The types of requests will be discussed in more detail in the protocol section. The remainder of this section focuses on the socket calls for creating socket connections. The listen System Call Once the server has successfully created a socket it must designate itself willing to listen for incoming requests. Listening on a socket is performed by calling the listen system call. 112 Embedded FreeBSD Cookbook include sys include int listen int s int backlog The first parameter is the socket descriptor returned by the socket system call. The second parameter backlog specifies the maximum number of outstanding connections that may be queued by the server. Listen returns 0 on success or -1 on error. The accept .

TỪ KHÓA LIÊN QUAN