Because the socket is not connected and (when a sendto call is used to send a datagram socket) the address is not provided

Source: Internet
Author: User

When I was learning socket programming, I always encountered this problem. I searched the internet and made a small summary. I hope it will help you.

1. During shutdown, I found this problem and found something on csdn.

Function call analysis (msdn): Once the socket connection is completed, the socket should be closed and all resources occupied by the socket handle should be released. You can call closesocket directly to release a resource with an opened socket handle, but you must understand that closesocket call may have negative effects. The specific impact is related to how to call closesocket, the most obvious impact is data loss. Therefore, shutdown must be called before closesocket to close the socket.
Shutdown: to ensure that both parties can receive all the data sent by the application, a qualified application is to notify the recipient that both sides do not send data! This is the so-called "normally close" socket method, and this method is passed to it by the shutdown function. There are three parameters: sd_receive, sd_send, and sd_both, if it is sd_receive, it indicates that the function of receiving the socket is not allowed to be called. This does not affect the protocol layer. In addition, for TCP sockets, the connection must be reset whether the data is waiting for acceptance or is about to arrive. (Note that for UDP protocol, still accept and arrange incoming data, so shutdown is meaningless for UDP sockets ). If se_send is selected, the sending function cannot be called. For TCP sockets, this means that a FIN packet is generated after all data is sent and confirmed by the receiver. If sd_both is specified, the answer is self-evident.
Closesocket: this function will release the description of the socket, which is well known (programmers who often read msdn). Therefore, after calling this function, when this socket is used, the call fails. Generally, the error wsaenotsock is returned. At this time, all resources associated with the socket descriptor of closesocket will be released, including discarding data in the transmission queue !!!! For the threads in the current process, all closed operations, pending overlapping operations, and any related events will fail to be called to complete routine or port execution! In addition, the so_linger flag also affects closesocket behavior, but it is not explained here for traditional socket programs.
Therefore, we can see that shutdown has reasonable integrity for the disconnection.
Next we will analyze the shutdown and closesocket behavior (behavior) from the TCP protocol: closesocket or shutdown (when sd_send is used as a parameter) and send a FIN packet to the communication peer, at this time, the socket state will change from established to fin_wait_1, and the other party will send an ACK packet as a response, and the socket will become fin_wait_2. If the other party closes the connection, the other party will send fin, we will respond to an ACK and set the socket to time_wait. Therefore, we can see that the TCP behavior of closesocket and shutdown is the same. The difference is that the function part. shutdown will ensure that the data in the data transmission queue established in Windows will not be lost, closesocket will discard all the data. Therefore, if you want closesocket, you can replace shutdown. However, in network protocol programs with complex data interaction, it is better to shut down the data !? Access the rfc793 file of the http://www.rfc-editor.org on TCP connection principles

I found that there are still some usage for understanding. It seems that when shutdown, I want to send a message to the other party and tell him that the communication is no longer in progress. However, since the other party has already (released) gone, therefore, if the notification object cannot be found, an exception is thrown.

Then you need to consider whether the client is responsible for actively ending the conversation. I want to post it later.

The error message indicates that the target cannot be found when the message is sent.

There are other things. Write this article first. If you have time, make a good summary and study.

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.