tailieunhanh - Linux Socket Programming by Example PHẦN 2

Từ phiên hiển thị, bạn có thể thấy rằng chương trình khách hàng sẽ nhắc bạn cho đầu vào. Các đầu vào đầu tiên là cặp đơn giản D% nhân vật và một RETURN. Kết quả trở về từ máy chủ như '08 / 13/99 '. Lưu ý rằng địa chỉ socket của người gửi gói tin đã được báo cáo, và nó đã đồng ý với địa chỉ máy chủ (mặc định chương trình). | Use of these functions is quite simple. For example to convert a short integer to network order the following code can be used short host_short 0x1234 short netw_short netw_short htons host_short The value netw_short will receive the appropriate value from the conversion to network order. To convert a value from network order back into host order is equally simple host_short ntohs netw_short Tip The h in the function name refers to host whereas n refers to network. Similarly s refers to short and l refers to long. Using these conventions it is a simple matter to pick the name of the conversion function you need. Caution The byteorder 3 functions may be implemented as macros on some systems. Linux systems that run on CPUs using the big-endian byte ordering might provide a simple macro instead because no conversion of the value is required. Initializing a Wild Internet Address Now you are ready to create an Internet address. The example shown here will request that the address be wild. This is often done when you are connecting to a remote service. The reason for doing this is that your host might have two or more network interface cards each with a different IP number. Furthermore Linux also permits the assignment of more than one IP number to each interface. When you specify a wild IP number you allow the system to pick the route to the remote service. The kernel will then determine what your final local socket address will be at the time the connection is established. Linux Socket Programming by Example - Warren W. Gay 53 There are also times when you want the kernel to assign a local port number for you. This is done by specifying sin_port as the value zero. The example code shown in Listing demonstrates how to initialize an AF_INET address with both a wild port number and a wild IP number. Listing Initializing an IN_ADDRANY AF_INET Address 1 struct sockaddr_in adr_inet 2 int adr_len 3 4 memset adr_inet 0 sizeof adr_inet 5 6 AF_INET 7

TỪ KHÓA LIÊN QUAN