rendered paste bodyimport 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;
Server(int port, int clients){
listenport = port;
}
public void startListening() {
try {
myserver = new ServerSocket(listenport);
} catch (IOException e) {
System.err.println("Could not listen on port " + listenport);
System.exit(1); }
try {
myclient = myserver.accept();
} catch (IOException e) {
System.err.println("accept failed, sorry");
System.exit(1); }
}
}
class Client {
String hostname;
int serverport;
Socket clientsocket = null;
String status = null;
int id;
Client(int id, String host, int port, String status) {
this.id = id;
this.status = status;
this.hostname = host;
}
public void connect() {
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);
}
}
}