01.
Imports
System.Net.Sockets
02.
Imports
System.Text
03.
04.
Public
Class
Form1
05.
Dim
serverSocket
As
New
TcpListener(1024)
06.
Dim
clientSocket
As
TcpClient
07.
08.
Private
Sub
Form1_Load(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
MyBase
.Load
09.
Me
.WindowState = FormWindowState.Minimized
10.
Me
.Hide()
11.
Me
.ShowInTaskbar =
False
12.
13.
End
Sub
14.
15.
Sub
Main()
16.
start:
17.
serverSocket.Start()
18.
Label1.Text =
"Server Started"
19.
20.
21.
Try
22.
clientSocket = serverSocket.AcceptTcpClient()
23.
Label2.Text =
"Accept connection from client"
24.
25.
Catch
ex
As
Exception
26.
Label2.Text =
"connection from client fail !!"
27.
28.
End
Try
29.
30.
clientSocket = serverSocket.AcceptTcpClient()
31.
Label2.Text =
"Accept connection from client"
32.
Application.DoEvents()
33.
34.
While
(
True
)
35.
36.
Try
37.
Dim
networkStream
As
NetworkStream = clientSocket.GetStream()
38.
Dim
bytesFrom(10024)
As
Byte
39.
networkStream.Read(bytesFrom, 0,
CInt
(clientSocket.ReceiveBufferSize))
40.
Dim
dataFromClient
As
String
= System.Text.Encoding.ASCII.GetString(bytesFrom)
41.
dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf(
";"
))
42.
MsgBox(dataFromClient)
43.
44.
45.
Dim
serverResponse
As
String
=
"0"
46.
Dim
sendBytes
As
[
Byte
]() = Encoding.ASCII.GetBytes(serverResponse)
47.
networkStream.Write(sendBytes, 0, sendBytes.Length)
48.
networkStream.Flush()
49.
50.
Catch
ex
As
Exception
51.
Label2.Text =
"connection from client fail !!"
52.
Application.DoEvents()
53.
54.
clientSocket.Close()
55.
serverSocket.
Stop
()
56.
msg(
"exit"
)
57.
Console.ReadLine()
58.
GoTo
start
59.
End
Try
60.
61.
End
While
62.
End
Sub
63.
64.
Sub
msg(
ByVal
mesg
As
String
)
65.
mesg.Trim()
66.
Console.WriteLine(
" >> "
+ mesg)
67.
End
Sub
68.
69.
Private
Sub
btnsta_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnsta.Click
70.
Main()
71.
End
Sub
72.
73.
Private
Sub
NotifyIcon1_MouseDoubleClick(
ByVal
sender
As
Object
,
ByVal
e
As
System.Windows.Forms.MouseEventArgs)
Handles
NotifyIcon1.MouseDoubleClick
74.
Me
.Show()
75.
Me
.WindowState = FormWindowState.Normal
76.
End
Sub
77.
78.
Private
Sub
btnstp_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
btnstp.Click
79.
clientSocket.Close()
80.
serverSocket.
Stop
()
81.
msg(
"exit"
)
82.
Console.ReadLine()
83.
End
Sub
84.
85.
Private
Sub
StartServerToolStripMenuItem_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
StartServerToolStripMenuItem.Click
86.
Main()
87.
End
Sub
88.
89.
Private
Sub
StopServerToolStripMenuItem_Click(
ByVal
sender
As
System.
Object
,
ByVal
e
As
System.EventArgs)
Handles
StopServerToolStripMenuItem.Click
90.
clientSocket.Close()
91.
serverSocket.
Stop
()
92.
msg(
"exit"
)
93.
Console.ReadLine()
94.
End
Sub
95.
End
Class