Click here to Skip to main content
Click here to Skip to main content

Tagged as

A TCP Chat Application

, 23 Feb 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A chat application using a TCP protocol. This application is for VB6 User

Introduction

Its a chat application using the TCP/IP sockets. To get this application running you need to add the control for the communication through the etharnet.

Background

This was the simple application created for the seminar at college level. Hope will be helpful for the beginners in the communication programming

Using the code

There are couple of section that need to be coded before launching the application. We need to create two forms One will act as a server and other will be acting as a client. Where we can manupulate as a client server chat application

Now let see the coding of the Server Side Form

There are couple of events to be handled during the programming.

 '' Form Load
Private Sub Form_Load()
    On Error GoTo err
    tcpServer.LocalPort = 1001
    tcpServer.Listen
    Exit Sub
err:
    MsgBox Error
End Sub    

 
'' Commnad for Sending Message
Private Sub Command1_Click()
    On Error GoTo err
    If txtSendData.Text <> "" Then
        tcpServer.SendData txtSendData.Text
        main.AddItem (tcpServer.LocalHostName + " (You)  :  " + txtSendData.Text)
        Beep
        txtSendData.Text = ""
    End If
    Exit Sub
err:
    MsgBox("No Connection", vbInformation)
End Sub

 
'' Command to send the message in reserve format
Private Sub Command2_Click()
    Dim a
    a = StrReverse(txtSendData.Text)
    On Error GoTo err
    If txtSendData.Text <> "" Then
        tcpServer.SendData(a)
        main.AddItem(tcpServer.LocalHostName + " (You)  :  " + Trim(a))
        Beep
        txtSendData.Text = ""
    End If
    Exit Sub
err:
    MsgBox("No Connection", vbInformation)
End Sub


'' Code to accept incoming Client Connection Request
Private Sub tcpServer_ConnectionRequest (ByVal requestID As Long)
On Error GoTo err
If tcpServer.State <> sckClosed Then _
    tcpServer.Close
    tcpServer.Accept requestID
    Exit Sub
err:
    MsgBox Error
End Sub

'' Code to when the data recieved 
Private Sub tcpServer_DataArrival (ByVal bytesTotal As Long)
On Error GoTo err
    Dim strData As String
    tcpServer.GetData strData
    txtOutput.Text = strData
    main.AddItem (tcpServer.RemoteHostIP + " (Friend)  :  " + txtOutput.Text)
    Beep
    Exit Sub
err:
    MsgBox Error
End Sub


Now let see the coding of the Client Side Form
'' Form Load
Private Sub Form_Load()
On Error GoTo err
    cmdDisconnect.Enabled = False
    tcpClient.RemotePort = 1001
    Exit Sub
err:    
    MsgBox Error
End Sub

'' Code for connecting to the server
Private Sub cmdConnect_Click()
On Error GoTo err
    tcpClient.RemoteHost = Trim(ip.Text)
    cmdDisconnect.Caption = "Disconnect To " & ip.Text
    tcpClient.Connect
    cmdDisconnect.Enabled = True
    cmdConnect.Enabled = False
    Exit Sub
err:
    MsgBox Error
End Sub

'' Code for  disconnecting from the server
Private Sub cmdDisconnect_Click()
      On Error GoTo err    
    tcpClient.Close
    cmdDisconnect.Enabled = False
    main.Clear
    txtSendData.Text = ""
    cmdConnect.Enabled = True
    Exit Sub
err:
    MsgBox Error
End Sub

'' Code to send the message to the server
Private Sub Command1_Click()
    On Error GoTo err
    If txtSendData.Text <> "" Then
        tcpClient.SendData txtSendData.Text
        main.AddItem (tcpClient.LocalIP + " (You)  :  " + txtSendData.Text)
        Beep
        txtSendData.Text = ""
    End If
    Exit Sub
err:
    MsgBox( "Connection not established", vbInformation)
End Sub
 
'' Code to send the message in the reverse format 
Private Sub Command2_Click()
    Dim a
    a = StrReverse(txtSendData.Text)
    On Error GoTo err
    If txtSendData.Text <> "" Then
        tcpClient.SendData(a)
        main.AddItem (tcpClient.LocalIP + " (You)  :  " + Trim(a))
        Beep
        txtSendData.Text = ""
    End If
    Exit Sub
err:
    MsgBox ("Connection not established", vbInformation)
End Sub
 
'' Code for when the data recieved 
Private Sub tcpClient_DataArrival (ByVal bytesTotal As Long)
    On Error GoTo err
    Dim strData As String
    tcpClient.GetData strData
    txtOutput.Text = strData
    main.AddItem (tcpClient.RemoteHost + " (Friend)  :  " + txtOutput.Text)
    Beep
    Exit Sub
err:
    MsgBox Error
End Sub

tcp053.jpg

tcp055.jpg

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Imran A Momin
Software Developer (Junior)
India India
No Biography provided

Comments and Discussions

 
QuestionDoes it work on WAN? Pinmember3AgL3 DeeJay4-Jan-14 7:05 
GeneralMy vote of 4 PinmemberSirLaGsALoT07F368621-May-11 5:24 
GeneralMy vote of 1 PinmvpDave Kreskowiak9-Feb-09 4:11 
QuestionTCP chat Pinmemberpriyajeni5-Sep-08 0:44 
Generalreference tcpserver Pinmembermy274-Mar-08 16:29 
AnswerRe: reference tcpserver PinmemberImran A Momin4-Mar-08 18:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411028.1 | Last Updated 24 Feb 2008
Article Copyright 2008 by Imran A Momin
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid