selamat datng di blog saya terimakasih sudah mengunjungi

Thursday, May 12, 2011

cara membuat program chating dengan VB

Visual Basic  Tags: ,   
DOWNLOAD  Source Code

Source Code Visual basic yang menarik untuk dipelajari bagi pemula yang yang sedang mendalami komunikasi TCP/IP.
Untuk membuat program chating sendiri dengan visual Basic kita memerlukan komponen winsock. Winsock pada Visual Basic mengizinkan kepada kita untuk mengirim dan menerima perintah (pesan) dari computer satu ke computer yang lain. Pengiriman dan penerimaan perintah (pesan) melalui port dan IP tujuan yang telah ditentukan. Berikut akan kita bahas bagaimana cara menggunakan Winsock di  Visual Basic dalam membuat Program Chating.
1. Mengaktifkan Komponen Winsock di Visual Basic
Untuk mengaktifkan Komponen winsock yaitu klik kanan pada menu toolbar – Componens (lihat pada gambar 1.1)

Gambar 1.1
pilih menu component – aktifkan “Microsoft Winsock Control 6” maka akan muncul komponen winsock (dilingkari warna merah, bergambar komputer) seperti pada gambar 1.2.

Gambar 1.2
Contoh program ini terdiri dari 2 versi, computer klien dan server.
Cara mengoprasikannya sebagaiberikut :
Aplikasi server harus dijalankan dahulu, aplikasi ini tidak akan kelihatan di desktop.
Kemudian jalankan aplkasi klien pada  mesin manapun yang terdapat dalam jaringan (LAN). Kemudian masukan nomor IP atau nama komputer target di mana server target sedang berjalan. Berikut adalah gambar dan source Code dalam proses pembuatan
Control



Private Sub ClientCMD_Click()
IPtxt.Text = “IP server” ‘Menerangkan Textbox untuk menulis Nomor IP Server
Me.Caption = “Connect as Client” ‘Judul Form
IPtxt.Enabled = True ‘Textbox IPtxt aktif
nicknameTXT.Enabled = True ‘Textbox nicknameTXT aktif
ServerCMD.Enabled = False ‘Tombol ServerCMD nonaktif
ClientCMD.Enabled = False ‘Tombol ClientCMD nonaktif
ConnectCMD.Enabled = True ‘Tombol ConnectCMD aktif
End Sub
Private Sub ConnectCMD_Click()
If nicknameTXT.Text = “” Then Exit Sub ‘Anda harus memasukan nama sebelum konek
If Me.Caption = “Connect as Server” Then ‘connects u to the client
ServerFRM.Winsock.Close ‘closes any previous connections
ServerFRM.Winsock.LocalPort = CLng(187) ‘Port yang digunakan 187
ServerFRM.Winsock.Listen ‘Merespon client yang akan Konek ke server
ServerFRM.nickSERVER.Caption = nicknameTXT.Text ‘put the username for the server in a lbl on serverfrm
End If
If Me.Caption = “Connect as Client” Then ‘connects u to the server
If IPtxt.Text = “Server’s IP here” Then Exit Sub ‘just incase u forgot to put IP in
If IPtxt.Text = “” Then Exit Sub ‘just incase u forgot to put IP in
ClientFRM.Winsock.Close ‘closes any previous connections
ClientFRM.Winsock.Connect IPtxt.Text, “187″ ‘sends ur IP to connect to server:187 is the port
ClientFRM.nickCLIENT.Caption = nicknameTXT.Text ‘puts username for client in a lbl on clientfrm
End If
End Sub
Private Sub Form_Load()
ConnectCMD.Enabled = False ‘wont allow u to connect until u select necessary options
YouripLBL.Caption = “IP Anda:  ” & Winsock.LocalIP ‘shows u your IP address
IPtxt.Enabled = False ‘disabled till select server or client
nicknameTXT.Enabled = False ‘disabled till select server or client
End Sub
Private Sub ServerCMD_Click()
Me.Caption = “Connect as Server” ‘recaptions form to fit user
IPtxt.Enabled = True ‘needed button to connect
nicknameTXT.Enabled = True ‘needed button to connect
ServerCMD.Enabled = False ‘unneeded button to connect to server
ClientCMD.Enabled = False ‘unneeded button to connect to server
IPtxt.Enabled = False ‘unneeded txtbox to server
ConnectCMD.Enabled = True ‘needed button to connnect
End Sub

Client


‘===========================================================================
‘======================Displays info if disconnected========================
Private Sub Winsock_Close()
MainTXT.SelText = ” Tidak Konek ke Client” & vbCrLf ‘displays text if u get disconnected from the server
senddataCMD.Enabled = False ‘this wont let you send text anymore since server is disconnected
End Sub
‘===========================================================================
‘=======================RETREIVES DATA FROM SERVER==========================
Private Sub winsock_DataArrival(ByVal bytesTotal As Long)
Dim strData, strData2 As String ‘where the data sent by the client will be stored
Call Winsock.GetData(strData, vbString) ‘gets the data sent by the client
strData2 = Left(strData, 1) ‘saves the first variable’s value to strData2
strData = Mid(strData, 2) ‘saves the text the server sent to strData
If strData2 = “C” Then ‘tells form to load and do other form stuff
nickSERVER.Caption = strData ‘loads the server’s username from data sent
Me.Show ‘shows Client frm
Unload ConnectFRM ‘unloads the connect frm
Winsock.SendData “N” & nickCLIENT.Caption ‘sends your username to the server
ClientFRM.Caption = “Chating [Welcome, " & nickCLIENT.Caption & "!]” ‘renames the form approiately
MainTXT.SelText = “Konek ke Client” & vbCrLf ‘displays that connection worked
End If
If strData2 = “T” Then MainTXT.SelText = nickSERVER.Caption & “:     ” & strData & vbCrLf ‘adds the data to the txtbox from the server
End Sub
‘===========================================================================
‘========================SENDS DATA TYPED TO SERVER=========================
Private Sub senddataCMD_Click()
MainTXT.SelText = nickCLIENT.Caption & “:     ” & dataTXT.Text & vbCrLf ‘puts what u typed in ur maintxt
Winsock.SendData “T” & dataTXT.Text ‘sends the data to the server
dataTXT.Text = “” ‘clears the txtbox u typed in
End Sub
‘===========================================================================
Server

‘======================Displays info if disconnected========================
Private Sub Winsock_Close()
MainTXT.SelText = ” Tidak Konek ke Client ” & vbCrLf ‘displays text if u get disconnected from the client”
senddataCMD.Enabled = False ‘this wont let you send text anymore since client is disconnected
End Sub
‘======================ALLOWS CLIENT TO CONNECT=============================
Private Sub winsock_ConnectionRequest(ByVal requestID As Long)
If Winsock.State <> sckClosed Then Winsock.Close ‘closes connection if one is already open
Winsock.Accept requestID ‘allows new connection
Me.Show ‘shows server frm when WINSOCK connects to the client
Unload ConnectFRM ‘unloads connect frm when WINSOCK connects to the client
Winsock.SendData “C” & nickSERVER.Caption  ‘send data to server telling to load serverfrm
ServerFRM.Caption = “Chating  [Welcome, " & nickSERVER.Caption & "!]” ‘renames the form approiately
MainTXT.SelText = ” Konek ke Client ” & vbCrLf ‘displays that connection worked”
End Sub
‘===========================================================================
‘=======================RETREIVES DATA FROM CLIENT==========================
Private Sub winsock_DataArrival(ByVal bytesTotal As Long)
Dim strData, strData2 As String ‘where the data sent by the client will be stored
Call Winsock.GetData(strData, vbString) ‘gets the data sent by the client
strData2 = Left(strData, 1) ‘saves the first variable’s value to strData2
strData = Mid(strData, 2) ‘saves the text the server sent to strData
If strData2 = “T” Then MainTXT.SelText = nickCLIENT.Caption & “:     ” & strData & vbCrLf  ‘adds the data to the txtbox
If strData2 = “N” Then nickCLIENT.Caption = strData ‘loads the client’s username from data sent
End Sub
‘===========================================================================
‘========================SENDS DATA TYPED TO CLIENT=========================
Private Sub senddataCMD_Click()
MainTXT.SelText = nickSERVER.Caption & “:     ” & dataTXT.Text & vbCrLf ‘puts what u typed in ur maintxt
Winsock.SendData “T” & dataTXT.Text ‘sends the data to the client
dataTXT.Text = “” ‘clears the txtbox u typed in
End Sub
‘===========================================================================
Unique Code : Y7QC7Q22VFREijangculture.blogspot.com

Baca Juga Artikel Terkait



No comments:

Post a Comment