Sunteți pe pagina 1din 11

Dasar Network Programming

Network
DAFTAR ISI
• SOCKET pada VB NET
Pemrograman SOCKET pada vb net

server
client
Visual Basic networking applications using sockets

Network komunikasi pada Visual Basic menggunakan


socket yg membuat developer melihat networking
sebagaimana file I/O.

Program membaca/menulis socket seperti


membaca/menulis file
NET Framework socket
Namespace

System.Net.Sockets

class

TcpListener

class

TcpClient
5 step to establish TCP server with VB.net

The first step


create an object of class TcpListener
TcpListener server = New TcpListener( port )

The second step in


our connection process is to call Start method,
which causes the TcpListener object to begin listening for connection requests.

The third step


establishes the connection between the server and client.

Step four
processing phase, in which the server and the client communicate via
methods Receive and Send of class Socket.

The fifth step


connection-termination phase
uses method Close of the Socket object to closethe connection.
TCP/IP Server VB NET
Imports System.Net.Sockets
Imports System.Text

Module Module1
'---port number to use for listening---
Const portNo As Integer = 5000
Sub Main()
Dim localAdd As System.Net.IPAddress = System.Net.IPAddress.Parse("127.0.0.1")
'---listen at the local address---
Dim listener As New TcpListener(localAdd, portNo)
listener.Start()
'---Accepts connection ---
Dim tcpClient As TcpClient = listener.AcceptTcpClient()

'--receive data---
Dim ns As NetworkStream = tcpClient.GetStream
Dim data(tcpClient.ReceiveBufferSize) As Byte
'---read incoming stream; Read()
Dim numBytesRead As Integer = ns.Read(data, 0, CInt(tcpClient.ReceiveBufferSize))

'---display data received---


Console.WriteLine("Received :" & Encoding.ASCII.GetString(data, 0, numBytesRead))

End Sub
End Module
5 step to establish TCP Client with VB.net

first step,
create an object of class TcpClient
TcpClient client = New TcpClient()
client.Connect( serverAddress, serverPort )

step two,
the TcpClient uses its method GetStream to get a Network-
Stream so that it can write to and read from the server.

third step
the processing phase, in which the client and the server communicate.
In this phase, the client uses methods Read, ReadByte, Write and WriteByte of
class NetworkStream

step four
client to close the connection
by calling method Close of the NetworkStream object.
TCP/IP Client VB Net

Visual Basic 2005


Imports System.Net.Sockets
Module Module1

Sub Main()
Dim tcpclient As New TcpClient
'---connect ke server---
tcpclient.Connect("127.0.0.1", 5000)
'---menggunakan obj NetworkStream untuk send and receive data
Dim ns As NetworkStream = tcpclient.GetStream

Dim data As Byte() = Encoding.ASCII.GetBytes("Hello")

'---send the text---


ns.Write(data, 0, data.Length)
End Sub

End Module
Writing a simple UDP client

imports System.Net
imports System.Net.Sockets
imports System.Text
imports System.IO

Private sub button1_Click()

Dim udpClient as new UdpClient()

udpClient.Connect(tbHost.Text, 8080)

Dim sendBytes as Byte()

sendBytes = Encoding.ASCII.GetBytes("Hello World?")

udpClient.Send(sendBytes, sendBytes.Length)

End sub
Writing a simple UDP server
imports System.Threading
imports System.Net
imports System.Net.Sockets
imports System.Text

Private Sub Form1_Load()


Dim thdUDPServer = new Thread(new ThreadStart(AddressOf serverThread))
thdUDPServer.Start()
End Sub

Public Sub serverThread()


Dim udpClient as new UdpClient(8080)
While true
Dim RemoteIpEndPoint as new IPEndPoint(IPAddress.Any, 0)
Dim receiveBytes as Byte()
receiveBytes = udpClient.Receive(RemoteIpEndPoint)
Dim returnData As String = Encoding.ASCII.GetString(receiveBytes)
lbConnections.Items.Add RemoteIpEndPoint.Address.ToString() + ":" + returnData.ToString()
Wend
End Sub

S-ar putea să vă placă și