LAN chat client problem

Ljuslykta

Member
Joined
Mar 9, 2010
Messages
9
Programming Experience
3-5
I'm sorry if this in the wrong section.

I'm trying to learn network programming. I have found a pretty easy example on internet, here: Socket Programming using Visual Basic.Net - .Net Articles & Samples

He describes how to make a simple chat program. The problem is thatit only works on one computer. Somehow i want to make the server use the internal ip or a
Hamachi adress, but i don't know how. Here's the code:

The Client:

Imports System.IO
Imports System.Net.Sockets

VB.NET:
Module Module1
 
    Sub Main()
 
        Try
            Console.WriteLine("Connecting to 8585 Local Host")
            Dim serverListener As New TcpClient("localhost", 8585)
            Dim readStream As Stream = serverListener.GetStream
            serverListener.SendBufferSize = 256
 
            Console.WriteLine("Input Lines:")
            Dim str As String = Console.ReadLine()
 
            While True
                Dim sendBuff As Byte() = System.Text.Encoding.ASCII.GetBytes(str)
 
                readStream.Write(sendBuff, 0, sendBuff.Length)
 
                If str.StartsWith(".") Then
                    GoTo Done
                End If
 
                str = Console.ReadLine()
            End While
 
Done:       Console.WriteLine("Done")
 
        Catch exp As Exception
 
            Console.WriteLine("Exception: " + exp.ToString())
 
        End Try
 
 
    End Sub
 
End Module

And the server:

VB.NET:
Imports System.IO
Imports System.Net.Sockets
 
Module Module1
 
    Sub Main()
        Console.WriteLine("")
        Dim clientListener As New TcpListener(8585)
 
        clientListener.Start()
 
        Console.WriteLine("")
        Dim mySocket As Socket = clientListener.AcceptSocket()
 
        Console.WriteLine("")
 
        Dim recieveBuff(225) As Byte
        mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None)
        Dim str As String = System.Text.Encoding.ASCII.GetString(recieveBuff, 0, recieveBuff.Length).Trim(Microsoft.VisualBasic.ChrW(0))
 
        While Not str.StartsWith(".")
            Console.WriteLine(str)
 
            mySocket.Receive(recieveBuff, recieveBuff.Length, SocketFlags.None)
            str = System.Text.Encoding.ASCII.GetString(recieveBuff, 0, recieveBuff.Length).Trim(Microsoft.VisualBasic.ChrW(0))
 
        End While
 
        Console.WriteLine("")
        clientListener.Stop()
 
    End Sub
 
End Module

Any ideas?
 
This is not a programming issue. From the internet, your network only has one IP address and that's all external machines can connect to. You need to set up the gateway on your network to forward traffic based on port number. You should look into port forwarding, which will be configured slightly differently for every router.
 
That is no different, when using Hamachi you and other clients have an assigned IP address in 5.0.0.0/8 range within the VPN. With both TcpListener and TcpClient you can specify which ip and port to listen from and connect to.
 
The problem is that I don't know the code to listen to any other ip than the local. I tried to change the ClientListener to this:

Dim clientListener As New TcpListener(Net.IPAddress.Parse("5.75.456.5"),8585)¨
And the ip is my Hamachi ip.

When I do that, I get an error, like "Unavalibale IP"
 
Seems weird that you have that IP, and then you don't. Perhaps there is documentation for that system you can consult.
 
I have the some problem :( cant find one simple example (im also new to internet programming) that works.. only work if both server and client in same machine!!

Did you do it Ljuslykta? Could you put it to work?

How is it suposed to get ip adress? I tried in 2 ways: from ipconfig in console mode AND by website What Is My IP Address - Shows Your IP Address and get diferent ones !!!

I know must be very ignorant question... but really i tried for some hours to solve this problem. If anyone can put this simple example running and could enter msn or skype and just putted it to work with me. Would be very veryyy greatfull! Any help welcome!
 
Last edited by a moderator:
Back
Top