Membuat Program Chatting Client Server Sederhana dengan VB6
1. Bahasa pemrograman Visual Basic 6
2. Komponen Yang digunakan
Download Source Code Disini
1. Bahasa pemrograman Visual Basic 6
2. Komponen Yang digunakan
- Program Server
- 2 buah ListBox
- 2 buah Panel
- 1 buah Text
- 1 buah Button (caption = Send, name = cmdsend)
- 1 buah winsock
- Program Klien
- 2 buah Text
- 1 buah ListBox
- 1 buah Button (caption = Send)
- 1 buah winsock
4. Source code Server
- Mengaktifkan Program Server maka server akan otomatis meakukan listening dengan winsock melalui alamat IP dan port yang digunakan dan telah ditentukan
- Mengatifkan Program Klien
- untuk menghubungan program Klien dan server tekan tombol connect pada klien, maka otomatis klien dan server aan terkoneksi
- Masukan pesan pada text pesan dan tekan tombol send untuk mengirim pesan maka otomatis di program server akan muncul pesan dari klien
- Untuk mengirim pesan ke klien pilih klien yang ada di listbox daftar klien, lalu masukkan pesan pada kotak pesan, tekan tombol send, maka otomatis di klien yang dipilih akan muncul pesan dari server
Option Explicit Private Sub Winsock1_Close(Index As Integer) List1.List(Index - 1) = Winsock1(Index).RemoteHostIP & " on port " & Winsock1(Index).RemotePort & " [disconnected]" End Sub Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long) Dim i As Long Dim j As Long Dim newWinsock As Boolean On Error GoTo errHandle If Index = 0 Then For i = 1 To Winsock1.UBound If (Winsock1(i).State = sckClosed) Or (Winsock1(i).State = sckClosing) Then j = i Exit For End If Next i If j = 0 Then Call Load(Winsock1(Winsock1.UBound + 1)) j = Winsock1.UBound newWinsock = True End If With Winsock1(j) Call .Close Call .Accept(requestID) End With If newWinsock Then List1.AddItem Winsock1(j).RemoteHostIP & " on port " & Winsock1(j).RemotePort & " [connected]" Else List1.List(j - 1) = Winsock1(j).RemoteHostIP & " on port " & Winsock1(j).RemotePort & " [connected]" End If End If Exit Sub errHandle: Call Winsock1(0).Close End Sub Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long) Dim strData As String Winsock1(Index).GetData strData List2.AddItem strData End Sub Private Sub cmdSend_Click() On Error Resume Next If List1.ListIndex < 0 Then MsgBox "Komputer laen belum dipilih", vbExclamation, "Peringatan" Exit Sub End If Winsock1(List1.ListIndex + 1).SendData Text1.Text List2.AddItem "Saya: " + Text1.Text Text1.Text = "" DoEvents End Sub Private Sub Form_Load() With Winsock1(0) .Close .LocalPort = 1108 .Listen End With End SubSource code Klien
Option Explicit Private Function startConnect(ByVal ipServer As String) As Boolean Dim i As Long If Winsock1.State <> sckClosed Then Winsock1.Close Call Winsock1.Connect(ipServer, 1108) With Winsock1 Do While .State <> sckConnected DoEvents If .State = sckError Then Exit Function Loop End With startConnect = True End Function Private Sub cmdConnect_Click() Text1.Text = "" If cmdConnect.Caption = "Connect" Then If startConnect(txtServer.Text) Then cmdConnect.Caption = "Disconnet" cmdSend.Enabled = True Else cmdSend.Enabled = False End If Else cmdSend.Enabled = False Winsock1.Close cmdConnect.Caption = "Connect" End If End Sub Private Sub cmdSend_Click() Winsock1.SendData Text1.Text DoEvents List1.AddItem "Saya : " & Text1.Text Text1.Text = "" End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim strData As String Winsock1.GetData strData List1.AddItem "Server: " & strData End Sub Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) MsgBox "Error: " & Description Winsock1.Close End Sub
Download Source Code Disini