TcpClient - 绑定/使用特定的IP。 (vb.net) [英] TcpClient - bind to/use a specific IP. (vb.net)

查看:62
本文介绍了TcpClient - 绑定/使用特定的IP。 (vb.net)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我在尝试绑定到特定的IP地址时遇到了一些麻烦。


代码:

私有mobjClient作为TcpClient


mobjClient =新TcpClient(IPAddress.Parse(" 10.16.104.87")。ToString,

8892)


mobjClient.GetStream.BeginRead(marData,0,1024,AddressOf DoRead,

什么都没有)


上面的代码与代码的其余部分完美配合 - 但

我想弄清楚的是如何使用特定的IP来连接




我有4个IP地址,我想使用3和3。一。我怎么能告诉

使用那个IP的代码?


目前我已经尝试过:

mobjClient.Client。绑定(新

IPEndPoint(IPAddress.Parse(" 10.24.36.22"),8892))


但这不起作用。任何人都可以帮助我吗?


/ Jan

Hi all,

I got into some trouble trying to bind to a specific IP address.

Code:
Private mobjClient As TcpClient

mobjClient = New TcpClient(IPAddress.Parse("10.16.104.87").ToString ,
8892)

mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead,
Nothing)

The above code works perfectly along with the rest of the code - but
what I am trying to figure out is how to use a specific IP to connect
from.

I have 4 IP addresses and I want to use a the "3" one. How can I tell
the code to use that one IP?

Currently I have tried with:
mobjClient.Client.Bind(New
IPEndPoint(IPAddress.Parse("10.24.36.22"), 8892))

But that doesnt work. Can anyone help me out here?

/ Jan

推荐答案

" Jan Vinten" < ja ******** @ gmail.comschrieb
"Jan Vinten" <ja********@gmail.comschrieb

大家好,


我遇到了一些试图绑定到特定的IP地址时遇到麻烦。


代码:

私有mobjClient作为TcpClient


mobjClient =新的TcpClient(IPAddress.Parse(" 10.16.104.87")。ToString,

8892)

mobjClient.GetStream.BeginRead(marData,0, 1024,AddressOf DoRead,

没什么)


以上代码与其余代码完美配合 - 但是

什么我想知道如何使用特定的IP来连接来自


我有4个IP地址,我想使用 ; 3英寸一。我怎么能告诉代码使用那个IP?


目前我已经尝试过:

mobjClient.Client。绑定(新

IPEndPoint(IPAddress.Parse(" 10.24.36.22"),8892))


但这不起作用。有人可以帮我从这里出去吗?
Hi all,

I got into some trouble trying to bind to a specific IP address.

Code:
Private mobjClient As TcpClient

mobjClient = New TcpClient(IPAddress.Parse("10.16.104.87").ToString ,
8892)

mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead,
Nothing)

The above code works perfectly along with the rest of the code - but
what I am trying to figure out is how to use a specific IP to
connect from.

I have 4 IP addresses and I want to use a the "3" one. How can I
tell the code to use that one IP?

Currently I have tried with:
mobjClient.Client.Bind(New
IPEndPoint(IPAddress.Parse("10.24.36.22"), 8892))

But that doesnt work. Can anyone help me out here?



列出网络接口和IP地址:

Imports System.Net

进口系统。 Net.NetworkInformation


''...


Dim NInterfaces = NetworkInterface.GetAllNetworkInterfaces


对于NInterfaces中的每个NInterface

Dim Props = NInterface.GetIPProperties()

Dim Addresses = Props.UnicastAddresses

Debug.Print (NInterface.Description)


地址中的每个地址

Debug.Print(""& Address.Address.ToString)

下一页

下一页

对于TCPClient:


mobjClient =新的TcpClient(新的IPEndPoint(Address.Address, 8892))


" Address.Address"就是你之前在内环中看到的。

Armin


List network interfaces and IP addresses:

Imports System.Net
Imports System.Net.NetworkInformation

''...

Dim NInterfaces = NetworkInterface.GetAllNetworkInterfaces

For Each NInterface In NInterfaces
Dim Props = NInterface.GetIPProperties()
Dim Addresses = Props.UnicastAddresses

Debug.Print(NInterface.Description)

For Each Address In Addresses
Debug.Print(" " & Address.Address.ToString)
Next
Next
For the TCPClient:

mobjClient = New TcpClient(New IPEndPoint(Address.Address, 8892))

"Address.Address" is what you see in the inner loop before.
Armin


" Armin Zingler" < az ******* @ freenet.deschrieb
"Armin Zingler" <az*******@freenet.deschrieb

[code]
[code]



也许我误解了你的问题。(?)

Armin

Maybe I misunderstood your question.(?)
Armin


7月16日,9:50 pm,Armin Zingler < az.nos ... @ freenet.dewrote:
On Jul 16, 9:50 pm, "Armin Zingler" <az.nos...@freenet.dewrote:

" Jan Vinten" < jan.vin ... @ gmail.comschrieb
"Jan Vinten" <jan.vin...@gmail.comschrieb

大家好,
Hi all,


我试图绑定到特定的IP地址时遇到了一些麻烦。
I got into some trouble trying to bind to a specific IP address.


代码:

私有mobjClient为TcpClient
Code:
Private mobjClient As TcpClient


mobjClient =新TcpClient(IPAddress.Parse(" 10.16.104.87")。ToString,

8892)
mobjClient = New TcpClient(IPAddress.Parse("10.16.104.87").ToString ,
8892)


mobjClient.GetStream.BeginRead(marData,0,1024,AddressOf DoRead,

Nothing)
mobjClient.GetStream.BeginRead(marData, 0, 1024, AddressOf DoRead,
Nothing)


上面的代码与代码的其余部分完美配合 - 但

我想弄清楚的是如何使用特定的IP来连接来自

The above code works perfectly along with the rest of the code - but
what I am trying to figure out is how to use a specific IP to
connect from.


我有4个IP地址,我想使用3和3。一。我怎么能告诉代码使用那个IP?

I have 4 IP addresses and I want to use a the "3" one. How can I
tell the code to use that one IP?


目前我已尝试过:

mobjClient.Client.Bind(新

IPEndPoint (IPAddress.Parse(" 10.24.36.22"),8892))
Currently I have tried with:
mobjClient.Client.Bind(New
IPEndPoint(IPAddress.Parse("10.24.36.22"), 8892))


但是这不起作用。有人可以帮我从这里出去吗?
But that doesnt work. Can anyone help me out here?



列出网络接口和IP地址:

Imports System.Net

进口系统。 Net.NetworkInformation


''...


Dim NInterfaces = NetworkInterface.GetAllNetworkInterfaces


对于NInterfaces中的每个NInterface

Dim Props = NInterface.GetIPProperties()

Dim Addresses = Props.UnicastAddresses

Debug.Print (NInterface.Description)


地址中的每个地址

Debug.Print(""& Address.Address.ToString)

下一页

下一页


对于TCPClient:


mobjClient =新TcpClient(新IPEndPoint( Address.Address,8892))


" Address.Address"是你之前在内循环中看到的。


Armin


List network interfaces and IP addresses:

Imports System.Net
Imports System.Net.NetworkInformation

''...

Dim NInterfaces = NetworkInterface.GetAllNetworkInterfaces

For Each NInterface In NInterfaces
Dim Props = NInterface.GetIPProperties()
Dim Addresses = Props.UnicastAddresses

Debug.Print(NInterface.Description)

For Each Address In Addresses
Debug.Print(" " & Address.Address.ToString)
Next
Next

For the TCPClient:

mobjClient = New TcpClient(New IPEndPoint(Address.Address, 8892))

"Address.Address" is what you see in the inner loop before.

Armin



嗨阿明,


感谢您的回复。


我想找到所有IP地址的最简单方法是:

Dim ipE As IPHostEntry = Dns。 GetHostEntry(Dns.GetHostName())

Dim IpA()As IPAddress = ipE.AddressList


现在IpA包含数组中的所有IP地址。但这是另一个

的故事!


主要问题是我想使用特定的

连接到TCP服务器IP地址。 (防火墙问题)我只能让TcpClient

使用我的Windows机器上的第一个可用IP地址。我有4个

IP',并希望TcpClient使用3. one。我无法工作




因此,当我遇到套接字编码时,我花了几个小时搜索网络

代替。现在我能够绑定到特定的IP - 见下文:


私有ClientSocket作为套接字


ClientSocket =新套接字(AddressFamily.InterNetwork) ,

SocketType.Stream,ProtocolType.Tcp)

Dim endpoint As New IPEndPoint(IPAddress.Parse(" 10.16.104.87"),8892)


Dim bindaddress As New IPEndPoint(IPAddress.Parse(" 10.24.36.22"),0)

ClientSocket.Bind(bindaddress)


ClientSocket.BeginConnect(端点,AddressOf已连接,无)


它现在正在运行。 ; o)


/ Jan

Hi Armin,

Thank you for your reply.

I guess the easiest way to find all IP addresses are like:
Dim ipE As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
Dim IpA() As IPAddress = ipE.AddressList

Now IpA contains all IP addresses in a array. But that is another
story!

The main issue was that I wanted to connect to a TCP server using a
specific IP address. (firewall issues) I could only have the TcpClient
use the first available IP address on my Windows machine. I have 4
IP''s and wanted the TcpClient to use the 3. one. That I could not get
to work.

So I spend hours searching the net when I came across Socket coding
instead. Now I am able to bind to a specific IP - see below:

Private ClientSocket As Socket

ClientSocket = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)

Dim endpoint As New IPEndPoint(IPAddress.Parse("10.16.104.87"), 8892)

Dim bindaddress As New IPEndPoint(IPAddress.Parse("10.24.36.22"), 0)
ClientSocket.Bind(bindaddress)

ClientSocket.BeginConnect(endpoint, AddressOf Connected, Nothing)

And it is now working. ;o)

/ Jan


这篇关于TcpClient - 绑定/使用特定的IP。 (vb.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆