You are on page 1of 3

Winsock bagian 1 Intro 1. Buat project baru 2. Tambahkan komponen (CTRL + T) Microsoft Winsock Control 6.

.0 kemudian drag ke form 3. Drag komponen CommandButton dan set properties Name = cmdTesKoneksi (khusus program klien) Source code lengkap program pertama (server) : Private Sub Form_Load() With Winsock1 'set lokal port, misal 21211 'jangan menggunakan port standar yang sudah digunakan 'ex : 3306 (MySQL), 3050 (Firebird) de el el .LocalPort = 21211

1 2 3 4 5 6 7 'listen on this port 8 .Listen End With 9 10 End Sub 11 12 Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) With Winsock1 13 'tutup terlebih dulu, karena jika sebelumnya masih ada 14 15 koneksi 'akan menyebabkan error 16 .Close 17 18 'requestID mengandung informasi angka unik setiap koneksi 19 20 yang masuk .Accept requestID 21 End With 22 End Sub Source code lengkap program kedua (klien) : 1 Private Sub cmdTesKoneksi_Click() With Winsock1 2 If .State <> sckClosed Then .Close 3 4 'ip komputer target - server 5 .RemoteHost = "127.0.0.1" 6 7 'harus sama dengan port pada saat pemanggilan method 8 .Listen program server 9 .RemotePort = 21211 10 11 'start the connection 12 .Connect 13 End With 14 15 End Sub

Private Sub Winsock1_Connect() MsgBox "Connected" 16 17 End Sub 18 19 Private Sub Winsock1_Error(ByVal Number As Integer, Description 20 As String, ByVal Scode As Long, ByVal Source As String, ByVal 21 HelpFile As String, ByVal HelpContext As Long, CancelDisplay As 22 Boolean) MsgBox "Error: " & Description End Sub Winsock bagian 2 Membuat aplikasi chat sederhana 1:1 Adapun persiapan untuk aplikasi server/klien : 1. 2. 3. 4. Objek Winsock Label 2 TextBox CommandButton

Source code lengkap aplikasi server : 1 Option Explicit 2 3 Private Sub cmdSend_Click() 4 Winsock1.SendData txtChat.Text 5 DoEvents 6 7 txtMain.Text = txtMain.Text & vbCrLf & txtChat.Text 8 txtChat.Text = "" 9 End Sub 10 11 Private Sub Form_Load() Winsock1.LocalPort = 11111 12 Winsock1.Listen 13 14 End Sub 15 16 Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long) 'reset the socket, and accept the new connection 17 Winsock1.Close 18 Winsock1.Accept requestID 19 End Sub 20 21 22 Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim strData As String 23 24 'get the data and display it in the textbox 25 Winsock1.GetData strData 26 txtMain.Text = txtMain.Text & vbCrLf & strData 27 txtMain.SelStart = Len(txtMain.Text) 28 29 End Sub

Aplikasi server tinggal duduk maniz dan menunggu koneksi yang masuk. Source code lengkap aplikasi klien : Option Explicit 1 Private Sub Form_Load() 2 'set up the Winsock1 to connect to the local computer 3 Winsock1.RemoteHost = "127.0.0.1" 4 Winsock1.RemotePort = 11111 5 Winsock1.Connect 6 End Sub 7 8 Private Sub cmdSend_Click() 9 'send the data thats in the text box and 10 'clear it to prepare for the next chat message 11 Winsock1.SendData txtChat.Text 12 DoEvents 13 14 txtMain.Text = txtMain.Text & vbCrLf & txtChat.Text 15 txtChat.Text = "" 16 End Sub 17 18 Private Sub Winsock1_Connect() 19 'we are connected! 20 MsgBox "Connected" 21 End Sub 22 23 Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) 24 Dim strData As String 25 26 ' get the data from the socket 27 Winsock1.GetData strData 28 ' display it in the textbox 29 txtMain.Text = txtMain.Text & vbCrLf & strData 30 ' scroll the box down 31 txtMain.SelStart = Len(txtMain.Text) 32 End Sub 33 34 Private Sub Winsock1_Error(ByVal Number As Integer, Description 35 As String, ByVal Scode As Long, ByVal Source As String, ByVal 36 HelpFile As String, ByVal HelpContext As Long, CancelDisplay As 37 Boolean) 38 ' an error has occured somewhere, so let the user know 39 MsgBox "Error: " & Description 40 ' close the socket, ready to go again 41 Winsock1.Close End Sub

You might also like