All pastes #2006423 Raw Edit

Anonymous

public text v1 · immutable
#2006423 ·published 2010-11-30 01:51 UTC
rendered paste body
import java.io.*;
import java.net.*;

public class Node {

    public static void main(String[] args) {

        if(args.length == 2) {
		Server s = new Server(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
		s.startListening();
        } else if(args.length == 4) {
            	Client c = new Client(Integer.parseInt(args[0]),args[1],Integer.parseInt(args[2]),args[3]);
		c.connect();
        } else {
            	System.err.println("\nUsage (Server): java Node <port> <clients>");
		System.err.println("Usage (Client): java Node <id> <server> <port> <COMMIT|ABORT>\n");
		System.err.println("Note: Only ports 9000-9060 may be used on DCS machines\n");
            	System.exit(-1);
        }
    }
}

class Server {
	int listenport;
	ServerSocket myserver = null;
	Socket myclient = null;
	String inputline;
	PrintWriter out = null;
	BufferedReader in = null;

	Server(int port, int clients){
		listenport = port;

// should be doing something about clients here

					}

	public void startListening() {

	try {
		myserver = new ServerSocket(listenport);
		} catch (IOException e)   {
			 System.err.println("Could not listen on port " + listenport);
			System.exit(1); }
	System.out.println("Im alive and listening for 1 client");




	try {
		myclient = myserver.accept();
		} catch (IOException e)	{
			System.err.println("accept failed, sorry");
			System.exit(1); }

	System.out.println("Client connection accepted");

	try {
		 out = new PrintWriter(myclient.getOutputStream(), true);
		} catch (IOException e) {
				System.err.println("couldnt get output stream for myclient socket"); }

	try {
		in = new BufferedReader(
                               new InputStreamReader(
                                myclient.getInputStream()));
		} catch (IOException e) { System.err.println("couldnt get input stream for myclient socket"); }




	try {	inputline = in.readLine(); } catch (IOException e) {}

	if (inputline.equals("hello"))	{
		System.out.println("Client said Hello");
		System.out.println("Sending ACK to client");
		out.println("ACK");
	 }

	else System.out.println("client said something wierd");

	System.out.println("closing connections");
	try {in.close();} catch (IOException e) {}
	out.close();
	try {myclient.close();} catch (IOException e) {}
	try {myserver.close();} catch (IOException e) {}

}	}


class Client {

	String hostname;
	int serverport;
	Socket clientsocket = null;
	String status = null;
	int id;
	String inputline;
	String outputline;
	PrintWriter out = null;
	BufferedReader in = null;


	Client(int id, String host, int port, String status) {
	this.id = id;		// not in use yet
	this.serverport = port;
	this.status = status;
	this.hostname = host;
	}

	public void connect() {

	System.out.println("Im alive and trying to connect to server");

	try {
	clientsocket = new Socket(hostname, serverport);
	} catch (UnknownHostException e) {
		System.err.println("dont know about this host :  " + hostname);
		System.exit(1);
		}
	catch (IOException e) {
		System.err.println("could not get IO for this host " + hostname) ;
		System.exit(1);
			}
	System.out.println("connection accepted, forming IO streams on socket");

	try {
		out = new PrintWriter(clientsocket.getOutputStream(), true);
		} catch (IOException e) {
				System.err.println("couldnt get output stream for client socket"); }

	try {
		 in = new BufferedReader(
                               new InputStreamReader(
                                clientsocket.getInputStream()));
		} catch (IOException e) { System.err.println("couldnt get input stream for client socket"); }

	System.out.println("Saying hello to server....");
	out.println("hello");

	try {inputline = in.readLine(); } catch (IOException e) {}
	if (inputline == "ACK") {
		System.out.println("server send ACK"); }


	System.out.println("closing connections");
	try {in.close();} catch (IOException e) {}
	out.close();
	try {clientsocket.close();} catch (IOException e) {}
	}

}