How can i send an String to my pcs port? tcp/ip

20 Views Asked by At

I am trying to make a program in vb net that connects to the local ip 127.0.0.1 and sends a string to port 6680, waits for a response (msgbox), sends a string again and waits for a response again (msgbox) but I can't do it, could someone help me? I want somthing like this with no classes and no methods, all in the main:

  1. Connection to the Local Ip (127.0.0.1)
  2. Send String to a port 6680 "String"
  3. Write the response with a msgbox
  4. Send String to the same port 6680 "String"
  5. Write the response with a msbox Thank you!

I tried this but it didn't work, it hangs:

             Dim client As TcpClient = New TcpClient("127.0.0.1", port)

            ' Buffer to store the response bytes.
            Dim data As Byte()

            ' String to store the response ASCII representation.
            Dim responseData As String = String.Empty

            ' Read the first batch of the TcpServer response bytes.
            Dim Stream As NetworkStream = client.GetStream()
            Stream.ReadTimeout = 1000
            Dim bytes As Int128 = Stream.Read(data, 0, data.Length)


            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
            'responseData = System.Text.Encoding.GetEncoding("ISO-8859-1").GetString(data, 0, bytes);

            MyData = responseData.Replace("\r\n", "")
            Stream.Close()
            client.Close()

            MsgBox(MyData)
0

There are 0 best solutions below