private void runTcpServer() { ServerSocket ss = null; try { ss = new ServerSocket(TCP_SERVER_PORT); //ss.setSoTimeout(10000); //accept connections Socket s = ss.accept(); BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); //receive a message String incomingMsg = in.readLine() + System.getProperty("line.separator"); Log.i("TcpServer", "received: " + incomingMsg); text.append("received: " + incomingMsg); //send a message String outgoingMsg = "goodbye from port " + TCP_SERVER_PORT + System.getProperty("line.separator"); out.write(outgoingMsg); out.flush(); Log.i("TcpServer", "sent: " + outgoingMsg); text.append("sent: " + outgoingMsg); //SystemClock.sleep(5000); s.close(); } catch (InterruptedIOException e) { //if timeout occurs e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (ss != null) { try { ss.close(); } catch (IOException e) { e.printStackTrace(); } } } }
Socket s = ss.accept();
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)