How to troubleshoot the Socket connection Timeout __java

Source: Internet
Author: User
Tags ack

The last two days of the project one of the IM server's IM authentication problems, the user IM registration when the connection timeout problem;

After restarting the service, the user's IM registration verification can be performed without a timeout problem; But wait for half a clock (indefinite time);

As a result, there will be a connection timeout problem, all of which can only be restarted to solve the problem, and it is impossible to restart the service as soon as there is a problem.

Question one:

Log in to the server background, use the netstat-ant command to view the current network status: (This image is disabled IPV6 after the screenshot)


Found that there is no IM authentication bound port 9193 in the output, but the service is running;

You can then use lsof-i:9193 to obtain an open file record stating that the port is started, but the type in the record is IPV6

Guess whether it is because of the use of the IPV6 connection caused by the problem, and then test 80,8080 and other ports, found that the general IPV6 connection mode will not show

Next, select Disable IPV6:

Open/etc/sysctl.conf

Add at end of file:

# Disable IPV6 for all interfaces of the entire system
Net.ipv6.conf.all.disable_ipv6 = 1
# Disables the IPv6 of a specified interface (for example: Eth0, lo)
Net.ipv6.conf.lo.disable_ipv6 = 1
Net.ipv6.conf.eth0.disable_ipv6 = 1
In/etc/sysctl.conf make these changes take effect, run the following command:
$ sudo sysctl-p/etc/sysctl.conf
or reboot directly.


Question two:

After the above problem is resolved, the connection timeout exception still appears after restarting the service;

Use Netstat-ant to view the status of the connection, close_wait status; The problem is due to a service-side or

One side of the client manually shut down the connection, the server did not receive a disconnect request, so the error;

Only to see the source code:

View the source, found that the client and the server socket source code, there is no exception to write the way to deal with;

Then modify the server source code, plus the judgment in the exception: (Client socket request to add processing)

1. Determine if the socket is empty

2. Determine whether the socket is closed (using isclosed ())

3. Manually turn off the open socket

After package upgrade server, client connection registration, problem solving


From Network:

Description of SOCKET close_wait status:
Close_wait The reason: Is that a party in the network connection after the disconnect, the peer did not detect the error (the other disconnected) and did not call the closesocket, resulting in this state of appearance;

When disconnecting from a connection:
When the left side of the active shutdown sends a fin past, the right side passively closes the side to respond to an ACK,
This ACK is a TCP response (while TCP submits an error to the upper application, causing the send of the socket above
or recv return socket_error), not the application sent, at which point the passively closed side is
Close_wait the state. If this passive shutdown does not continue to invoke closesocket at this time,
Then he would not send the next fin, causing himself to be always in close_wait. Only the passive shutdown of this side
Call closesocket to send a fin to the active shutdown of this side, but also make their own state changes
For Last_ack, the socket is not placed as closed until the ACK sent by the active shutdown is received.

First we know that if our client program is in a close_wait state, it means that the socket is closed passively.
Because if the server side actively disconnect the current connection, then the two sides to shut down this TCP connection a total of four packet:
Server---> FIN---> Client
Server <---ACK <---Client
The server end is in a fin_wait_2 state, and our program is in a close_wait state.
Server <---FIN <---Client
Then the client sends the fin to the server,client to be placed in the Last_ack state.
Server---> ACK---> Client
The server responds with an ACK, then the client socket is actually placed in the closed state.


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.