Imports System.Net Imports System.Net.Sockets Module Server Dim some(1) As Socket Dim sck As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp) Sub Main() Dim myip As IPAddress = IPAddress.Parse("127.0.0.1") Dim myep As EndPoint = New IPEndPoint(myip, 1234) sck.Bind(myep) sck.Listen(1) WaitForConnection(sck) Console.ReadKey() End Sub Sub AcceptConnection(ByVal CBack As IAsyncResult) Dim a As Socket = CType(CBack.AsyncState, Socket) ReDim Preserve some(some.Length) some(some.Length - 1) = a.EndAccept(CBack) Send("Check", some(some.Length - 1)) WaitForConnection(CType(CBack.AsyncState, Socket)) End Sub Sub WaitForConnection(ByRef sock As Socket) sock.BeginAccept(AddressOf AcceptConnection, sock) End Sub Sub Send(ByVal text As String, ByRef sock As Socket) Dim bytes(text.Length) As Byte bytes = System.Text.Encoding.UTF8.GetBytes(text) sock.BeginSend(bytes, 0, bytes.Length, SocketFlags.None, AddressOf SendComplete, sock) End Sub Sub SendComplete(ByVal CBack As IAsyncResult) CType(CBack.AsyncState, Socket).EndSend(CBack) End Sub Sub ConnectRequest(ByVal CBack As IAsyncResult) Dim wsk As Socket = CType(CBack.AsyncState, Socket).EndAccept(CBack) End Sub End Module
|