package com.sun.midp.io.j2me.socket; import gnu.testlet.TestHarness; import gnu.testlet.MIDletTestlet; import java.io.*; import javax.microedition.io.*; public class StressTestSocket implements MIDletTestlet { public int getExpectedPass() { return 200; } public int getExpectedFail() { return 0; } public int getExpectedKnownFail() { return 0; } TestHarness th; static final String URL = "socket://localhost:50003"; public void test(TestHarness th) { this.th = th; try { testMultipleSendsReceivesOnSameSocket(); testMultipleSendsReceivesOnMultipleSockets(); } catch (IOException e) { th.fail("Exception unexpected: " + e); e.printStackTrace(); } } void send(OutputStream os, InputStream is, String string) throws IOException { os.write((string + "\n").getBytes()); byte buf[] = new byte[1024]; int i = 0; do { buf[i++] = (byte)is.read(); } while (buf[i-1] != -1 && buf[i-1] != '\n' && i < buf.length); String received = new String(buf, 0, i-1); th.check(received, string); } void testMultipleSendsReceivesOnSameSocket() throws IOException { SocketConnection client = (SocketConnection)Connector.open(URL); OutputStream os = client.openOutputStream(); InputStream is = client.openInputStream(); for (int i = 0; i < 100; i++) { send(os, is, "Message n." + i); } is.close(); os.close(); client.close(); } void testMultipleSendsReceivesOnMultipleSockets() throws IOException { for (int i = 0; i < 100; i++) { SocketConnection client = (SocketConnection)Connector.open(URL); OutputStream os = client.openOutputStream(); InputStream is = client.openInputStream(); send(os, is, "Message n." + i); is.close(); os.close(); client.close(); } } }