Membuat design seperti di bawah ini :
Di sini saya menggunakan jaringan localhost, karena dipraktekkan dalam 1 pc. Tapi juga dapat dipraktekkan di pc yang berbeda. Asalkan bisa ping sesama komputer.
Tambahkan kontrol winsock pada masing-masing form dengan nama "ws".
Nb : Untuk yang belum terinstall winsock vb6 nya, silahkan baca tutorial saya sebelumnya.
Tambahkan 1 buah MdiForm
Program Untuk client :
Public Class frmClient
Dim statusWs1 As Boolean = False
Private Sub btnKoneksi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKoneksi.Click
ws.Close()
ws.Connect(txtIp.Text, txtPort.Text)
End Sub
Private Sub ws_CloseEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles ws.CloseEvent
statusWs1 = False
End Sub
Private Sub ws_ConnectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ws.ConnectEvent
MsgBox("Terkoneksi dengan server!", MsgBoxStyle.Information)
statusWs1 = True
End Sub
Private Sub ws_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles ws.DataArrival
Dim data As String = ""
ws.GetData(data, vbString, e.bytesTotal)
txtMasuk.Text = txtMasuk.Text & vbCrLf & data
End Sub
Private Sub ws_Error(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles ws.Error
MsgBox("Tidak dapat terkoneksi", MsgBoxStyle.Critical)
statusWs1 = False
End Sub
Private Sub btnKirim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKirim.Click
If statusWs1 Then
ws.SendData(txtPesan.Text)
Else
MsgBox("Maaf belum terkoneksi")
End If
End Sub
End Class
Dim statusWs1 As Boolean = False
Private Sub btnKoneksi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKoneksi.Click
ws.Close()
ws.Connect(txtIp.Text, txtPort.Text)
End Sub
Private Sub ws_CloseEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles ws.CloseEvent
statusWs1 = False
End Sub
Private Sub ws_ConnectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ws.ConnectEvent
MsgBox("Terkoneksi dengan server!", MsgBoxStyle.Information)
statusWs1 = True
End Sub
Private Sub ws_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles ws.DataArrival
Dim data As String = ""
ws.GetData(data, vbString, e.bytesTotal)
txtMasuk.Text = txtMasuk.Text & vbCrLf & data
End Sub
Private Sub ws_Error(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles ws.Error
MsgBox("Tidak dapat terkoneksi", MsgBoxStyle.Critical)
statusWs1 = False
End Sub
Private Sub btnKirim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKirim.Click
If statusWs1 Then
ws.SendData(txtPesan.Text)
Else
MsgBox("Maaf belum terkoneksi")
End If
End Sub
End Class
Program untuk server :
Public Class frmServer
Dim statusWs1 As Boolean = False
Private Sub frmServer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
MsgBox(statusWs1)
End Sub
Private Sub frmServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ws.Close()
ws.LocalPort = txtPort.Text
ws.Listen()
End Sub
Private Sub ws_CloseEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles ws.CloseEvent
statusWs1 = True
ws.Listen()
End Sub
Private Sub ws_ConnectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ws.ConnectEvent
MsgBox("Host : " & ws.RemoteHost & vbCrLf & "IP : " & ws.RemoteHostIP & vbCrLf & "Terkoneksi", MsgBoxStyle.Information)
statusWs1 = True
End Sub
Private Sub ws_ConnectionRequest(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles ws.ConnectionRequest
On Error GoTo lain
ws.Close()
Dim y As MsgBoxResult = MsgBox("Host : " & ws.RemoteHost & vbCrLf & "IP : " & ws.RemoteHostIP & vbCrLf & "Ingin Meminta Koneksi, Ijinkan?", vbYesNo)
If y = vbYes Then
statusWs1 = True
ws.Accept(e.requestID)
End If
Exit Sub
lain:
statusWs1 = False
End Sub
Private Sub btnKirim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKirim.Click
If statusWs1 = True Then
ws.SendData(txtPesan.Text)
Else
MsgBox("Maaf belum terkoneksi")
End If
End Sub
Private Sub ws_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles ws.DataArrival
Dim data As String = ""
ws.GetData(data, vbString, e.bytesTotal)
txtMasuk.Text = txtMasuk.Text & vbCrLf & data
End Sub
End Class
Dim statusWs1 As Boolean = False
Private Sub frmServer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
MsgBox(statusWs1)
End Sub
Private Sub frmServer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ws.Close()
ws.LocalPort = txtPort.Text
ws.Listen()
End Sub
Private Sub ws_CloseEvent(ByVal sender As Object, ByVal e As System.EventArgs) Handles ws.CloseEvent
statusWs1 = True
ws.Listen()
End Sub
Private Sub ws_ConnectEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ws.ConnectEvent
MsgBox("Host : " & ws.RemoteHost & vbCrLf & "IP : " & ws.RemoteHostIP & vbCrLf & "Terkoneksi", MsgBoxStyle.Information)
statusWs1 = True
End Sub
Private Sub ws_ConnectionRequest(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_ConnectionRequestEvent) Handles ws.ConnectionRequest
On Error GoTo lain
ws.Close()
Dim y As MsgBoxResult = MsgBox("Host : " & ws.RemoteHost & vbCrLf & "IP : " & ws.RemoteHostIP & vbCrLf & "Ingin Meminta Koneksi, Ijinkan?", vbYesNo)
If y = vbYes Then
statusWs1 = True
ws.Accept(e.requestID)
End If
Exit Sub
lain:
statusWs1 = False
End Sub
Private Sub btnKirim_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnKirim.Click
If statusWs1 = True Then
ws.SendData(txtPesan.Text)
Else
MsgBox("Maaf belum terkoneksi")
End If
End Sub
Private Sub ws_DataArrival(ByVal sender As Object, ByVal e As AxMSWinsockLib.DMSWinsockControlEvents_DataArrivalEvent) Handles ws.DataArrival
Dim data As String = ""
ws.GetData(data, vbString, e.bytesTotal)
txtMasuk.Text = txtMasuk.Text & vbCrLf & data
End Sub
End Class
Program untuk MdiForm :
Imports System.Windows.Forms
Public Class MDIParent1
Private Sub ServerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ServerToolStripMenuItem.Click
frmServer.MdiParent = Me
frmServer.Show()
End Sub
Private Sub ClientToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClientToolStripMenuItem.Click
frmClient.MdiParent = Me
frmClient.Show()
End Sub
Private Sub MDIParent1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Public Class MDIParent1
Private Sub ServerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ServerToolStripMenuItem.Click
frmServer.MdiParent = Me
frmServer.Show()
End Sub
Private Sub ClientToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClientToolStripMenuItem.Click
frmClient.MdiParent = Me
frmClient.Show()
End Sub
Private Sub MDIParent1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Hasil Program :
Post a Comment