[Add to favorites] network socket programming guide 4

Source: Internet
Author: User

Sendto () and recvfrom () Functions
"That's good," you said. "But you haven't talked about the connectionless datagram socket ?" No problem. Now let's start this content.
Since the datagram socket is not connected to the remote host, what information do we need before sending a packet? Yes, it's the target address! Take a look at the following:
Int sendto (INT sockfd, const void * MSG, int Len, unsigned int flags,
Const struct sockaddr * To, int tolen );
As you can see, except for the other two information, the rest are the same as the function send. To is a pointer to the data structure struct sockaddr. It contains the IP address and port information of the destination. Tolen can be easily set to sizeof (struct sockaddr ). Similar to the send () function, sendto () returns the actual number of bytes sent (it may be smaller than the number of bytes you want to send !), Or return-1 in case of an error.
Similar functions include Recv () and recvfrom (). The definition of recvfrom () is as follows:
Int recvfrom (INT sockfd, void * Buf, int Len, unsigned int flags, struct sockaddr * From, int * fromlen );
Again, this function is the same as Recv () except for the two added parameters. From is a pointer to the local data structure struct sockaddr. Its content is the IP address and port information of the source machine. Fromlen is a local pointer of the int type, and its initial value is sizeof (struct sockaddr ). After the function is returned, fromlen stores the length of the address actually stored in from.
Recvfrom () returns the length of the received bytes, or-1 if an error occurs.
Remember, if you use connect () to connect to a datagram socket, you can simply call send () and Recv () to meet your requirements. At this time, it is still a datagram socket. By using UDP, the system socket interface will automatically add the target and source information to you.
--------------------------------------------------------------------------------
Close () and Shutdown () Functions
You have been sending (send () and receiving (Recv () data all day, and now you are ready to close your socket descriptor. This is simple. You can use the close () function of the general UNIX file descriptor:
Close (sockfd );
It will prevent reading and writing more data on the socket. Any enterprise image that reads or writes a socket on the other end will return an error message.
If you want to have more control over how to close the socket, you can use the function Shutdown (). It allows you to disable communication in a certain direction or two-way communication (like close (). You can use:
Int Shutdown (INT sockfd, int how );
Sockfd is the description of the socket file you want to close. The value of how is one of the following:
0-unacceptable
1-cannot be sent
2-sending and receiving are not allowed (the same as close)
If Shutdown () succeeds, 0 is returned. If the shutdown fails,-1 is returned (errno is set at the same time .) If you use Shutdown () in a connectionless datagram socket, it is only for sending () and Recv () cannot be used (remember that you can use them after using connect in the datagram socket ).
--------------------------------------------------------------------------------
Getpeername () function
This function is too simple.
It's so simple that I don't even want to list a single chapter. But I did. The getpeername () function tells you who is on the other side of the connected streaming socket. The number of functions is as follows:
# Include <sys/socket. h>
Int getpeername (INT sockfd, struct sockaddr * ADDR, int * addrlen );
Sockfd is the descriptor of the connected stream socket. ADDR is a pointer to the structure struct sockaddr (or struct sockaddr_in), which stores information on the other side of the connection. Addrlen is an int pointer, Which is initialized to sizeof (struct sockaddr ). When an error occurs, the function returns-1 and sets the corresponding errno.
Once you obtain their addresses, you can use inet_ntoa () or gethostbyaddr () to print or obtain more information. But you cannot get its account. (If it runs a silly daemon, this is possible, but it is beyond the scope of this article, please take a look at the RFC-1413 for more information .)
--------------------------------------------------------------------------------
Gethostname () function
The gethostname () function is even simpler than getpeername (). It returns the Host Name of the machine in which your program runs. Then you can use gethostbyname () to obtain the IP address of your machine.
The following is the definition:
# Include <unistd. h>
Int gethostname (char * hostname, size_t size );
The parameter is simple: Hostname is a character array pointer, which will be saved when the function returns
Host Name. Size is the length of the hostname array.
If the function call is successful, 0 is returned,-1 is returned, and errno is set.
--------------------------------------------------------------------------------

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.