ENVIAR DADOS VIA TCPIP E VER RETORNO.

HELIO.COSTA 31/10/2016 19:04:40
#468647
Boa noite,

preciso enviar uma string para o ip e ver o retorno.

Enviar [Ô]ABC[Ô] e ver o que retorna.

Estou enviando e lendo desta forma, mas não está funcionando.

Dim Campo As String = [Ô]ABC[Ô]
Dim tcpClient As New TcpClient() [ô]Dim tcpClient As New System.Net.Sockets.TcpClient()
[ô]tcpClient.Connect(CIP, CCanal)
tcpClient.Connect([Ô]192.168.1.32[Ô], [Ô]80[Ô])
Dim networkStream As NetworkStream = tcpClient.GetStream()
If networkStream.CanWrite And networkStream.CanRead Then
[ô] Do a simple write.
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(Campo)
networkStream.Write(sendBytes, 0, sendBytes.Length)

[ô] Read the NetworkStream into a byte buffer.
Dim bytes As Byte() = New Byte(256) {} [ô] Dim bytes(tcpClient.ReceiveBufferSize) As Byte
networkStream.Read(bytes, 0, 50) [ô]CInt(tcpClient.ReceiveBufferSize))

[ô] Output the data received from the host to the console.
Dim returnData As String = Encoding.ASCII.GetString(bytes)
Console.WriteLine(([Ô]Host returned: [Ô] + returnData))
Else
If Not networkStream.CanRead Then
Console.WriteLine([Ô]Cannot not write data to this stream. [Ô] &
[Ô]Please check the server and try again.[Ô])
tcpClient.Close()
Else
If Not networkStream.CanWrite Then
Console.WriteLine([Ô]Cannot read data from this stream. [Ô] &
[Ô]Please check the server and try again.[Ô])
tcpClient.Close()
End If
End If
End If
DS2T 01/11/2016 07:56:40
#468657
Resposta escolhida
192.168.1.32 é o IP da máquina que o código está sendo executado ou é uma máquina remota?
HELIO.COSTA 01/11/2016 19:05:51
#468679
Boa tarde,

Descobri, tinha que ter mais dados na linha de comando, para que interessar, segue abaixo:

Dim tcpClient As New TcpClient() [ô] Create a client that will connect to a server listening on the contosoServer computer at port 11000.
tcpClient.Connect(IPAutomacao, PortaAutomacao) [ô]tcpClient.Connect(CIP, CCanal)
Dim networkStream As NetworkStream = tcpClient.GetStream() [ô] Get the stream used to read the message sent by the server.
[ô]networkStream.ReadTimeout = 100 [ô] Set a 10 millisecond timeout for reading.
Dim bytes As Byte() = New Byte(256) {} [ô] Read the server message into a byte buffer.

Dim strCommand As String = [Ô]GET /ABC[Ô] + IPAutomacao + [Ô] HTTP/1.1[Ô] + vbCrLf
strCommand = strCommand + [Ô]Accept: */*[Ô] + vbCrLf
strCommand = strCommand + [Ô]Accept: text/html[Ô] + vbCrLf
strCommand = strCommand + vbCrLf

DadosIP = [Ô][Ô]
Dim TamnahoIP As Integer = 0
TamnahoIP = Len(DadosIP)
While TamnahoIP <> 257
Try
Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(strCommand)
networkStream.Write(sendBytes, 0, sendBytes.Length)
networkStream.Read(bytes, 0, Bufferautomacao) [ô]
[ô] Sleep(500)
DadosIP = Encoding.ASCII.GetString(bytes) [ô]Convert the server[ô]s message into a string and display it.
TamnahoIP = Len(DadosIP)
DadosIP += Encoding.ASCII.GetString(bytes) [ô]Convert the server[ô]s message into a string and display it.
msg = Encoding.ASCII.GetBytes(DadosIP)
StatusAutomacao = DadosIP
lblStatus.Text = [Ô]Status:[Ô]
networkStream.Close()
tcpClient.Close()
Exit While
Catch ex As Exception
lblStatus.Text = ex.Message & [Ô] - chamando ip...[Ô]
End Try
End While
Tópico encerrado , respostas não são mais permitidas