-
Nov 8th, 2004, 01:08 PM
#1
Thread Starter
Junior Member
TcpListener, Cannot change the ip address.
You will have to excuse me because I am very new to programming. I am trying to modify Micorosofts example code for a TcpListener. If I leave it alone at address "127.0.0.1" it works just like it is suppose to. But if i change the address to lets say "10.1.1.1" It no longer works. It throws up an Exception: Requested Address is not valid within its context. Here's the code if you want to know what I am changing.
Any Ideas or corrections are greatly welcome.
Thanks
gunslngr
VB Code:
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Imports System.Configuration
Class MyTcpListener
Public Shared Sub Main()
Try
' Set the TcpListener on port 13000.
Dim port As Int32 = 13000
Dim localAddr As IPAddress = IPAddress.Parse("10.1.1.1")
Dim server As New TcpListener(localAddr, port)
' Start listening for client requests.
server.Start()
' Buffer for reading data
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing
' Enter the listening loop.
While True
Console.Write("Waiting for a connection... ")
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
Console.WriteLine("Connected!")
data = Nothing
' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Console.WriteLine([String].Format("Received: {0}", data))
' Process the data sent by the client.
data = data.ToUpper()
Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
Console.WriteLine([String].Format("Sent: {0}", data))
i = stream.Read(bytes, 0, bytes.Length)
End While
' Shutdown and end connection
client.Close()
End While
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try
Console.WriteLine(ControlChars.Cr + "Hit enter to continue...")
Console.Read()
End Sub 'Main
End Class 'MyTcpListener
-
Nov 8th, 2004, 01:22 PM
#2
You can't stick any ip address that floats your boat in there. It has to be either 127.0.0.1 or the actual ip address of that machine it is running.
-
Nov 8th, 2004, 01:25 PM
#3
Frenzied Member
What's the SocketException.ErrorCode?
-
Nov 8th, 2004, 01:25 PM
#4
Thread Starter
Junior Member
Cander that is actually a pc on the network here. Actually it is a Definity Server. But it does not seem to like any address other than "127.0.0.1".
-
Nov 8th, 2004, 01:27 PM
#5
But is that app actually running on it?
-
Nov 8th, 2004, 01:32 PM
#6
Thread Starter
Junior Member
Mike,
All it is telling me is The requested address is not valid in its context
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.Net.Sockets.TcpListener.Start()
at ConsoleApplication1.MyTcpListener.Main() in C:\vbprojects\ConsoleApplication1\module1.vb:line 22
Line 22 is the server.Start() line.
You will need to excuse my ignorance if this is not what you wanted.
-
Nov 8th, 2004, 01:32 PM
#7
Frenzied Member
Yeah, like Cander said. When you call the .Start method on a TcpListener, it starts to listen. This is not the place where you tell your app to connect to a different device. Hope that makes sense.
FWIW, I use this code to bind to whatever address on my first nic
VB Code:
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, properties.listenPort)
-
Nov 8th, 2004, 01:34 PM
#8
Thread Starter
Junior Member
Thanks guys I think i see what you are telling me. The TcpListener code is only listening on the pc not the actual network. I will continue to play with this and see what i can come up with. Thanks for all the help guys.
Gunslngr
-
Nov 8th, 2004, 01:36 PM
#9
Frenzied Member
If you catch the SocketExcption, which you're doing, and then get the ErrorCode property of the SocketException, that's the Winsock error code and you can look up that number and try to fix.
But Cander is right, you need to specify the machine's IP address that the app is running on when you want to start listening for incoming connections.
It's a different thing to connect to another machine, what you're doing there is opening a socket that will accept requests, that's why it needs to be your ip.
-
Nov 8th, 2004, 01:38 PM
#10
Like it says in MSDN about Tcplistener
Initializes a new instance of the TcpListener class that listens for incoming connection attempts on the specified local IP address and port number.
-
Nov 8th, 2004, 01:41 PM
#11
Thread Starter
Junior Member
I missed the local part. I just started coding last week and I am probably getting in over my head but I am going to try and keep going. Again thanks for all the help guys.
Gunslngr
-
Nov 8th, 2004, 01:46 PM
#12
Thread Starter
Junior Member
It works perfect now thanks so much guys.
-
Nov 8th, 2004, 01:47 PM
#13
Good luck.
-
Nov 8th, 2004, 01:54 PM
#14
Frenzied Member
Also, check out the sticky 101 VB.NET Examples . One of the examples is TCP communication, and has sample client and server code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|