Imports System
Imports System.Net
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Imports Microsoft.VisualBasic
Public Class Form1
Public Shared allDone As New ManualResetEvent(False) ' Thread signal.
Public Class StateObject
Public workSocket As Socket = Nothing ' Client socket.
Public Const BufferSize As Integer = 1024 ' Size of receive buffer.
Public buffer(BufferSize) As Byte ' Receive buffer.
Public sb As New StringBuilder ' Received data string.
End Class 'StateObject
Public Shared Sub AcceptCallback(ByVal ar As IAsyncResult)
Dim listener As Socket = CType(ar.AsyncState, Socket) ' Get the socket that handles the client request.
Dim handler As Socket = listener.EndAccept(ar) ' End the operation.
Dim state As New StateObject ' Create the state object for the async receive.
state.workSocket = handler
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
End Sub 'AcceptCallback
Public Shared Sub ReadCallback(ByVal ar As IAsyncResult)
Dim content As String = String.Empty
' Retrieve the state object and the handler socket from the asynchronous state object.
Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim handler As Socket = state.workSocket
' Read data from the client socket.
Dim bytesRead As Integer = handler.EndReceive(ar)
If bytesRead > 0 Then
' There might be more data, so store the data received so far.
state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead))
' Check for end-of-file tag. If it is not there, read more data.
content = state.sb.ToString()
If content.IndexOf("<EOF>") > -1 Then ' All the data has been read from the client. Display it on the console.
Debug.Print("Read {0} bytes from socket. " + vbLf + " Data modtaget : {1}", content.Length, content)
Send(handler, content) ' Echo the data back to the client.
Else ' Not all data received. Get more.
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf ReadCallback), state)
End If
End If
End Sub 'ReadCallback
Private Shared Sub Send(ByVal handler As Socket, ByVal data As String)
Dim byteData As Byte() = Encoding.ASCII.GetBytes(data) ' Convert the string data to byte data using ASCII encoding.
handler.BeginSend(byteData, 0, byteData.Length, 0, New AsyncCallback(AddressOf SendCallback), handler) ' Begin sending the data to the remote device.
Debug.Print("Sendt til klilent: " & data)
End Sub 'Send
Private Shared Sub SendCallback(ByVal ar As IAsyncResult)
Dim handler As Socket = CType(ar.AsyncState, Socket) ' Retrieve the socket from the state object.
' Complete sending the data to the remote device.
Dim bytesSent As Integer = handler.EndSend(ar)
Debug.Print("Sent {0} bytes to client.", bytesSent)
handler.Shutdown(SocketShutdown.Both)
handler.Close()
allDone.Set() ' Signal the main thread to continue.
End Sub 'SendCallback
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim bytes() As Byte = New [Byte](1023) {} ' Data buffer for incoming data.
' Establish the local endpoint for the socket.
Dim ipHostInfo As IPHostEntry = Dns.Resolve(Dns.GetHostName())
Dim ipAddress As IPAddress = ipHostInfo.AddressList(0)
Dim localEndPoint As New IPEndPoint(ipAddress, 8000)
Dim listener As New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) ' Create a TCP/IP socket.
' Bind the socket to the local endpoint and listen for incoming connections.
listener.Bind(localEndPoint)
listener.Listen(100)
While True
allDone.Reset() ' Set the event to nonsignaled state.
' Start an asynchronous socket to listen for connections.
Debug.Print("Venter på forbindelse...")
listener.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), listener)
allDone.WaitOne() ' Wait until a connection is made and processed before continuing.
End While
End Sub
End Class