VB.NET client server question.

Soldato
Joined
18 Oct 2002
Posts
15,861
Location
NW London
I've created a client and server application.

The client - server are working able successfully passing messages to/from one another, when both are run on the same PC.

The Server will be run in my house, on a computer which is on a network of 2 PCs (MainPC and BedroomPC).

lets say for example,
that the ip address of my server = 1.2.3.4
the machine name of my server = MainPC
the port number of my server = 8888

When the client and server are running on the same PC, the following code works nicely:

clientSocket = New TcpClient()
clientSocket.Connect("MainPC", 8888)

Dim serverStream As NetworkStream
serverStream = clientSocket.GetStream()
Dim messageToSend As String = "blah"
Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(messageToSend)
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()

Now, I am going to run the client on a different PC, over the internet.
So, I want to connect the client, to the server which is already running. The server details are:

that the ip address of my server = 1.2.3.4
the machine name of my server = MainPC
the port number of my server = 8888



How would I connect the client to the above server, bearing in mind that I have to enter the ip address, machine name and port in the following code:

clientSocket = New TcpClient()
clientSocket.Connect("MainPC", 8888)

How can I insert the ip address of MainPC, into the statements above, bearing in mind that the server is running on only 1 computer ("MainPC") on my network ?

Thanks.
 
Rather than use clientSocket.Connect("MainPC", 8888) you should just be able to use your internet IP address (see http://whatismyip.com) or use a dynamic hostname service such as http://no-ip.org or http://dyndns.com so that if you have a dynamic IP it will always get updated to the right one.

Using a dynamic IP service you could just use say:
clientSocket.Connect("sunama.no-ip.org", 8888)


Make sure you forward port 888 on your router to your server too.

http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.connect.aspx

I understand this. And this would not be a problem if I were running only 1 PC on my network. However, I am running 2 PCs and it is the "MainPC" which the client needs to connect to. How do I prevent it making an attempt to connect to the other PC? And how do I ensure that the client request goes straight to "MainPC" and not "BedroomPC"?
 
Last edited:
Back
Top Bottom