Cliente:
Código
Public Class Connection Public Class Client Private _Server As String Private _Port As Integer Private c_Socket As New TcpClient Private c_Stream As NetworkStream #Region "Props" Public Property Server() As String Get Server = _Server End Get Set(ByVal value As String) _Server = value End Set End Property Public Property Port() As Integer Get Port = _Port End Get Set(ByVal value As Integer) _Port = value End Set End Property #End Region #Region "Eventos" #Region "Declaracion(delegados)" Public Delegate Sub _OnConnect(ByVal server As String, ByVal port As Integer) Public Delegate Sub _OnDisconect() Public Delegate Sub _OnError(ByVal Info As String) Public Delegate Sub _OnSendComplete() #End Region #Region "Declaracion(Evento)" Public Event OnConnect As _OnConnect Public Event OnDisconnect As _OnDisconect Public Event OnError As _OnError Public Event OnSendComplete As _OnSendComplete #End Region #End Region #Region "Funciones" Public Sub Connect(ByVal server As String, ByVal port As Integer) _Server = server _Port = port Connect() End Sub Public Sub Connect() Try c_Socket.Connect(_Server, _Port) c_Stream = c_Socket.GetStream() RaiseEvent OnConnect(_Server, _Port) Catch ex As Exception RaiseEvent OnError(ex.Message) End Try End Sub Public Sub Disconnect() Try c_Socket.Close() c_Stream.Close() RaiseEvent OnDisconnect() Catch ex As Exception RaiseEvent OnError(ex.Message) End Try End Sub Public Sub Send(ByVal Data As String) Try Dim Buffer As Byte() Buffer = Encoding.ASCII.GetBytes(Data) c_Stream.Write(Buffer, 0, Buffer.Length) c_Stream.Flush() RaiseEvent OnSendComplete() Catch ex As Exception RaiseEvent OnError(ex.Message) End Try End Sub #End Region End Class End Class
Servidor:
Código
Public Class Connection Public Class Server Private _Port As Integer Private s_Listener As TcpListener Private s_Socket As Socket Private s_Timer As New Timers.Timer(50) Private b_Listen As Boolean = False #Region "Eventos" #Region "Declaracion(delegados)" Public Delegate Sub _OnListening(ByVal port As Integer) Public Delegate Sub _OnStop() Public Delegate Sub _OnAccept(ByVal server As String) Public Delegate Sub _RecievedData(ByVal Data As String, ByVal bytes As Integer) Public Delegate Sub _OnError(ByVal Info As String) #End Region #Region "Declaracion(Evento)" Public Event OnStartListening As _OnListening Public Event OnStopListening As _OnStop Public Event OnConnectionAccept As _OnAccept Public Event OnDataRecieved As _RecievedData Public Event OnError As _OnError #End Region #End Region #Region "Props" Public Property Port() As Integer Get Port = _Port End Get Set(ByVal value As Integer) _Port = value End Set End Property #End Region #Region "Funciones" Public Sub StartListen(ByVal port As Integer) _Port = port StartListen() End Sub Public Sub StartListen() Try Dim _server As IPAddress = IPAddress.Parse("127.0.0.1") s_Listener = New TcpListener(_server, _Port) s_Listener.Start() b_Listen = True AddHandler s_Timer.Elapsed, AddressOf s_Timer_Tick RaiseEvent OnStartListening(_Port) Catch ex As Exception RaiseEvent OnError(ex.Message) End Try End Sub Public Sub StopListen() Try b_Listen = False s_Listener.Stop() s_Socket.Close() RemoveHandler s_Timer.Elapsed, AddressOf s_Timer_Tick RaiseEvent OnStopListening() Catch ex As Exception RaiseEvent OnError(ex.Message) End Try End Sub #End Region #Region "Internal" Private Sub s_Timer_Tick(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) Dim Data As Byte(), b As Boolean = False Data = Nothing While b_Listen = True s_Socket = s_Listener.AcceptSocket() If b = False Then s_Socket.SendTimeout = 100 b = True End If RaiseEvent OnConnectionAccept(s_Socket.RemoteEndPoint.ToString) s_Socket.Receive(Data, s_Socket.ReceiveBufferSize, SocketFlags.None) RaiseEvent OnDataRecieved(Encoding.ASCII.GetString(Data), Data.Length) End While End Sub #End Region End Class End Class