简体   繁体   中英

Threaded TCP connection - Android

I'm trying to make an Android app that will send some packets of data to an embedded device. Since my embedded device is not supporting UDP datagram protocol, I would like to simulate it using sync TCP.

That being said, a response string from the device is not needed, so I would like to disconnect the TCP socket as soon as the data is sent from my app.

I would be grateful if someone could outline how to make a Thread that would perform the connection and close the socket if no data is received within some timeframe.

My current code is simple:

    try {
        Socket s = new Socket("localhost",12345);

        //outgoing stream redirect to socket
        OutputStream out = s.getOutputStream();

        PrintWriter output = new PrintWriter(out);
        output.println("Hello Android!");
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));

        //read line(s)
        String st = input.readLine();
        . . .
        //Close connection
        s.close();


} catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
} catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

Thank you in advance!!!

First take care about the protocol, if your application stuck at String st = input.readLine(); so it doesn't mean server doesn't send data, because the readLine() method expect a line(end with CRLF), and this is possible that server just sends some string without line feed, then buffered reader still stucks the thread(current thread) because it expects the CRLF. Next you would have another class which accepts the Socket as parameter, then waits for while, then close the socket because of no response from the server., something like this

class SocketMgr implements Runnable{
final private int timeout=5000;
private Socket s;
public SocketMgr(Socket s){this.s=s;new Thread(this).start();}
public void run(){
try{Thread.sleep(timeout);s.close();}catch(Exception ex){}
}
}

and in your code

 try {
        Socket s = new Socket("localhost",12345);

        //outgoing stream redirect to socket
        OutputStream out = s.getOutputStream();

        PrintWriter output = new PrintWriter(out);
        output.println("Hello Android!");
        output.flush();
        BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
        //////////////////////////////////
        new SocketMgr(s);
        //////////////////////////////////
        //read line(s)
        String st = input.readLine();
        . . .
        //Close connection
        s.close();


} catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
} catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

also you would check this out for a simple [java threading tutorial] once again, this is important to know about the protocol belong to your server. good luck, have a nice socket program :).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM