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

public class DataInputStreamTest extends Thread {
    ServerSocket svrsock;

    DataInputStreamTest(int port) {
        try {
            svrsock = new ServerSocket(port);
        } catch (IOException e) {
	    System.out.println("Cannot create a server socket.");
	    e.printStackTrace();
	}
    }


    public void run() {
	while (true) {
	    Socket cltsock = null;
	    try {
		cltsock = svrsock.accept();
		if (cltsock != null)
		    readFrom(cltsock);
		cltsock.close();
	        sleep(10);
	    } catch (InterruptedException e) {
		System.out.println("Interrupted.");
		if (cltsock != null) cltsock = null;
		if (svrsock != null) svrsock = null;
		return;
	    } catch (IOException ioe) {
		System.out.println("IOException occuered.");
		ioe.printStackTrace();
	    }
	}
    }

    void readFrom(Socket cltsock) {
	DataInputStream dis;
	DataOutputStream dos;
	/* currently no use. */
	//PrintWriter pwtr;
	try {
	    dis = new DataInputStream(cltsock.getInputStream());
	    dos = new DataOutputStream(cltsock.getOutputStream());
	    /* currently no use. */
	    //pwtr = new PrintWriter(cltsock.getOutputStream());
	} catch (IOException e) {
	    System.out.println("Cannot initialize streams");
	    e.printStackTrace();
	    return;
	}

	int length, retlen;
	byte[] buf;
	try {
	    length = dis.readInt();
	    buf = new byte[length];
	    retlen = dis.read(buf);

	    String msg = new String(buf);
	    System.out.println("---Accepting Message (length: " + length
			       + " retlen: " + retlen + ")---\n"
			       + msg
			       + "\n---end---");
	    String retstr = "your message is \"" + msg + '"';
	    System.out.println("---Returning Message---\n"
			       + retstr
			       + "\n---end---");
	    /* this code do not work correctly. why? */
	    /* maybe I cannot use this class in JDK 1.1.1 */
	    //pwtr.println(retstr);
	    dos.write(retstr.getBytes());
	} catch (IOException e) {
	    System.out.println("Cannot communicate");
	    e.printStackTrace();
	}
    }

    public static void main(String args[]) {
	if (args.length < 1) {
	    System.out.println("Please give me a port number");
	    System.exit(0);
	}
	int port = Integer.parseInt(args[0]);
	DataInputStreamTest svr = new DataInputStreamTest(port);
	svr.start();

	SimpleClient clt = new SimpleClient("localhost", port);

	String msgs[] = {
	    "This is the first test.",
	    "Double lines\n hello--"
	    };
	for (int i=0;i<msgs.length;i++) {
	    System.out.println("---Sending message---\n" + msgs[i]
			       + "\n---end---");
	    clt.sendMessage(msgs[i]);
	}

	svr.stop();
    }
}

class SimpleClient {
    String hostname;
    int port;
    Socket svr;

    SimpleClient(String hostname, int port) {
	this.hostname = hostname;
	this.port = port;
    }

    public void sendMessage(String msg) {
	try {
	    svr = new Socket(hostname, port);
	} catch (IOException e) {
	    System.out.println("Cannot connect server " + hostname +
			       "/" + port);
	    e.printStackTrace();
	}

	DataOutputStream dos;
	BufferedReader brdr;
	try {
	    dos = new DataOutputStream(svr.getOutputStream());
	    /* this code do not work correctly. why? */
	    brdr = new BufferedReader(new InputStreamReader(svr.getInputStream()));
	} catch (IOException e) {
	    System.out.println("Cannot initialize streams");
	    e.printStackTrace();
	    //svr.close();
	    svr = null;
	    return;
	}	    

	try {
	    byte[] buf = msg.getBytes();
	    dos.writeInt(buf.length);
	    dos.write(buf);
	
	    String retstr = brdr.readLine();
	    /*
	     * This following code is critical if the connection is stable.
	     * I think.
	     */
	    while (brdr.ready()) {
		retstr += brdr.readLine();
	    }
	    System.out.println("---returned message--\n"
			       + retstr
			       + "\n---end---");
	} catch (IOException e) {
	    System.out.println("Cannot communicate");
	    e.printStackTrace();
	}

	try {
	    svr.close();
	} catch (IOException ioe) {
	    System.out.println("Cannot close svr socket.");
	}
	svr = null;
    }
}
