Hola a todos aqui os traigo el ejemplo de tcplistener y tcpclient para que 2 aplicaciones se puedan comunicar.
Como el codigo esta comentado solo voy a poner los pantallazos de los
formularios,
aparte tenemos el video del ejemplo en youtube aqui
Parte Servidor
Y a continuacion el codigo:
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 |
Imports System.Net.Sockets Imports System.Text Public Class Form1 'Lo declaro Dim TCP As TcpListener Dim th As New Threading.Thread(AddressOf Rutina) 'Controlara que se esta ejecutando el bucle siempre y cuando sea false Dim Ejecuto = False Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'Deshabilito que se pueda interactual con otros elementos en la runtina de trabajo CheckForIllegalCrossThreadCalls = False End Sub Dim TCPSERVERCLI As New TcpClient Public Function Rutina() Do If Ejecuto = True Then Exit Do End If 'miro si tcp tiene conexiones pendiente XD If TCP.Pending = True Then 'Si tiene Pendientes lo asigno a el cliente del servidor que hara de escucha TCPSERVERCLI.Client = TCP.AcceptSocket End If If TCPSERVERCLI.Connected = True Then 'Indica que hay datos pendientes de recoger If TCPSERVERCLI.Available > 0 Then Dim DataBytes(1000) As Byte 'Ahora el decodificador Dim decode As New ASCIIEncoding TCPSERVERCLI.Client.Receive(DataBytes) 'ahora decodifico los bytes y los paso a el richtextbox Me.RichTextBox1.Text = decode.GetString(DataBytes) 'La Misma operacion que en el cliente Dim DevuelveSi(100) As Byte DevuelveSi = decode.GetBytes("SI ME HA LLEGADO") TCPSERVERCLI.Client.Send(DevuelveSi) End If End If Loop End Function Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click TCP = New TcpListener(Me.TextBox1.Text) TCP.Start() 'Inicio el hilo th.Start() End Sub End Class |
Ahora la parte Cliente
Primero la imagen del formulario
Ahora el Codigo
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 |
Imports System.Net.Sockets Imports System.Text Public Class Form1 Dim tcpcli As TcpClient Dim th As New Threading.Thread(AddressOf Conectado) Dim salir As Boolean = False Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing salir = True End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'Lo desactivo al igual que el server CheckForIllegalCrossThreadCalls = False End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click tcpcli = New TcpClient(IP.Text, PUERTO.Text) Try If tcpcli.Connected = False Then tcpcli.Connect(IP.Text, PUERTO.Text) End If th.Start() Catch End Try End Sub 'Aqui hago lo propio para enviar el dato Public Function Conectado() Do If salir = True Then Exit Do End If If tcpcli.Connected = True Then If VarEnvio <> "" Then Dim Databyte() As Byte Databyte = New ASCIIEncoding().GetBytes(VarEnvio) tcpcli.Client.Send(Databyte) VarEnvio = "" End If End If 'Si tenemos datos a la espera If tcpcli.Connected = True Then If tcpcli.Available > 0 Then Dim TextoDewvuelto As String Dim BytesDevuelto(100) As Byte tcpcli.Client.Receive(BytesDevuelto) TextoDewvuelto = New ASCIIEncoding().GetString(BytesDevuelto) MsgBox(TextoDewvuelto) End If End If Loop End Function Dim VarEnvio As String = "" Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click VarEnvio = Me.TextBox3.Text End Sub End Class |
El ejemplo es muy simple, ahora tu lo puedes complicar todo lo que quieras.
Aqui la descarga del codigo fuente
Enlace a el video Aqui
Si te gustó , Comenta