TcpListener, Cannot change the ip address.-VBForums
Results 1 to 14 of 14

Thread: TcpListener, Cannot change the ip address.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    19

    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:
    1. Imports System
    2. Imports System.IO
    3. Imports System.Net
    4. Imports System.Net.Sockets
    5. Imports System.Text
    6. Imports Microsoft.VisualBasic
    7. Imports System.Configuration
    8.  
    9.  
    10. Class MyTcpListener
    11.  
    12.     Public Shared Sub Main()
    13.  
    14.         Try
    15.             ' Set the TcpListener on port 13000.
    16.             Dim port As Int32 = 13000
    17.             Dim localAddr As IPAddress = IPAddress.Parse("10.1.1.1")
    18.  
    19.             Dim server As New TcpListener(localAddr, port)
    20.  
    21.             ' Start listening for client requests.
    22.             server.Start()
    23.  
    24.             ' Buffer for reading data
    25.             Dim bytes(1024) As [Byte]
    26.             Dim data As [String] = Nothing
    27.  
    28.             ' Enter the listening loop.
    29.             While True
    30.                 Console.Write("Waiting for a connection... ")
    31.  
    32.                 ' Perform a blocking call to accept requests.
    33.                 ' You could also user server.AcceptSocket() here.
    34.                 Dim client As TcpClient = server.AcceptTcpClient()
    35.                 Console.WriteLine("Connected!")
    36.  
    37.                 data = Nothing
    38.  
    39.                 ' Get a stream object for reading and writing
    40.                 Dim stream As NetworkStream = client.GetStream()
    41.  
    42.                 Dim i As Int32
    43.  
    44.                 ' Loop to receive all the data sent by the client.
    45.                 i = stream.Read(bytes, 0, bytes.Length)
    46.                 While (i <> 0)
    47.                     ' Translate data bytes to a ASCII string.
    48.                     data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
    49.                     Console.WriteLine([String].Format("Received: {0}", data))
    50.  
    51.                     ' Process the data sent by the client.
    52.                     data = data.ToUpper()
    53.  
    54.                     Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(data)
    55.  
    56.                     ' Send back a response.
    57.                     stream.Write(msg, 0, msg.Length)
    58.                     Console.WriteLine([String].Format("Sent: {0}", data))
    59.  
    60.                     i = stream.Read(bytes, 0, bytes.Length)
    61.  
    62.                 End While
    63.  
    64.                 ' Shutdown and end connection
    65.                 client.Close()
    66.             End While
    67.         Catch e As SocketException
    68.             Console.WriteLine("SocketException: {0}", e)
    69.         End Try
    70.  
    71.         Console.WriteLine(ControlChars.Cr + "Hit enter to continue...")
    72.         Console.Read()
    73.     End Sub 'Main
    74.  
    75. End Class 'MyTcpListener

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    What's the SocketException.ErrorCode?

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    19
    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".

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    But is that app actually running on it?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    19
    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.

  7. #7
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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:
    1. Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
    2.             Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
    3.             Dim localEndPoint As New IPEndPoint(ipAddress, properties.listenPort)

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    19
    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

  9. #9
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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.

  10. #10
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    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.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    19
    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

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Nov 2004
    Posts
    19
    It works perfect now thanks so much guys.



  13. #13
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Good luck.
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  14. #14
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.