UDP error 10054: The remote host forced the shutdown of an existing connection

Source: Internet
Author: User
Tags dotnet

UDP error 10054: The remote host forced the shutdown of an existing connection

Original address: Http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic1887.aspx

In the development of the UDP messaging service of a company project, we occasionally encounter the problem that, in the process of UDP communication, if the client disconnects midway, the server receives a socketexception with an error ID of 10054, which describes "the remote host forcibly shuts down an existing connection". The next thing is scary, the UDP service stops listening and all clients are affected. This means that a client-caused exception causes the entire system to crash. The problem is too serious.

The people of the Earth know that UDP is not connected, how can this anomaly occur? Baidu has a circle, found that there are many phenomena of this problem, but there is no effective response. Another Google lap, a little bit of a prospect. Found a Microsoft explanation and a dotnet workaround:

Microsoft's explanation: http://support.microsoft.com/kb/263823

Dotnet Treatment Method: http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework/topic1887.aspx

However, the processing method seems to be setting the parameters incorrectly: byte[] Optioninvalue = {Convert.tobyte (true)}; Byte[] Optionoutvalue;

The exception is still thrown by this setting. First, according to Microsoft's explanation, Optioninvalue should pass false instead of true; second, according to Microsoft's explanation, Optionoutvalue should be a DWORD value, should not be assigned, or set to null.

According to the above two points, the above two sentences should be changed to: byte[] Optioninvalue = {Convert.tobyte (false)}; byte[] Optionoutvalue = new Byte[4];

After testing, 500 users were tested to log in, send and receive messages, log off, exit abnormally, and reconnect without throwing the exception again. Service performance is stable.

During UDP communication, if the client disconnects midway, the server receives a socketexception with an error ID of 10054, which is described as "the remote host forcibly shuts down an existing connection", and the next thing is scary, the UDP service stops listening and all clients are affected. This means that a client-caused exception causes the entire system to crash.

I've been looking for a few days. Finally found a solution.

After initializing the object, set the following properties:

UINT ioc_in = 0x80000000;     UINT Ioc_vendor = 0x18000000; UINT Sio_udp_connreset = ioc_in | Ioc_vendor |     12; Clientsocket.iocontrol ((int) sio_udp_connreset, new byte[] {Convert.tobyte (FALSE)}, NULL);

Socket.iocontrol method (IoControlCode, byte[], byte[])

Use the IoControlCode enumeration to specify the control code to set the low-level operation mode for the Socket.

Parameters
IoControlCode

An IoControlCode value that specifies the control code for the operation to be performed.

Optioninvalue

An array of type Byte that contains the input data required by the operation.

Optionoutvalue

An array of type Byte that contains the output data returned by the operation.

return value

The number of bytes in the optionoutvalue parameter.

Abnormal

Exception type condition

SocketException

An error occurred while trying to access the socket. For more information, see the Remarks section.

ObjectDisposedException

The Socket is closed.

InvalidOperationException

An attempt was made to change the blocking mode without using the Blocking property.

Note

This method provides low-level access to the operating system socket on which the current instance of the socket class is based. For more information, see the WSAIOCTL documentation in the MSDN Library.

I experience:

Again do communication project inadvertently encountered this problem, remember school when the teacher said that UDP is no connection, but in the project encountered this remote host forced to close the existing connection error, make confused, what do not say, immediately Google, found a lot of friends have encountered this problem, Finally saw the original author of this article solved the problem, hehe, again reproduced, hope to help all meet the problem of friends to solve the problem quickly.

UDP error 10054: The remote host forced the shutdown of an existing connection

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.