Boost: ASIO: IP: TCP: socket is connected? (How do I know that the socket has been connected ?)

Source: Internet
Author: User

Translation: Hua Liang from: Http://stackoverflow.com/questions/1511129/boostasioiptcpsocket-is-connected

Problem:

I want to verify the connection status before realize my operations (read/write ).

Is there a way to make an isconnect () method?

I saw
This, But it seems "ugly ".

I tested
Is_open () function also, but doesn't have the expected behavior.

Thanks

The general content is: How do I know the current connection status before network communication?

Answer:

TCP is meant to be robust in the face of a harsh network;

TCP is destined to be robust in a chaotic network environment, because TCP is designed as such.

Even though TCP provides what looks like a persistent end-to-end connection, it's all just a lie, each packet is really just a unique, unreliable datax.

Although TCP appears to provide a continuous point-to-point connection, it is just an illusion. Each data packet is an independent and unreliable data packet.


The connections are really just virtual conduits created with a little state tracked at each end of the connection (source and destination ports and addresses, and local socket). The network stack uses this state to know
Which process to give each incoming packet to and what State to put in the header of each outgoing packet.

The so-called connection is only a virtual pipeline consisting of some States saved by each endpoint of the connection. Through these statuses, the network stack knows to pass the incoming data packet to that process, and to put the status to the packet header of the sent data packet.

Because of the underlying-inherently connectionless and unreliable-nature of the network, the stack will only report a severed connection when the remote end sends a FIN packet to close the connection, or if it doesn' t receive an ACK response to a sent
Packet (after a timeout and a couple retries ).

Because of the asynchronous nature of ASIO, the easiest way to be notified of a graceful disconnection is to have an outstanding
async_readWhich will returnerror::eofImmediately when the connection is closed. But this alone still leaves the possibility of other issues like half-open connections and network issues going undetected.

Due to ASIO's asynchronous nature, the easiest way to discover a disconnected connection isReceived in async_readerror::eof。


The most implements tively way to work around und unexpected connection interruption is to use some sort of keep-alive or ping. this occasional attempt to transfer data over the connection will allow your dient detection of an unintentionally severed connection.

The TCP protocol actually has a built-in
Keep-alive mechanic which can be configured in ASIO usingasio::tcp::socket::keep_alive. The nice thing about TCP keep-alive is that it's transparent to the user-mode application, and only the peers interested in keep-alive need configure
It. the downside is that you need OS level access/knowledge to configure the timeout parameters, they're unfortunately not exposed via a simple socket option and usually have default timeout values that are quite large (7200 seconds on Linux ).

Probably the most common method of keep-alive is to implement it at the application layer, where the application has a special Noop or ping message and does nothing but respond when tickled. this method gives you the most flexibility in implementing a keep-alive
Strategy.

Link | improve
This answer
Edited
Feb 24 at 19: 29
Answered
Oct 3 '09

Joshperry
9,2111440

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.