client / server application with TCPClient

0

Good morning, I am developing an application that communicates with a control box and receives data from it. the control box sends me a plot in ascii with which I determine if a sensor is being covered or a loop is being stepped on, and I send it an ascii frame to control 2 traffic lights. the comuniacion that I use is via serial / ethernet with a converter. the problem I have is that with the class TCPclient , I can send data to the control box without problems, but when I want to receive data from it using tcplistener I get the message :

The communication with the control box is continuous, that is, I am receiving and sending data at the same time. How can I solve this problem if I can send because I can not receive without problems, what am I doing wrong?

I enclose a fragment of the code that validates the reception of data and throws me the error.

Public Class RecebirDato
Public Delegate Sub ClientCarrier(conexionTcp As ConexionTCP)
Public Event OnClientConnected As ClientCarrier
Public Event OnClientDisconnected As ClientCarrier
Public Delegate Sub DataRecieved(conexionTcp As ConexionTCP, data As String)
Public Event OnDataRecieved As DataRecieved
Public Shared _tcpListener As TcpListener
Public Shadows _acceptThread As Thread
Private connectedClients As New List(Of ConexionTCP)

Public Sub New()
    AddHandler OnDataRecieved, AddressOf MensajeRecibido
    AddHandler OnClientConnected, AddressOf ConexionRecibida
    AddHandler OnClientDisconnected, AddressOf ConexionCerrada
End Sub

Private Sub MensajeRecibido(conexionTcp As ConexionTCP, datos As String)
    Dim paquete = New Paquete(datos)
    Dim valor As String
    Dim principal As New Principal
    Dim contenido As String = paquete.Contenido
    Dim valores As List(Of String) = Mapa.Deserializar(contenido)
    valor = valores(0)
    Dim msgPack = New Paquete(valor)
    conexionTcp.EnviarPaquete(msgPack)
    principal.txtObservaciones.Text = valor
End Sub

Private Sub ConexionRecibida(conexionTcp As ConexionTCP)
    SyncLock connectedClients
        If Not connectedClients.Contains(conexionTcp) Then
            connectedClients.Add(conexionTcp)
        End If
    End SyncLock
    'Invoke(New Action(Function() InlineAssignHelper(label1.Text, String.Format("Clientes: {0}", connectedClients.Count))))
End Sub

Private Sub ConexionCerrada(conexionTcp As ConexionTCP)
    SyncLock connectedClients
        If connectedClients.Contains(conexionTcp) Then
            Dim cliIndex As Integer = connectedClients.IndexOf(conexionTcp)
            connectedClients.RemoveAt(cliIndex)
        End If
    End SyncLock
    'Invoke(New Action(Function() InlineAssignHelper(label1.Text, String.Format("Clientes: {0}", connectedClients.Count))))
End Sub

Public Sub EscucharClientes(_ipAddress As String, _port As Integer)
    Try
        _tcpListener = New TcpListener(IPAddress.Parse(_ipAddress), _port)
        _tcpListener.Start()
        _acceptThread = New Thread(AddressOf AceptarClientes)
        _acceptThread.Start()
    Catch e As Exception
        MessageBox.Show(e.Message.ToString())
    End Try
End Sub

Private Sub AceptarClientes()
    Do
        Try
            Dim conexion = _tcpListener.AcceptTcpClient()
            Dim srvClient = New ConexionTCP(conexion)
            With srvClient
                .ReadThread = New Thread(AddressOf LeerDatos)
            End With
            srvClient.ReadThread.Start(srvClient)
            RaiseEvent OnClientConnected(srvClient)
        Catch e As Exception
            MessageBox.Show(e.Message.ToString())
        End Try
    Loop While True
End Sub

Private Sub LeerDatos(client As Object)
    Dim cli = TryCast(client, ConexionTCP)
    Dim charBuffer = New List(Of Integer)()
    Do
        Try
            If cli Is Nothing Then
                Exit Try
            End If
            If cli.streamReader.EndOfStream Then
                Exit Try
            End If
            Dim charCode As Integer = cli.streamReader.Read()
            If charCode = -1 Then
                Exit Try
            End If
            If charCode <> 0 Then
                charBuffer.Add(charCode)
                Continue Do
            End If
            If OnDataRecievedEvent IsNot Nothing Then 
                Dim chars = New Char(charBuffer.Count - 1) {}
                For i As Integer = 0 To charBuffer.Count - 1
                    chars(i) = Convert.ToChar(charBuffer(i))
                Next i
                Dim message = New String(chars)
                RaiseEvent OnDataRecieved(cli, message)
            End If
            charBuffer.Clear()
        Catch generatedExceptionName As IOException
            Exit Try
        Catch e As Exception
            MessageBox.Show(e.Message.ToString())
            Exit Try
        End Try
    Loop While True
    RaiseEvent OnClientDisconnected(cli)
End Sub

End Class

in advance thanks for your help.

    
asked by Benkos 05.01.2017 в 18:12
source

1 answer

0

I already solved it, the only thing I did was create a TCP client class that sent me and received data from a server, it is the same class as many times as I need with ip routes that are inside the network segment.

Thanks greetings.

    
answered by 18.01.2017 / 18:59
source