0

I am having problems trying to use my android (Basic4Android) to communicate with my PC running a .net TCP server. I need to be able to have buttons that send 4byte commands to the server and receive back a response. When I run the program on the android the server does connect and receives the string "INFO", but then nothing else sends or receives until I restart the program and it only sends the command "INFO" again. I don't get any errors when I press the buttons to send commands, but the server never receives anything. The server is a Windows form multi-thread program written in VB.NET. I wrote a VB.NET client program that works that I can attach as an example of what I am trying to do. This is my first attempt at a Android application and so far I am just modifing the network examples I found in the tutorials.

The code is below... Thanks

Sub Process_Globals
    Dim Socket1 As Socket
End Sub

Sub Globals
    Dim Button_ARM As Button
    Dim Button_STAY As Button
    Dim Button_AUTO As Button
    Dim Button_OFF As Button
    Dim Label_Received As Label
    Dim Label_Sent As Label
    Dim tr As TextReader 
    Dim tw As TextWriter
    Dim sb As StringBuilder
End Sub

Sub Activity_Create(FirstTime As Boolean) 
    Activity.LoadLayout("Alarm_Control")
    Socket1.Initialize("Socket1") 
    Socket1.Connect("#.#.#.#" , 8000, 20000)   'My IP address goes here
End Sub

Sub Socket1_Connected (Successful As Boolean) 
    If Successful = False Then 
        Msgbox(LastException.Message, "Error connecting") 
        Return 
    End If 
    tr.Initialize(Socket1.InputStream)
    tw.Initialize(Socket1.OutputStream)
    tw.WriteLine("INFO")
    Label_Sent.Text = "Sent INFO"
    tw.Flush    
    sb.Initialize
    sb.Append(tr.ReadLine) 
    Label_Received.Text = sb.ToString
    'Socket1.Close
End Sub 

Sub Button_ARM_Click 
    tw.WriteLine("O001")
    tw.Flush
    Label_Sent.Text = "Sent O001"
End Sub

Sub Button_STAY_Click
    tw.WriteLine("O002")
    tw.Flush
    Label_Sent.Text = "Sent O002"
End Sub

Sub Button_OFF_Click
    tw.WriteLine("O000")
    tw.Flush
    Label_Sent.Text = "Sent O000"
End Sub
Erel
  • 1,802
  • 2
  • 15
  • 58
user925882
  • 11
  • 1
  • 1

3 Answers3

0

How are you reading the data in the server side? Note that TextWriter.WriteLine writes a line terminated with chr(10). Make sure that you are not waiting for chr(13) and chr(10).

Erel
  • 1,802
  • 2
  • 15
  • 58
0

I am using networkStream.read and networkStream.write in a thread called by the listner. The vb.net client program work correctly with the vb.net server.

Public Sub handlerThread()
    Dim handlerSocket As Socket
    handlerSocket = alSockets(alSockets.Count - 1)
    Dim networkStream As NetworkStream = New NetworkStream(handlerSocket)
    Dim bytes(4) As Byte
    networkStream.Read(bytes, 0, CInt(4))        ' Read the stream into 4-byte array
    Dim clientdata As String = Encoding.ASCII.GetString(bytes) 
    Label_Received.Text = clientdata
    Dim responseString As String = "Received " + clientdata
    Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
    networkStream.Write(sendBytes, 0, sendBytes.Length)
    Label_Sent.Text = responseString
    handlerSocket = Nothing
End Sub
FireAphis
  • 6,650
  • 8
  • 42
  • 63
0

Chr(10) does not seem to be an issue here. I'm having the same problem using this java server code:

import java.io.*;
import java.net.*;

public class ec192 {
  public static void main(String[] args) throws IOException {

    Socket echoSocket = null;
    PrintWriter out = null;
    BufferedReader in = null;

    try {

  echoSocket = new Socket("192.168.0.45", 2222);
      out = new PrintWriter(echoSocket.getOutputStream(), true);
      in = new BufferedReader(new InputStreamReader(
                              echoSocket.getInputStream()));
    } catch (UnknownHostException e) {
      System.err.println("Don't know about host: taranis.");
      System.exit(1);
    } catch (IOException e) {
      System.err.println("Couldn't get I/O for "
                         + "the connection to: 2222 taranis.");
      System.exit(1);
    }

BufferedReader stdIn = new BufferedReader(
                               new InputStreamReader(System.in));
String userInput;

    while ((userInput = stdIn.readLine()) != null) {
    out.println(userInput);
    System.out.println("echo: " + in.readLine());
}

out.close();
in.close();
stdIn.close();
echoSocket.close();
  }
}
Ken White
  • 123,280
  • 14
  • 225
  • 444