GPSBots.com
Home :: [Photos] : [Tutorials] : [Projects] : [Resources] : [About Us]

VB: Simple TCP Server & Client

VB: Simple TCP Server & Client
Visual Basic 6 comes with a built in control called the "Microsoft Winsock Control". Using that, you can easily add a tcp client or server to your program. It's an extremely easy way to add internet connectivity for your devices.
  • tcp_simple.frm - The VB form
  • tcp_simple.vbp - The VB project
  • tcp_simple.exe - The compiled EXE
  • tcp_simple.zip - The above files in a zip file

    How it Works

    This project sets up a one-way chat session from the client to the server. Even though both modes are in the same program, I hope that the code is clearly distinguished.

    Notes

    ' These set up which port to listen and connect on
    tcpServer.LocalPort = 1337 ' what to listen on
    tcpServer.Listen ' set up the TCP server
    tcpClient.RemotePort = 1337 ' what to connect to

    Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
        tcpServer.Accept requestID ' need to accept the connection
    End Sub

    Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
        Dim val As String
        tcpServer.GetData val
        ' now do whatever you want with val
    End Sub

    ' To send the data you just call the send function
    tcpClient.SendData val


  •