-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.vb
More file actions
94 lines (70 loc) · 3.53 KB
/
Source.vb
File metadata and controls
94 lines (70 loc) · 3.53 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
Imports System
Imports System.IO
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports Microsoft.VisualBasic
Module Module1
Class MyTcpListener
Public Shared Sub Main()
Console.Title = "Leprechaun's DummyServer"
Dim logfile As String
Try
' Set the TcpListener port
Dim sr As New StreamReader("porta.txt")
Dim line As String
line = sr.ReadToEnd()
Dim port As String = line
Dim server As New TcpListener(IPAddress.Any, port)
' Start listening for client requests.
server.Start()
' Buffer for reading data
Dim bytes(1024) As [Byte]
Dim data As [String] = Nothing
' Enter the listening loop.
While True
logfile = Now.Day & "-" & Now.Month & "-" & Now.Year & ".txt"
Console.Write("Waiting for a connection... ")
' Perform a blocking call to accept requests.
' You could also user server.AcceptSocket() here.
Dim client As TcpClient = server.AcceptTcpClient()
Dim ClientRemoteIP As String = client.Client.RemoteEndPoint.ToString.Remove(client.Client.RemoteEndPoint.ToString.IndexOf(":"))
Console.WriteLine(Environment.NewLine)
Console.WriteLine("-------------------------------------------------------")
Console.WriteLine("Connected IP: " + ClientRemoteIP)
Using writer As StreamWriter = New StreamWriter(logfile, True)
writer.WriteLine("---------------------------------------" + vbCrLf + vbCrLf + DateTime.Now + " - Connected IP: " + ClientRemoteIP + " - Port: " + port)
End Using
Console.Beep()
data = Nothing
' Get a stream object for reading and writing
Dim stream As NetworkStream = client.GetStream()
Dim i As Int32
' Loop to receive all the data sent by the client.
i = stream.Read(bytes, 0, bytes.Length)
While (i <> 0)
' Translate data bytes to a ASCII string.
data = System.Text.Encoding.ASCII.GetString(bytes, 0, i)
Console.WriteLine([String].Format("Received: {0}", data))
' Process the data sent by the client.
data = data.ToUpper()
Dim msg As [Byte]() = System.Text.Encoding.ASCII.GetBytes(data)
' Send back a response.
stream.Write(msg, 0, msg.Length)
Console.WriteLine([String].Format("Sent: {0}", data))
Using writer As StreamWriter = New StreamWriter(logfile, True)
writer.WriteLine(data + vbCrLf + vbCrLf)
End Using
i = stream.Read(bytes, 0, bytes.Length)
End While
' Shutdown and end connection
client.Close()
End While
Catch e As SocketException
Console.WriteLine("SocketException: {0}", e)
End Try
Console.WriteLine(ControlChars.Cr + "Hit enter to continue...")
Console.Read()
End Sub 'Main
End Class 'MyTcpListener
End Module