Results 1 to 14 of 14

Thread: [RESOLVED] [2005] Tcp Communication..?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Resolved [RESOLVED] [2005] Tcp Communication..?

    Hello i am looking into Tcp Communication.

    As i just want to know the basics i am trying to make a program that will act as the Client and the Server (correct me if this is not possible). I want it so when the user loads the form using Tcp Communication the text of label1 will change to "This was a success!".

    I have declared to variables, tell me if i need to declare more socket related materials...

    vb Code:
    1. Dim Client As New System.Net.Sockets.TcpClient
    2. Dim Listener As System.Net.Sockets.TcpListener

    If anyone has any links for Tcp Communication can you supply them?

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Tcp Communication..?

    Have a look at the link in my signature, it should tell you a thing or two.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005] Tcp Communication..?

    I checked it out, i copied the code, but with this piece of code:

    Code:
    New System.Net.Sockets.TcpClient("localhost", 43001)
    What do i do here?
    I have a router do i need to create a port?
    Also where it says localhost do i write the IP of the hosting computer..?

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Tcp Communication..?

    That line of code simply creates a TcpClient object which attempts to connect to localhost on port 43001. Specifying localhost as a hostname is equal to specifying the loopback ip 127.0.0.1.
    If you want to connect to a server on the local host, keep "localhost", otherwise, change it to the servers IP.

    You do not need to configure your router for outgoing traffic.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005] Tcp Communication..?

    So do i have to do this:

    vb Code:
    1. New System.Net.Sockets.TcpClient("localhost", 43001)

    or can i just do this:

    vb Code:
    1. New System.Net.Sockets.TcpClient("localhost")

    and if you can do it without the port then can i connect to the server ip without a port as whell...?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Tcp Communication..?

    Look at the TcpClient documentation. Is there a constructor that accepts a single String argument?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005] Tcp Communication..?

    Well i just tried it and when you leave out the port it just tries to convert the string "localhost" to an integer, so it basicly tries to read localhost as the port i think...

    Well what can i do to solve my problem?.. the error message when trying to connect with the default 43001 and localhost is:

    No connection could be made because the target machine actively refused it 127.0.0.1:43001

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Tcp Communication..?

    Well don't just try it. Read first to see if what you want to do actually makes sense. Also, if there's any trying to convert going on then you must have Option Strict Off, so I suggest you change that immediately.

    Do you have a server listening on port 43001? The client is trying to connect to a server. If there's no server then what's the client going to connect to?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005] Tcp Communication..?

    Well i didnt open port 43001 unless it is suppose to be open by default.
    I completly copied Athiest's code so i can test it out, learn from it and then maybe use it. I will look at the code again.

  10. #10
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Tcp Communication..?

    Its not about opening ports or not. You can not tell your TcpClient to connect to a specific port without first having a TcpListener (a socket), listening on that very port. If you dont, you'll get a message that the target machine actively refused the connection.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  11. #11
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2005] Tcp Communication..?

    Try creating a listner first and then a client and see if that works

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] Tcp Communication..?

    Ah, to learn. This is from the documentation for the TcpClient class:
    In order for TcpClient to connect and exchange data, a TcpListener or Socket created with the TCP ProtocolType must be listening for incoming connection requests.
    This is from the code example in that same topic:
    Code:
          ' Create a TcpClient.
          ' Note, for this client to work you need to have a TcpServer 
          ' connected to the same address as specified by the server, port
          ' combination.
          Dim port As Int32 = 13000
          Dim client As New TcpClient(server, port)
    That's pretty clear to me. You'd think with all my ranting someone everyone around here have learned to read the documentation first, but it still seems to be the last option.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2008
    Posts
    1,754

    Re: [2005] Tcp Communication..?

    Ok, it works now. Will it work if i take the server and put open up the server program on a different computer then my own lets say a family members of mine? Do i have to do anything in the server? I am pretty sure i only need to specify the IP of the family members computer/where the server is running.

  14. #14
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] Tcp Communication..?

    Indeed, if the server is running on a computer in your local network, all you must do is specify that computers local IP instead of "localhost". If you want to be able to connect to it from outside of the local network, you'll have to set up port forwarding in your router.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width