BindIPEndPointCallback给出套接字异常 [英] BindIPEndPointCallback giving a socket exception
问题描述
我遇到了BindIPEndPointCallback的问题。您应该了解的细节如下。现在在我的笔记本电脑上(赢得7 64),有2个实时互联网连接,一个通过蓝牙连接到我的手机(bt dun),另一个
是我连接的家庭wifi 。
我通过的IP地址"Dns.GetHostAddresses"是:
10.3.177.81(gprs one),192.168.253.1(vmware),192.168.136.1(vmware),192.168.1.15(wifi),192.168.1.21(wifi,这通过浏览ipv4属性添加,并为我的机器添加了一个新的ip用于实验)。 ip地址10.3.177.81
和192.168.1.15在这里很有用,因为它们正在运行互联网连接。现在我希望能够使用BindIPEndPointDelegate来使用其中任何一个,但我无法这样做。
我的代码是:
HttpWebRequest req =(HttpWebRequest)WebRequest.Create(url);
ServicePoint sp = ServicePointManager.FindServicePoint(new Uri(url));
sp.BindIPEndPointDelegate =新System.Net.BindIPEndPoint(
          BindIPEndPointCallback);
         /*req.ServicePoint.BindIPEndPointDelegate =新System.Net.BindIPEndPoint(
          BindIPEndPointCallback);
* / b
WebResponse resp = req.GetResponse();
,回调方法如下所示:
私人IPEndPoint BindIPEndPointCallback(的ServicePoint服务点,
                 &NBSP ;                        &NBSP ;
IPEndPoint remoteEndPoint,int retryCount)
{
返回新的IPEndPoint(IPAddress.Parse(" 192.168.1.15"),0);
}
现在执行就像,当我要求时,请说
www。 whatsmyipaddress.com (或google.com),在GetResponse()之后,调用回调,但在回调之后会引发异常,说:
System.Net。 WebException:无法连接到远程服务器---> System.Net.Sockets.SocketException:尝试对无法访问的网络执行套接字操作140.239.191.10:80
在System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot,SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4,Socket s6,Socket& socket,IPAddress& address,ConnectSocketState state,IAsyncResult asyncResult,Int32 timeout,Exception& exception)
   ---内部异常堆栈跟踪结束---
在System.Net.HttpWebRequest.GetResponse()
在C:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ .1.15与10.3.177.81在回调中,代码工作正常。还有一个很奇怪的细节:如果我的笔记本电脑首先连接到10.3.177.81,之后连接到192.168.1.15,那么只有10.3.177.81工作。我的意思是哪个笔记本电脑首先启动连接
(在我说启动笔记本电脑之后)也适用于代码,但另一个不行。
真的需要解决这个问题,真的很困惑。
谢谢。
Ab
我几乎和你的情况一样。我使用这样的代码:
Imports System.Net
Imports System.Net.Cache
Class mywebclient
Inherits System.Net.WebClient
私有 m_OutIPEndPoint As IPEndPoint
Sub New ( ByVal outIp 作为 IPEndPoint)
如果 outIp 没什么 然后
投掷 New ArgumentNullException(" outIp" )
结束 如果
m_OutIPEndPoint = outIp
我跨度> .CachePolicy =的新跨度> RequestCachePolicy(RequestCacheLevel.NoCacheNoStore)
的结束 Sub
受保护 覆盖 功能 GetWebRequest( ByVal 地址 As System.Uri) As System.Net.WebRequest
Dim 请求 As HttpWebRequest = CType ( MyBase .GetWebRequest(地址),HttpWebRequest)
Console.WriteLine("使用派生类的getwebrequest" )
request.ServicePoint.Bi ndIPEndPointDelegate = <跨度风格= "颜色:蓝;">新跨度> BindIPEndPoint(的 AddressOf 跨度> ipbindfunc)
request.KeepAlive = <跨度风格= "color:Blue;"> False
返回请求
End 功能
私人 功能 ipbindfunc( ByVal x 作为 ServicePoint, ByVal y As IPEndPoint, ByVal z As Integer ) As IPEndPoint
Console.WriteLine("调用ipbindfunc,此时的IP是" & m_OutIPEndPoint.ToString)
返回 m_OutIPEndPoint
'如果我使用这些语句下面(即不要分析出
'IP端点),它工作正常。
'返回新的IPEndPoint(IPAddress.Any,5000)
结束 功能
结束 Class
'这是调用函数
类 TestMywebclient
公共 共享 Sub main()
'建议192.168.0.107
Dim 端点= 新 IPEndPoint(IPAddress.Parse(" 192.168.0.131" ),5000)
Dim x 作为 新 mywebclient(端点)
Dim remote As String = InputBox("输入地址pls。")
Dim dlg As New SaveFileDialog
如果 dlg.ShowDialog()= DialogResult.OK 然后
尝试
x.DownloadFile(remote,dlg.FileName)
Catch ex As 异常
MsgBox(ex.ToString)
结束 尝试
结束 如果
结束 Sub
End Class
Hi,
I am having some problem with the BindIPEndPointCallback. Few details you should know are as follows. Right now on my laptop (win 7 64), there are 2 live internet connections, one is running through the bluetooth connected to my cell (bt dun), and the other is the home wifi that I'm connected to.
The ip addresses that I get through "Dns.GetHostAddresses" are:
10.3.177.81 (the gprs one), 192.168.253.1 (vmware), 192.168.136.1 (vmware), 192.168.1.15 (wifi), 192.168.1.21 (wifi, this is added by going through the ipv4 properties and just added a new ip for my machine for experimentation). The ip addresses 10.3.177.81 and 192.168.1.15 are of interest here because these are running the internet connections. Now I want to be able to use any of them using the BindIPEndPointDelegate, but I'm unable to do so.
My code is this:HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
ServicePoint sp = ServicePointManager.FindServicePoint(new Uri(url));
sp.BindIPEndPointDelegate = new System.Net.BindIPEndPoint(
BindIPEndPointCallback);
/*req.ServicePoint.BindIPEndPointDelegate = new System.Net.BindIPEndPoint(
BindIPEndPointCallback);
*/
WebResponse resp = req.GetResponse();and the callback method looks like this:
private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint,
IPEndPoint remoteEndPoint, int retryCount)
{
return new IPEndPoint(IPAddress.Parse("192.168.1.15"), 0);}
Now the execution goes like, when I request, say www.whatsmyipaddress.com (or google.com), after the GetResponse(), the callback is called, but after the callback an exception is raised saying:
System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A socket operation was attempted to an unreachable network 140.239.191.10:80
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetResponse()
at relay1.Form1.getrawcontent(String url) in C:\work\odesk\relay1\relay1\Form1.cs:line 158
However, if I replace 192.168.1.15 with 10.3.177.81 in the callback, the code works fine. Also a lil strange detail: if my laptop connected to 10.3.177.81 first and 192.168.1.15 later, then only 10.3.177.81 works. I mean which ever the laptop initiated connection first (after say I booted the laptop) works for the code as well, but the other doesnt.
really need to solve this problem, really confused.
Thanks.
Ab解决方案Hi , I nearly have the same situation like yours. I used codes like this :
Imports System.Net Imports System.Net.Cache Class mywebclient Inherits System.Net.WebClient Private m_OutIPEndPoint As IPEndPoint Sub New(ByVal outIp As IPEndPoint) If outIp Is Nothing Then Throw New ArgumentNullException("outIp") End If m_OutIPEndPoint = outIp Me.CachePolicy = New RequestCachePolicy(RequestCacheLevel.NoCacheNoStore) End Sub Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest Dim request As HttpWebRequest = CType(MyBase.GetWebRequest(address), HttpWebRequest) Console.WriteLine("使用派生类的getwebrequest") request.ServicePoint.BindIPEndPointDelegate = New BindIPEndPoint(AddressOf ipbindfunc) request.KeepAlive = False Return request End Function Private Function ipbindfunc(ByVal x As ServicePoint, ByVal y As IPEndPoint, ByVal z As Integer) As IPEndPoint Console.WriteLine("调用ipbindfunc,此时的IP是" & m_OutIPEndPoint.ToString) Return m_OutIPEndPoint 'if I use the statements below (i.e don't sepcify the out 'IP endpoint) , It works fine. 'Return New IPEndPoint(IPAddress.Any, 5000) End Function End Class 'This is call Function Class TestMywebclient Public Shared Sub main() 'suggested 192.168.0.107 Dim endpoint = New IPEndPoint(IPAddress.Parse("192.168.0.131"), 5000) Dim x As New mywebclient(endpoint) Dim remote As String = InputBox("Input the address pls.") Dim dlg As New SaveFileDialog If dlg.ShowDialog() = DialogResult.OK Then Try x.DownloadFile(remote, dlg.FileName) Catch ex As Exception MsgBox(ex.ToString) End Try End If End Sub End Class
这篇关于BindIPEndPointCallback给出套接字异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文