Membuat program Server
Buka Microsoft visual Basic 2010 Express
Klik New Project
Pilih Windows Form Application
Klik OK
Objek Kontrol
|
Properti
|
Perubahan
|
Form
|
Text
|
Server
|
TextBox
|
Name
|
TextBox1
|
MultiLine
|
True
| |
ScrollBars
|
Both
| |
TextBox
|
Name
|
TextBox2
|
Button
|
Name
|
BtnKirim
|
Text
|
Kirim Pesan
| |
Button
|
Name
|
BtnHapus
|
Text
|
Hapus Text
|
Source Kode
Imports System.IO, System.Net, System.Net.Sockets
Public Class Form1
Dim Listener As TcpListener
Dim Client As TcpClient
Dim ClientList As New List(Of ChatClient)
Dim sReader As StreamReader
Dim cClient As ChatClient
Sub xLoad() Handles Me.Load
Listener = New TcpListener(IPAddress.Any, 3818)
Listener.Start()
xUpdate("Server Aktif", False)
Listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listener)
End Sub
Sub AcceptClient(ByVal ar As IAsyncResult)
cClient = New ChatClient(Listener.EndAcceptTcpClient(ar))
AddHandler (cClient.MessageRecieved), AddressOf MessageRecieved
AddHandler (cClient.ClientExited), AddressOf ClientExited
ClientList.Add(cClient)
xUpdate("client Baru Bergabung", True)
Listener.BeginAcceptTcpClient(New AsyncCallback(AddressOf AcceptClient), Listener)
End Sub
Sub MessageRecieved(ByVal Str As String)
xUpdate(Str, True)
End Sub
Sub ClientExited(ByVal Client As ChatClient)
ClientList.Remove(Client)
xUpdate("Client Keluar", True)
End Sub
Delegate Sub _xUpdate(ByVal Str As String, ByVal Relay As Boolean)
Sub xUpdate(ByVal Str As String, ByVal Relay As Boolean)
On Error Resume Next
If InvokeRequired Then
Invoke(New _xUpdate(AddressOf xUpdate), Str, Relay)
Else
TextBox1.AppendText(Str & vbNewLine)
If Relay Then Send(Str)
End If
End Sub
Sub Send(ByVal Str As String)
For i As Integer = 0 To ClientList.Count - 1
Try
ClientList(i).Send(Str)
Catch
ClientList.RemoveAt(i)
End Try
Next
End Sub
Private Sub BtnKirim_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnKirim.Click
xUpdate("Server Berkata: " & TextBox2.Text, True)
TextBox2.Clear()
End Sub
Private Sub BtnHapus_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnHapus.Click
TextBox1.Clear()
End Sub
End Class
Tambahkan Class
Project => Add Class
Pilih Class
Klik Add
Masukkan Code di bawah ini ke Class:
Imports System.Net.Sockets, System.IO
Public Class ChatClient
Public Event MessageRecieved(ByVal Str As String)
Public Event ClientExited(ByVal Client As ChatClient)
Private sWriter As StreamWriter
Private Client As TcpClient
Sub New(ByVal xclient As TcpClient)
Client = xclient
client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
End Sub
Private Sub Read()
Try
RaiseEvent MessageRecieved(New StreamReader(Client.GetStream).ReadLine)
Client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf Read), Nothing)
Catch ex As Exception
RaiseEvent ClientExited(Me)
End Try
End Sub
Public Sub Send(ByVal Message As String)
sWriter = New StreamWriter(Client.GetStream)
sWriter.WriteLine(Message)
sWriter.Flush()
End Sub
End Class
Membuat program Client:
1. Buka Microsoft visual Basic 2010 Express
2. Klik New Project
3. Pilih Windows Form Application
4. Name : Ketik server
5. Klik OK
Masukkan objek kontrol-objek kontrol dan atur properti-porpertinya seperti pada tabel berikut:
Tambahkan ToolStrip
Klik Yang saya lingkari untuk membuat tombol Connect
klik untuk membuat tombol About
Klik untuk membuat jam (00:00:00)
klik untuk membuat tombol About
Klik untuk membuat jam (00:00:00)
Atur seperti tabel di bawah ini :
Properti
|
Perubahan
| |
Connect
|
Name
|
BtnConnect
|
DisplayStyle
|
Text
| |
Text
|
Connect
| |
About
|
Name
|
BtnAbout
|
DisplayStyle
|
Text
| |
Text
|
About
| |
00:00:00
|
Name
|
BtnJam
|
DisplayStyle
|
Text
| |
Text
|
00:00:00
|
Objek Kontrol
|
Properti
|
Perubahan
|
Form
|
Text
|
Client
|
TextBox
|
Name
|
TextBox1
|
MultiLine
|
True
| |
ScrollBars
|
Both
| |
TextBox
|
Name
|
TextBox2
|
GroupBox
|
Name
|
IPAddress
|
Text
|
IP Address
| |
GroupBox
|
Name
|
Port
|
Text
|
Port
| |
GroupBox
|
Name
|
Nama
|
Text
|
Masukkan Nama Kamu
| |
TextBox
|
Name
|
TxtIpAddress
|
Text
|
127.0.0.1
| |
TextBox
|
Name
|
TxtPort
|
Text
|
3818
| |
TextBox
|
Text
|
TxtNama
|
Button
|
Name
|
BtnHapus
|
Text
|
Hapus Text
| |
Button
|
Name
|
BtnKirim
|
Text
|
Kirim Pesan
| |
Timer
|
Enabled
|
True
|
Interval
|
1000
|
Tambahka form baru:
1. Project => Add Windows form
2. Pilih Windows Form
3. Klik Add
Source Kode
Imports System.IO, System.Net, System.Net.Sockets
Public Class Form1
Dim Client As TcpClient
Dim sWriter As StreamWriter
Sub xLoad() Handles Me.Load
Me.Text &= " " & TxtNama.Text
End Sub
Private Sub BtnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnConnect.Click
If BtnConnect.Text = "Connect" Then
Try
Client = New TcpClient(TxtIpAddress.Text, CInt(TxtPort.Text))
Client.GetStream.BeginRead(New Byte() {0}, 0, 0, New AsyncCallback(AddressOf Read), Nothing)
BtnConnect.Text = "Disconnect"
Catch
xUpdate("Tidak Dapat Connect Ke Server.")
End Try
Else
Client.Client.Close()
Client = Nothing
BtnConnect.Text = "Connect"
End If
End Sub
Sub Read(ByVal ar As IAsyncResult)
Try
xUpdate(New StreamReader(Client.GetStream).ReadLine)
Client.GetStream.BeginRead(New Byte() {0}, 0, 0, AddressOf Read, Nothing)
Catch
xUpdate("Kamu Sekarang Tidak Terhubung Ke Server.")
Exit Sub
End Try
End Sub
Private Sub Send(ByVal Str As String)
Try
sWriter = New StreamWriter(Client.GetStream)
sWriter.WriteLine(Str)
sWriter.Flush()
Catch
xUpdate("Server Tidak Ada.")
End Try
End Sub
Delegate Sub _xUpdate(ByVal Str As String)
Sub xUpdate(ByVal Str As String)
If InvokeRequired Then
Invoke(New _xUpdate(AddressOf xUpdate), Str)
Else
TextBox1.AppendText(Str & vbNewLine)
End If
End Sub
Private Sub BtnKirim_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnKirim.Click
Send(TxtNama.Text & " Berkata: " & TextBox2.Text)
TextBox2.Clear()
End Sub
Private Sub BtnHapus_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnHapus.Click
TextBox1.Clear()
End Sub
Private Sub BtnAbout_Click(ByVal sender As Object, ByVal e As EventArgs) Handles BtnAbout.Click
Form2.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Btnjam.Text = DateTime.Now()
End Sub
End Class
0 komentar:
Posting Komentar