-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.vb
252 lines (202 loc) · 7.3 KB
/
Form1.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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Threading
Imports System.Text
Public Class Form1
Private client As AsynchronousClient = Nothing
Private address As IPAddress = Nothing
Private Sub Log(message As String)
recievedDataList.Text += message
recievedDataList.SelectionStart = recievedDataList.TextLength
recievedDataList.ScrollToCaret()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
disconnect.Enabled = False
sendData.Enabled = False
data.Enabled = False
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles connect.Click
If (ipAddress.Text IsNot "") Then
If (System.Net.IPAddress.TryParse(ipAddress.Text, address)) Then
If (IsNothing(client)) Then
client = New AsynchronousClient(SocketType.Stream, ProtocolType.Tcp)
End If
client.Connect(address, port.Value)
AddHandler client.ReceivedData, AddressOf onRecievedData
AddHandler client.Loged, AddressOf onLogEvent
AddHandler client.Connected, AddressOf onConnect
AddHandler client.Disconnected, AddressOf onDisconnect
End If
End If
End Sub
Private Sub onDisconnect()
Me.BeginInvoke(
Sub()
disconnect.Enabled = False
connect.Enabled = True
sendData.Enabled = False
data.Enabled = False
client.Close()
client = Nothing
End Sub
)
End Sub
Private Function onConnect() As Object
Me.BeginInvoke(
Sub()
disconnect.Enabled = True
connect.Enabled = False
sendData.Enabled = True
data.Enabled = True
End Sub
)
End Function
Private Sub onLogEvent(message As String)
Me.BeginInvoke(Sub() Log(message))
End Sub
Private Sub onRecievedData(data As String)
Me.BeginInvoke(Sub() Log(data))
End Sub
Private Sub SendData_Click(sender As Object, e As EventArgs) Handles sendData.Click
Dim dataInt As Int32 = data.Value
client.Send(dataInt.ToString())
End Sub
Private Sub disconnect_Click(sender As Object, e As EventArgs) Handles disconnect.Click
client.Disconnect()
End Sub
Private Sub AboutUsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AboutUsToolStripMenuItem.Click
Process.Start("https://github.com/sampad1370/SocketInVB")
End Sub
End Class
' State object for receiving data from remote device.
Public Class StateObject
' Client socket.
Public workSocket As Socket = Nothing
' Size of receive buffer.
Public Const BufferSize As Integer = 256
' Receive buffer.
Public buffer() As Byte = New Byte((BufferSize) - 1) {}
' Received data string.
Public sb As StringBuilder = New StringBuilder
End Class
Public Class AsynchronousClient
' ManualResetEvent instances signal completion.
Public connectDone As ManualResetEvent = New ManualResetEvent(False)
Public disconnectDone As ManualResetEvent = New ManualResetEvent(False)
Public sendDone As ManualResetEvent = New ManualResetEvent(False)
Public receiveDone As ManualResetEvent = New ManualResetEvent(False)
' The response from the remote device.
Public response As String = String.Empty
Private client As Socket = Nothing
Public Event ReceivedData(data As String)
Public Event Loged(message As String)
Public Event Connected()
Public Event Disconnected()
Public Sub New(socketType As SocketType, protocolType As ProtocolType)
client = New Socket(socketType, protocolType)
client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, True)
End Sub
Private Sub ConnectCallback(ByVal ar As IAsyncResult)
Try
' Retrieve the socket from the state object.
Dim client As Socket = CType(ar.AsyncState, Socket)
' Complete the connection.
client.EndConnect(ar)
RaiseEvent Loged("Socket connected to " + client.RemoteEndPoint.ToString() + vbCrLf)
' Signal that the connection has been made.
connectDone.Set()
RaiseEvent Connected()
Receive(client)
Catch e As Exception
RaiseEvent Loged(e.ToString)
End Try
End Sub
Private Sub DisconnectCallback(ar As IAsyncResult)
Try
' Retrieve the socket from the state object.
Dim client As Socket = CType(ar.AsyncState, Socket)
client.EndDisconnect(ar)
disconnectDone.[Set]()
RaiseEvent Loged("Socket disconnected to " + client.RemoteEndPoint.ToString() + vbCrLf)
RaiseEvent Disconnected()
' Signal that the connection has been made.
Catch e As Exception
RaiseEvent Loged(e.ToString)
End Try
End Sub
Public Sub Connect(ipAddress As IPAddress, port As Int32)
client.BeginConnect(ipAddress, port, New AsyncCallback(AddressOf ConnectCallback), client)
End Sub
Public Sub Disconnect()
client.Shutdown(SocketShutdown.Both)
client.BeginDisconnect(True, New AsyncCallback(AddressOf DisconnectCallback), client)
End Sub
Private Sub Receive(ByVal client As Socket)
Try
' Create the state object.
Dim state As StateObject = New StateObject
state.workSocket = client
' Begin receiving the data from the remote device.
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
Catch e As Exception
RaiseEvent Loged(e.ToString)
End Try
End Sub
Private Sub ReceiveCallback(ByVal ar As IAsyncResult)
Try
' Retrieve the state object and the client socket
' from the asynchronous state object.
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim client As Socket = state.workSocket
' Read data from the remote device.
Dim bytesRead As Integer = client.EndReceive(ar)
If (bytesRead > 0) Then
' There might be more data, so store the data received so far.
Dim str As String = Encoding.ASCII.GetString(state.buffer, 0, bytesRead)
state.sb.Append(str)
' Get the rest of the data.
If (str.Length > 1) Then
RaiseEvent ReceivedData(str)
End If
' Signal that all bytes have been received.
receiveDone.Set()
client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReceiveCallback), state)
Else
' All the data has arrived; put it in response.
' Must change strategye to best detect packet
End If
Catch e As Exception
RaiseEvent Loged(e.ToString)
End Try
End Sub
Private Sub Send(ByVal client As Socket, ByVal data As String)
' Convert the string data to byte data using ASCII encoding.
Dim byteData() As Byte = Encoding.ASCII.GetBytes(data)
' Begin sending the data to the remote device.
client.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf SendCallback), client)
End Sub
Private Sub SendCallback(ByVal ar As IAsyncResult)
Try
' Retrieve the socket from the state object.
Dim client As Socket = CType(ar.AsyncState, Socket)
' Complete sending the data to the remote device.
Dim bytesSent As Integer = client.EndSend(ar)
RaiseEvent Loged("Sent " + bytesSent.ToString() + " bytes to server.")
' Signal that all bytes have been sent.
sendDone.Set()
Catch e As Exception
Console.WriteLine(e.ToString)
End Try
End Sub
Public Sub Send(ByVal data As String)
' Convert the string data to byte data using ASCII encoding.
Dim byteData() As Byte = Encoding.ASCII.GetBytes(data & vbLf)
' Begin sending the data to the remote device.
'client.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf SendCallback), client)
client.Send(byteData)
End Sub
Public Sub Close()
client.Close()
End Sub
End Class