-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcp_server.vb
39 lines (36 loc) · 1.39 KB
/
tcp_server.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Module MainModule
Dim _server As TcpListener
Sub Main()
Try
_server = New TcpListener(IPAddress.Parse("127.0.0.1"), 8080)
_server.Start()
Threading.ThreadPool.QueueUserWorkItem(AddressOf NewClient)
Catch ex As Exception
Console.WriteLine(ex)
End Try
Console.ReadLine()
End Sub
Private Sub NewClient(state As Object)
Try
Using client As TcpClient = _server.AcceptTcpClient()
Threading.ThreadPool.QueueUserWorkItem(AddressOf NewClient)
Using networkStream As NetworkStream = client.GetStream()
While True
Dim toReceive(100000) As Byte
Dim length As Integer = networkStream.Read(toReceive, 0, toReceive.Length)
Dim text As String = Encoding.ASCII.GetString(toReceive, 0, length)
Console.WriteLine(text)
Console.WriteLine()
Dim toSend() As Byte = Encoding.ASCII.GetBytes("Message Received...")
networkStream.Write(toSend, 0, toSend.Length)
End While
End Using
End Using
Catch ex As Exception
Console.WriteLine(ex)
End Try
End Sub
End Module