473,326 Members | 2,148 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

TcpClient - bind to/use a specific IP. (vb.net)

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
Jul 16 '08 #1
10 6212
"Jan Vinten" <ja********@gmail.comschrieb
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?

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
Jul 16 '08 #2
"Armin Zingler" <az*******@freenet.deschrieb
[code]
Maybe I misunderstood your question.(?)
Armin
Jul 16 '08 #3
On Jul 16, 9:50 pm, "Armin Zingler" <az.nos...@freenet.dewrote:
"Jan Vinten" <jan.vin...@gmail.comschrieb
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?

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
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
Jul 17 '08 #4
Could you give this same type example for UDP, please? I have an IP
address (local subnet) or URL (remote access) and a port number. I need
to send one integer and receive 38 integers back UDP.

I have it working but by using the old VB6 style control and Strict On
does not like it so I have to run with it off.

This seems to easy but I can't find anything that works for UDP (plenty
of TCP & HTTP). Any help or pointer(s) to sample code would be
appreciated.

Mike

On Wed, 16 Jul 2008 21:50:44 +0200, in
microsoft.public.dotnet.languages.vb "Armin Zingler"
<az*******@freenet.dewrote:
>"Jan Vinten" <ja********@gmail.comschrieb
>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?


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
Jul 19 '08 #5
<Ju********@home.netschrieb
Could you give this same type example for UDP, please? I have an IP
address (local subnet) or URL (remote access) and a port number. I
need to send one integer and receive 38 integers back UDP.

I have it working but by using the old VB6 style control and Strict
On does not like it so I have to run with it off.

This seems to easy but I can't find anything that works for UDP
(plenty of TCP & HTTP). Any help or pointer(s) to sample code would
be appreciated.
I'm not sure. Have you tried System.Net.Sockets.UdpClient? My example dealed
only with IP which, as you probably know, is one layer below UDP and not an
alternative.

Have a look here: http://msdn.microsoft.com/en-us/library/c19ex43h.aspx and
UDP sub topic http://msdn.microsoft.com/en-us/library/tst0kwb1.aspx
Armin

Jul 19 '08 #6
I will read these. Thanks.

I have searched for a long time for this. Maybe these have not surfaced
yet. I hope they are new and do the trick. I want to get rid of that
old ActiveX Winsock control. I suspect it is causing the program to not
work on every computer I have tried to install and run it on. Will
post working code when I have it on PSC as usual.

Mike

On Sat, 19 Jul 2008 14:20:53 +0200, in
microsoft.public.dotnet.languages.vb "Armin Zingler"
<az*******@freenet.dewrote:
>I'm not sure. Have you tried System.Net.Sockets.UdpClient? My example dealed
only with IP which, as you probably know, is one layer below UDP and not an
alternative.

Have a look here: http://msdn.microsoft.com/en-us/library/c19ex43h.aspx and
UDP sub topic http://msdn.microsoft.com/en-us/library/tst0kwb1.aspx
Jul 19 '08 #7
It is really sad that the creators/documenters of VS think that almost
no one will use Visual Basic for anything interesting. This is apparent
by the lack of Visual Basic documentation in the help files.

For instance, I really need to use the non-blocking UDP receive
variation. The documentation contains two examples: C++ and C#. They
are cryptic as well as the sparse documentation on how to code the
command.

Thanks?

http://msdn.microsoft.com/en-us/libr...inreceive.aspx

Mike

On Sat, 19 Jul 2008 14:20:53 +0200, in
microsoft.public.dotnet.languages.vb "Armin Zingler"
<az*******@freenet.dewrote:
><Ju********@home.netschrieb
>Could you give this same type example for UDP, please? I have an IP
address (local subnet) or URL (remote access) and a port number. I
need to send one integer and receive 38 integers back UDP.

I have it working but by using the old VB6 style control and Strict
On does not like it so I have to run with it off.

This seems to easy but I can't find anything that works for UDP
(plenty of TCP & HTTP). Any help or pointer(s) to sample code would
be appreciated.

I'm not sure. Have you tried System.Net.Sockets.UdpClient? My example dealed
only with IP which, as you probably know, is one layer below UDP and not an
alternative.

Have a look here: http://msdn.microsoft.com/en-us/library/c19ex43h.aspx and
UDP sub topic http://msdn.microsoft.com/en-us/library/tst0kwb1.aspx
Armin
Jul 20 '08 #8
<Ju********@home.netschrieb
It is really sad that the creators/documenters of VS think that
almost no one will use Visual Basic for anything interesting.
Unfortunatelly I agree. Or I'd better say, I agree because it's
unfortunatelly true. Sometimes I also get the impression that we are
supposed to write msgbox "hello world!" all day long.
Armin

Jul 20 '08 #9
I feel like I am getting close but nothing useful is happening and I
have an error message and code when I look at the variables. Here's the
send routine:

------------------------------------------------------

(way up top, among others)
Imports System.Net.Sockets

(in the project module)
Friend Const InverterPort As Integer = 14917

(in the form)
dim rc as integer

Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp)

Dim broadcast As IPAddress = IPAddress.Parse("10.0.0.101") '
replace with pccbackup.no-ip.info for remote testing/operation 24x7.

Dim endpoint As New IPEndPoint(broadcast, InverterPort)

Dim iUniqueBox As Integer = 1119

Dim sendbuf As Byte() = BitConverter.GetBytes(iUniqueBox)

rc = s.SendTo(sendbuf, endpoint)

------------------------------------------------------

When it runs, rc gets set to 4. I see no documentation of what this
means. Maybe that is the number of bytes sent??? They loosely refer to
knowing how many bytes are sent but never reference or code this int
value in the examples I have found.

When I breakpoint on s.SendTo and hover on the "endpoint" variable, it
gives a box with "Address" as the name of the first item in the list.
When I hover on "broadcast" in that info box, it gives a box with an
error code 10045 and a message saying that "The attempted operation is
not supported for the type of object referenced." I don't get this
displayed to me, but see it there.

It seems to want to send the correct four bytes out but nothing happens.
I never get an answer. I don't think anything is going out. I have
only replaced the previous ActiveX send statement with this routine. The
receive routine is untouched and working so far. Just doing a little at
a time.

By changing "10.0.0.101" to "pccbackup.no-ip.info" anyone can try this
remotely at any time of the day or night and see what I mean. It should
return 38 integers.

As usual, many thanks for any pointers. This little deal is making me
crazy and has been for six months! I think this is as simple as I can
make it and still not getting the job done.

Still lost Mike

P.S. Here's the Java send/receive routine this is based on:

//Step 3) Send the 4 byte request packet to port 14917
InetAddress address = InetAddress.getByName(args[0]);
DatagramPacket packet = new DatagramPacket(buffer, 4, address, 14917);
socket.send(packet);
System.out.println( "" );
System.out.println( "Sending Packet to: "+ args[0] );
//Step 4) Get the response packet from the PVM1010
packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
System.out.println( "" );
System.out.println( "Response Packet size: "+ packet.getLength() );
System.out.println( "" );

-----------------------------------------------------------

On Sat, 19 Jul 2008 14:20:53 +0200, in
microsoft.public.dotnet.languages.vb "Armin Zingler"
<az*******@freenet.dewrote:
><Ju********@home.netschrieb
>Could you give this same type example for UDP, please? I have an IP
address (local subnet) or URL (remote access) and a port number. I
need to send one integer and receive 38 integers back UDP.

I have it working but by using the old VB6 style control and Strict
On does not like it so I have to run with it off.

This seems to easy but I can't find anything that works for UDP
(plenty of TCP & HTTP). Any help or pointer(s) to sample code would
be appreciated.

I'm not sure. Have you tried System.Net.Sockets.UdpClient? My example dealed
only with IP which, as you probably know, is one layer below UDP and not an
alternative.

Have a look here: http://msdn.microsoft.com/en-us/library/c19ex43h.aspx and
UDP sub topic http://msdn.microsoft.com/en-us/library/tst0kwb1.aspx
Armin
Jul 22 '08 #10
For starters, try this:

Dim _socket As New Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp)

Dim endpoint As New
IPEndPoint(Dns.GetHostEntry("pccbackup.no-ip.info").AddressList(0), 14917)

Console.WriteLine(_socket.SendTo(BitConverter.GetB ytes(1119), endpoint))

Dim _buffer(1023) As Byte

Dim _result = _socket.Receive(_buffer, 1023, SocketFlags.None)

Console.WriteLine(_result)

For _i = 0 To _result - 1
Console.WriteLine(_buffer(_i))
Next

Console.WriteLine("***")

The result of the Socket SendTo method is the number of bytes sent. This is
clearly stated in the documentation.

Are you sure that you are expecting 38 bytes to be received. The above gets
36.

Note that changing SocketFlags.None to SocketFlags.Broadcast for the Receive
will result in a 10045 exception.

The big 'trick' is to isolate the steps down to a set that works and then
work up from there.

Also you MUST be aware that UDP is a 'connectionless' protocal and therefore
the socket cannot give you any feedback as to whether or not anything you
send is actually received at the other end.

<Ju********@home.netwrote in message
news:2o********************************@4ax.com...
>I feel like I am getting close but nothing useful is happening and I
have an error message and code when I look at the variables. Here's the
send routine:

------------------------------------------------------

(way up top, among others)
Imports System.Net.Sockets

(in the project module)
Friend Const InverterPort As Integer = 14917

(in the form)
dim rc as integer

Dim s As New Socket(AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp)

Dim broadcast As IPAddress = IPAddress.Parse("10.0.0.101") '
replace with pccbackup.no-ip.info for remote testing/operation 24x7.

Dim endpoint As New IPEndPoint(broadcast, InverterPort)

Dim iUniqueBox As Integer = 1119

Dim sendbuf As Byte() = BitConverter.GetBytes(iUniqueBox)

rc = s.SendTo(sendbuf, endpoint)

------------------------------------------------------

When it runs, rc gets set to 4. I see no documentation of what this
means. Maybe that is the number of bytes sent??? They loosely refer to
knowing how many bytes are sent but never reference or code this int
value in the examples I have found.

When I breakpoint on s.SendTo and hover on the "endpoint" variable, it
gives a box with "Address" as the name of the first item in the list.
When I hover on "broadcast" in that info box, it gives a box with an
error code 10045 and a message saying that "The attempted operation is
not supported for the type of object referenced." I don't get this
displayed to me, but see it there.

It seems to want to send the correct four bytes out but nothing happens.
I never get an answer. I don't think anything is going out. I have
only replaced the previous ActiveX send statement with this routine. The
receive routine is untouched and working so far. Just doing a little at
a time.

By changing "10.0.0.101" to "pccbackup.no-ip.info" anyone can try this
remotely at any time of the day or night and see what I mean. It should
return 38 integers.

As usual, many thanks for any pointers. This little deal is making me
crazy and has been for six months! I think this is as simple as I can
make it and still not getting the job done.

Still lost Mike

P.S. Here's the Java send/receive routine this is based on:

//Step 3) Send the 4 byte request packet to port 14917
InetAddress address = InetAddress.getByName(args[0]);
DatagramPacket packet = new DatagramPacket(buffer, 4, address, 14917);
socket.send(packet);
System.out.println( "" );
System.out.println( "Sending Packet to: "+ args[0] );
//Step 4) Get the response packet from the PVM1010
packet = new DatagramPacket(buffer, buffer.length);
socket.receive(packet);
System.out.println( "" );
System.out.println( "Response Packet size: "+ packet.getLength() );
System.out.println( "" );

-----------------------------------------------------------

On Sat, 19 Jul 2008 14:20:53 +0200, in
microsoft.public.dotnet.languages.vb "Armin Zingler"
<az*******@freenet.dewrote:
>><Ju********@home.netschrieb
>>Could you give this same type example for UDP, please? I have an IP
address (local subnet) or URL (remote access) and a port number. I
need to send one integer and receive 38 integers back UDP.

I have it working but by using the old VB6 style control and Strict
On does not like it so I have to run with it off.

This seems to easy but I can't find anything that works for UDP
(plenty of TCP & HTTP). Any help or pointer(s) to sample code would
be appreciated.

I'm not sure. Have you tried System.Net.Sockets.UdpClient? My example
dealed
only with IP which, as you probably know, is one layer below UDP and not
an
alternative.

Have a look here: http://msdn.microsoft.com/en-us/library/c19ex43h.aspx
and
UDP sub topic http://msdn.microsoft.com/en-us/library/tst0kwb1.aspx
Armin
Jul 23 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Takashi Yamamoto | last post by:
Hi All, Need help on the ff questions, please: 1). if I developed a .NET application, what is the earliest OS required to run the application for end users ? 2). Do I need to bundle any...
1
by: Harry | last post by:
I want to bind three elements of an seven element arraylist to a data grid. I cannot find an example of complex binding specific fields of an array list to a datagrid. Any examples would be...
1
by: Claire | last post by:
Hi Im writing an application using the above controls in blocking mode. Ive not used them before and I'm more used to asynchronous socket programming utilizing socket events. As there are no...
0
by: Imar Spaanjaars | last post by:
Hi there, Can anyone tell me how to perform two-way binding with custom properties in ASP.NET 2? Let's say I have a custom class called Person that I want to bind to a FormView (using an...
4
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
0
by: m.posseth | last post by:
Hello All , I encounter the following strange situation ,,, I have developed a remoting component , this functions fine ,,, however a customer wanted to have the ability to access this...
7
by: abcd | last post by:
I have asp.net application which was tested and developed on asp.net 1.1. WIll my application work on asp.net 2.0 also. Can I say asp.net 2.0 is supported automatically for the applications which...
2
by: markcash | last post by:
I am trying to bind an ASP.NET image control to images stored locally on the hard drive of the web server. The images will be frequently changing so I am dynamically creating the filename. I am...
1
by: chandan | last post by:
Hi, I am using aspnet_regiss.exe utility from command prompt to register asp.net to IIS. But my requirement is to do it programmatically so that client need not worry about this registration. Can...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.