Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

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

193 views
Skip to first unread message

Jan Vinten

unread,
Jul 16, 2008, 5:00:01 AM7/16/08
to
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

Armin Zingler

unread,
Jul 16, 2008, 3:50:44 PM7/16/08
to
"Jan Vinten" <jan.v...@gmail.com> schrieb


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

unread,
Jul 16, 2008, 4:28:11 PM7/16/08
to
"Armin Zingler" <az.n...@freenet.de> schrieb
> [Code]

Maybe I misunderstood your question.(?)


Armin

Jan Vinten

unread,
Jul 17, 2008, 3:00:21 AM7/17/08
to
On Jul 16, 9:50 pm, "Armin Zingler" <az.nos...@freenet.de> wrote:
> "Jan Vinten" <jan.vin...@gmail.com> schrieb

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

Just_...@home.net

unread,
Jul 19, 2008, 7:48:53 AM7/19/08
to
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"

Armin Zingler

unread,
Jul 19, 2008, 8:20:53 AM7/19/08
to
<Just_...@home.net> schrieb

> 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

Just_...@home.net

unread,
Jul 19, 2008, 7:46:04 PM7/19/08
to
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.n...@freenet.de> wrote:

Just_...@home.net

unread,
Jul 19, 2008, 10:15:05 PM7/19/08
to
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/library/system.net.sockets.udpclient.beginreceive.aspx

Mike

On Sat, 19 Jul 2008 14:20:53 +0200, in


microsoft.public.dotnet.languages.vb "Armin Zingler"
<az.n...@freenet.de> wrote:

Armin Zingler

unread,
Jul 20, 2008, 6:09:42 AM7/20/08
to
<Just_...@home.net> schrieb

> 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

Just_...@home.net

unread,
Jul 22, 2008, 3:43:33 AM7/22/08
to
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.n...@freenet.de> wrote:

Stephany Young

unread,
Jul 23, 2008, 1:34:16 AM7/23/08
to
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.GetBytes(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.

<Just_...@home.net> wrote in message
news:2o1b84pibcfsjbc1a...@4ax.com...

0 new messages