Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim serverSocket As New TcpListener(1024)
Dim clientSocket As TcpClient
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Minimized
Me.Hide()
Me.ShowInTaskbar = False
End Sub
Sub Main()
start: 'start socket
serverSocket.Start()
Label1.Text = "Server Started"
' Application.DoEvents()
Try
clientSocket = serverSocket.AcceptTcpClient()
Label2.Text = "Accept connection from client"
'Application.DoEvents()
Catch ex As Exception
Label2.Text = "connection from client fail !!"
'Application.DoEvents()
End Try
clientSocket = serverSocket.AcceptTcpClient()
Label2.Text = "Accept connection from client"
Application.DoEvents()
While (True)
Try
Dim networkStream As NetworkStream = clientSocket.GetStream()
Dim bytesFrom(10024) As Byte
networkStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
Dim dataFromClient As String = System.Text.Encoding.ASCII.GetString(bytesFrom)
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf(";"))
MsgBox(dataFromClient)
Dim serverResponse As String = "0"
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(serverResponse)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Flush()
Catch ex As Exception
Label2.Text = "connection from client fail !!"
Application.DoEvents()
clientSocket.Close()
serverSocket.Stop()
msg("exit")
Console.ReadLine()
GoTo start
End Try
End While
End Sub
Sub msg(ByVal mesg As String)
mesg.Trim()
Console.WriteLine(" >> " + mesg)
End Sub
Private Sub btnsta_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsta.Click
Main()
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
Me.Show()
Me.WindowState = FormWindowState.Normal
End Sub
Private Sub btnstp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnstp.Click
clientSocket.Close()
serverSocket.Stop()
msg("exit")
Console.ReadLine()
End Sub
Private Sub StartServerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartServerToolStripMenuItem.Click
Main()
End Sub
Private Sub StopServerToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopServerToolStripMenuItem.Click
clientSocket.Close()
serverSocket.Stop()
msg("exit")
Console.ReadLine()
End Sub
End Class
Code Client(VB.NET)
Imports System.Net.Sockets
Imports System.Text
Public Class Form1
Dim clientSocket As New System.Net.Sockets.TcpClient()
Dim serverStream As NetworkStream
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsend.Click
Dim serverStream As NetworkStream = clientSocket.GetStream()
Dim outStream As Byte() = System.Text.Encoding.ASCII.GetBytes(txtdata.Text & ";")
serverStream.Write(outStream, 0, outStream.Length)
serverStream.Flush()
Dim inStream(10024) As Byte
serverStream.Read(inStream, 0, CInt(clientSocket.ReceiveBufferSize))
Dim returndata As String = System.Text.Encoding.ASCII.GetString(inStream)
MsgBox(returndata)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub btnconn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconn.Click
Try
With clientSocket
If .Connected = True Then .Close()
End With
clientSocket.Connect(txtserver.Text, txtport.Text)
Label1.Text = "Client Socket Program - Server Connected Success ..."
Catch ex As Exception
Label1.Text = "Client Socket Program - Server Connected Fail !!! ..."
End Try
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
clientSocket.Close()
End Sub
End Class
Tag : .NET, Win (Windows App), VB.NET, VS 2005 (.NET 2.x)