[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:
Dim Client As New System.Net.Sockets.TcpClient
Dim Listener As System.Net.Sockets.TcpListener
If anyone has any links for Tcp Communication can you supply them?
Re: [2005] Tcp Communication..?
Have a look at the link in my signature, it should tell you a thing or two.
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..?
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.
Re: [2005] Tcp Communication..?
So do i have to do this:
vb Code:
New System.Net.Sockets.TcpClient("localhost", 43001)
or can i just do this:
vb Code:
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...?
Re: [2005] Tcp Communication..?
Look at the TcpClient documentation. Is there a constructor that accepts a single String argument?
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:
Quote:
No connection could be made because the target machine actively refused it 127.0.0.1:43001
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?
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.
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.
Re: [2005] Tcp Communication..?
Try creating a listner first and then a client and see if that works :)
Re: [2005] Tcp Communication..?
Ah, to learn. This is from the documentation for the TcpClient class:
Quote:
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:
Quote:
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.
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.
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.