Click here to Skip to main content
11,577,710 members (61,784 online)
Click here to Skip to main content

Tagged as

A TCP Chat Application

, 23 Feb 2008 CPOL 27.8K 18
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

You may also be interested in...

Comments and Discussions

 
QuestionDoes it work on WAN? Pin
3AgL3 DeeJay4-Jan-14 6:05
member3AgL3 DeeJay4-Jan-14 6:05 
GeneralMy vote of 4 Pin
SirLaGsALoT07F368621-May-11 4:24
memberSirLaGsALoT07F368621-May-11 4:24 
GeneralMy vote of 1 Pin
Dave Kreskowiak9-Feb-09 3:11
mvpDave Kreskowiak9-Feb-09 3:11 
QuestionTCP chat Pin
priyajeni4-Sep-08 23:44
memberpriyajeni4-Sep-08 23:44 
Generalreference tcpserver Pin
my274-Mar-08 15:29
membermy274-Mar-08 15:29 
AnswerRe: reference tcpserver Pin
Imran A Momin4-Mar-08 17:48
memberImran A Momin4-Mar-08 17: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.150603.1 | Last Updated 24 Feb 2008
Article Copyright 2008 by Imran A Momin
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid