TCP client RRS feed

  • Question

  • Hi guys

    i have a machine A is a linux computer with ip and port 8001

    i donno is he listening or not so i just use a tcpclient to talk with this machine.

    when i point the ip to the linux computer and  it give me error shows tat( \opt\pcscale\files\recipe\.default.txt file not found)

    so i tried pointing the Ip address to my computer(windows 7) ip which is with different path.
    path(D:\dsa.txt ) it does not return anything, only return text " host return" missed out the returndata.



    Dim ip As IPAddress = IPAddress.Parse(TextBox1.Text) Dim port As Integer = TextBox2.Text Dim ipe As New IPEndPoint(ip, port) Dim tcpClient As New TcpClient() Try tcpClient.Connect(ipe) Dim networkStream As NetworkStream = tcpClient.GetStream() If networkStream.CanWrite And networkStream.CanRead Then Dim TXFileSize As String TXFileSize = CStr(FileLen(filepathtxt.Text)) Dim SendTXFileSize As [Byte]() = Encoding.ASCII.GetBytes(TXFileSize) networkStream.Write(SendTXFileSize, 0, SendTXFileSize.Length) Dim TXBytes() As Byte TXBytes = My.Computer.FileSystem.ReadAllBytes(filepathtxt.Text) networkStream.Write(TXBytes, 0, TXBytes.Length) Dim RXbytes(tcpClient.ReceiveBufferSize) As Byte networkStream.Read(RXbytes, 0, CInt(tcpClient.ReceiveBufferSize)) Dim returndata As String = Encoding.ASCII.GetString(RXbytes) ListBox1.Items.Add("Host returned: " & returndata) Else If Not networkStream.CanRead Then ListBox1.Items.Add("cannot not write data to this stream" & vbCrLf) tcpClient.Close() Else If Not networkStream.CanWrite Then ListBox1.Items.Add("cannot read data from this stream" & vbCrLf) tcpClient.Close() End If End If End If Catch ex As Exception ListBox1.Items.Add(ex.Message & vbCrLf) End Try

    after the above test. i put a server to it. when i debug it, the sequence of running path is different between linux IP and my computer ip. u guys can try out the code and u noe what i meant.

    so again i did it with linux ip and path ,still file not found. and i point the file path to my CPU(D:\dsa.txt)  and same thing return. as just now.

    finally i point to my computer IP and path( D:\dsa.txt) it return me the text inside the dsa.txt. run with breakpoint  and step by step it return the   "Correct Data"

    if i run without break point. return result was different from with breakpoint.

    return result was just " 31 " 31 was from line " Dim clientdata As String = Encoding.ASCII.GetString(bytes)"only if with break point i can slowly see the clientdata string become text in the dsa.txt file .why is this so?

    but the point is, i need to retrive data from linux computer not my computer. can anyone tell me how to do it?

      Const portNumber As Integer = 8001
            Dim tcpListener As New TcpListener(portNumber)
            ListBox1.Items.Add("Waiting for connection...")
                Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
                ListBox1.Items.Add("Connection accepted.")
                Dim networkStream As NetworkStream = tcpClient.GetStream()
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
                Dim clientdata As String = Encoding.ASCII.GetString(bytes)
                ListBox1.Items.Add(("Client sent: " + clientdata))
                Dim responseString As String = "Connected to server."
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
                ListBox1.Items.Add(("Message Sent / : " + responseString))
            Catch e As Exception
            End Try

    • Edited by newbiew Tuesday, April 2, 2013 9:23 AM
    • Moved by Youen Zen Wednesday, April 3, 2013 6:40 AM From visual basic
    Tuesday, April 2, 2013 9:16 AM

All replies