vb.net 解决:winsock 资源不足,无法完成套接字连接启动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15591708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Solving : Insufficient winsock resources available to complete socket connection initiation
提问by mkaatr
I have two systems, one is sending large amount of data (about 1GB) through web service. First it splits the data into chunks, then send it to the client when requested with extra information.
我有两个系统,一个是通过网络服务发送大量数据(大约 1GB)。首先它将数据分成块,然后在请求额外信息时将其发送给客户端。
The data is not sent as byte array directly, but instead is placed into a special class i created that has two members:
数据不直接作为字节数组发送,而是放入我创建的一个特殊类中,该类具有两个成员:
1) Message : i use it specify what to do with the chunk 2) Obj : a byte array representing the data
1)消息:我用它指定如何处理块 2)Obj:表示数据的字节数组
So basically the server fills this structure then it serialize the whole object, and finally send it to the client. Each chunk is about 4KB
所以基本上服务器填充这个结构然后序列化整个对象,最后将它发送到客户端。每个chunk大约4KB
Again this is repeated many times, so the amount of data transmission could be 1GB or 2GB. When I try to run this i get this error:
再次重复多次,因此数据传输量可能是 1GB 或 2GB。当我尝试运行它时,我收到此错误:
System.InsufficientMemoryException was caught Message="Insufficient winsock resources available to complete socket connection initiation."
" System.InsufficientMemoryException 被捕获 Message="可用的 winsock 资源不足,无法完成套接字连接启动。
When I tried: netstat -an -p TCP
当我尝试时:netstat -an -p TCP
I got something like this...
我得到了这样的东西...
TCP 127.0.0.1:58759 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58759 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58760 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58760 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58761 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58761 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58762 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58762 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58763 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58763 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58764 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58764 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58765 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58765 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58766 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:58766 127.0.0.1:57469 TIME_WAIT
...
...
increasing up to about:
增加到大约:
...
...
TCP 127.0.0.1:65531 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:65531 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:65532 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:65532 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:65533 127.0.0.1:57469 TIME_WAIT
TCP 127.0.0.1:65533 127.0.0.1:57469 TIME_WAIT
There should be a hot fix for this - in a previous answer but it did not work. The hot fix is here:
对此应该有一个热修复 - 在之前的答案中但它没有用。热修复在这里:
http://support.microsoft.com/kb/2577795
http://support.microsoft.com/kb/2577795
I used similar code before, but instead of doing serialization, I just sent the data as array of bytes directly, and it worked perfectly fine. I find no difference between the codes except for the use of the encapsulation class I created and the serialization process.
我之前使用过类似的代码,但我没有进行序列化,而是直接将数据作为字节数组发送,并且运行良好。除了使用我创建的封装类和序列化过程之外,我发现代码之间没有区别。
Could anyone explain what am I doing wrong?
谁能解释一下我做错了什么?
thank you.
谢谢你。
===========================================
============================================
I found a simple way to replicate this issue:
我找到了一个简单的方法来复制这个问题:
Start by creating a VB.NET webservice that has the following method:
首先创建一个具有以下方法的 VB.NET Web 服务:
<WebMethod(enablesession:=False)> _
Public Function HelloWorld() As Byte()
Dim B(0 To 1000) As Byte
Return B
End Function
Next create a client for it as follows:
接下来为它创建一个客户端,如下所示:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim T As New ServiceReference1.Service1SoapClient
T.Open()
Dim I As Integer
For I = 0 To 1000000
T.HelloWorld()
Me.Text = I
Application.DoEvents()
Next
End Sub
you will get this error. Hope someone has a solution for this.
你会得到这个错误。希望有人对此有解决方案。
回答by shekky
Try closing/disposing the client between calls.
尝试在调用之间关闭/处理客户端。
For I as Integer = 0 To 1000000
Using T As New ServiceReference1.Service1SoapClient
T.Open()
T.HelloWorld()
Me.Text = I
End Using
Application.DoEvents()
Next
For I as Integer = 0 To 1000000
Using T As New ServiceReference1.Service1SoapClient
T.Open()
T.HelloWorld()
Me.Text = I
End Using
Application.DoEvents()
Next

