TizzyT

ServerClient -TizzyT

Feb 26th, 2017
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.31 KB | None | 0 0
  1. Imports System.Net
  2. Imports System.Net.Sockets
  3. Imports System.Text
  4.  
  5. Module Module1
  6.     Dim _Client As Socket
  7.     Dim readerThread As Threading.Thread
  8.     Sub Main()
  9.         _Client = New Socket(SocketType.Stream, ProtocolType.Tcp)
  10.         _Client.Connect(New IPEndPoint(IPAddress.Parse(RequestLine("Enter an IP Adress")), Integer.Parse(RequestLine("Enter a port"))))
  11.         readerThread = New Threading.Thread(Sub()
  12.                                                 While True
  13.                                                     Dim Failed As Boolean = False
  14.                                                     Dim Count As Integer
  15.                                                     Dim R() As Byte
  16.                                                     If _Client.Available > 0 Then
  17.                                                         Dim Buffer(_Client.Available - 1) As Byte
  18.                                                         SyncLock _Client
  19.                                                             _Client.Receive(Buffer, Buffer.Length, SocketFlags.None)
  20.                                                         End SyncLock
  21.                                                         Console.WriteLine(Encoding.UTF8.GetString(Buffer).Replace(vbNullChar, ""))
  22.                                                     End If
  23.                                                 End While
  24.                                             End Sub)
  25.         readerThread.Start()
  26.         While True
  27.             Dim M As String = String.Empty
  28.             M = RequestLine("Enter a message")
  29.             If Not M.Equals(String.Empty) Then
  30.                 SyncLock _Client
  31.                     _Client.Send(Encoding.UTF8.GetBytes(M))
  32.                 End SyncLock
  33.             End If
  34.         End While
  35.     End Sub
  36.  
  37.     Public Function RequestLine(ByVal Message As String) As String
  38.         Console.WriteLine(Message)
  39.         Return Console.ReadLine
  40.     End Function
  41.  
  42. End Module
Advertisement
Add Comment
Please, Sign In to add comment