public void run() { try { // Wait for a connection. SocketConnection sc = (SocketConnection) scn.acceptAndOpen(); InputStream is = sc.openInputStream(); OutputStream os = sc.openOutputStream(); int ch = 0; int count = 0; while (ch != -1) { ch = is.read(); if (ch == -1) { break; } os.write(ch); os.flush(); count++; } } catch (IOException e) { e.printStackTrace(); } finally { finished = true; } }
private void runLoopbackTest(String url) throws IOException { System.out.println("Connecting to " + url); SocketConnection sc = (SocketConnection) Connector.open(url); try { sc.setSocketOption(SocketConnection.LINGER, 5); InputStream is = sc.openInputStream(); OutputStream os = sc.openOutputStream(); String testData = "OK\r\n"; os.write(testData.getBytes()); os.flush(); StringBuffer buf = new StringBuffer(); int ch = 0; int count = 0; while (ch != -1) { ch = is.read(); buf.append((char) ch); count++; if (count >= testData.length()) { break; } } assertEquals("Data received", buf.toString(), testData); is.close(); os.close(); } finally { sc.close(); } }