Click here to Skip to main content
15,891,473 members
Articles / Programming Languages / Visual Basic
Article

A TCP Chat Application

Rate me:
Please Sign up or sign in to vote.
2.22/5 (9 votes)
23 Feb 2008CPOL 45.8K   18   6
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.

VB.NET
 '' 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
VB.NET
'' 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)


Written By
Software Developer (Junior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDoes it work on WAN? Pin
dj.3agl34-Jan-14 6:05
dj.3agl34-Jan-14 6:05 
GeneralMy vote of 4 Pin
SirLaGsALoT07F368621-May-11 4:24
SirLaGsALoT07F368621-May-11 4:24 
GeneralMy vote of 1 Pin
Dave Kreskowiak9-Feb-09 3:11
mveDave Kreskowiak9-Feb-09 3:11 
QuestionTCP chat Pin
priyajeni4-Sep-08 23:44
priyajeni4-Sep-08 23:44 
Generalreference tcpserver Pin
my274-Mar-08 15:29
my274-Mar-08 15:29 
AnswerRe: reference tcpserver Pin
Imran A Momin4-Mar-08 17:48
Imran A Momin4-Mar-08 17:48 

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

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