Popular Posts

Jumat, Agustus 07, 2009

Program Chatting Dengan Visual Basic dan Winsock

Jika pada artikel sebelumnya kita membahas mengenai program chating dengan menggunakan fasilitas dari Net Send, maka kali ini kita akan membahas mengenai program chating dengan menggunakan Visual Basic + Winsock. Okee deh...langsung aja kita lakukan persiapannya...
yang pertama siap kan seperangkat alat sholat..(lho kok..!!!)
kita memerlukan alat sholat, agar meskipun kita sedang mengerjakan program, kita tidak melupakan sholat.

oke langsung saja kita buka visual studio kita tercinta..
Langkah pertama kita harus mengaktifkan komponen winsock, caranya tekan tombol alternatif Ctrl+t. maka akan muncul jendela Component, lalu pada tab Control, centang pada Microsoft Winsock Control 6.0.
- Buat dua buah form. yang satu sebagai client dan yang satu sebagai server
- Buat tampilan form untuk client seperti gambar

pada form client isikan listing berikut
Private Sub cmdkirim_Click()
Winsock1.SendData Winsock1.LocalHostName & " : " & txtkirim.Text
txtData = txtData & Winsock1.LocalHostName & " : " & txtkirim.Text & vbCrLf
txtkirim.Text = ""
txtkirim.SetFocus
End Sub

Private Sub cmdKoneksi_Click()
On Error GoTo a
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.RemoteHost = txtIp
Winsock1.RemotePort = txtPort
Winsock1.Connect

Exit Sub
a:
MsgBox "Error : " & Err.Description, vbCritical, "Error"

End Sub

Private Sub txtkirim_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then cmdkirim_Click
End Sub

Private Sub Winsock1_Connect()
txtData.Text = "Konek ke : " & Winsock1.RemoteHostIP & vbCrLf
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Accept requestID
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim data As String
Winsock1.GetData data
txtData.Text = txtData.Text & data & vbCrLf
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)
txtData.Text = txtData.Text & Err.Description & vbCrLf
Winsock1.Close
End Sub

- Pada form server buat tampilan seperti gambar

- Pada form Server isi listing berikut
Private Sub cmdbukaport_Click()
On Error GoTo a
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.LocalPort = txtPort
Winsock1.Listen
txtData = "Buka Port : " & txtPort
Exit Sub
a:
MsgBox Err.Description
End Sub

Private Sub cmdKirim_Click()
Winsock1.SendData Winsock1.LocalHostName & " : " & txtkirim.Text
txtData = txtData & Winsock1.LocalHostName & " : " & txtkirim.Text & vbCrLf
txtkirim.Text = ""
txtkirim.SetFocus
End Sub

Private Sub txtkirim_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then cmdKirim_Click
End Sub

Private Sub Winsock1_Close()
txtData = Winsock1.RemoteHostIP & " : " & " : Menutup Koneksi"
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
txtData = "Menerima Koneksi dari : " & Winsock1.RemoteHostIP & vbCrLf
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim data As String
Winsock1.GetData data
txtData.Text = txtData.Text & data & vbCrLf
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)
txtData.Text = txtData.Text & Err.Description & vbCrLf
Winsock1.Close
End Sub

Download Project Lengkapnya>> Program Chating Visual Basic + Winsock.rar

Comments :

0 komentar to “Program Chatting Dengan Visual Basic dan Winsock”


Posting Komentar