package mug13; /* * This is the network inteface used by the client in the Grinnell MUG. * * @author Brett McMillian * @author Louisa Poythress * @author Michael Billups * * @Version 1.3 - Can send and receive objects to and from the server. * */ import java.io.*; import java.net.*; public class Client { //*************** // Fields //*************** Socket clientSocket = null; ObjectOutputStream oosFromClient; ObjectInputStream oisFromServer; //Client username String name; //*************** // Constructors //*************** public Client(String name) throws UnknownHostException, IOException, ClassNotFoundException { this.name = name; //Setup a BufferedReader to read the port# and //IP Address of the server from specs.txt int port = 0; String ipAddress; FileReader fr = new FileReader("specs.txt"); BufferedReader br = new BufferedReader(fr); InetAddress address = null; try { br.skip(6); Integer intPort = new Integer(br.readLine()); port = intPort.intValue(); br.skip(4); ipAddress = br.readLine(); address = InetAddress.getByName(ipAddress); } catch (UnknownHostException e) { System.err.println("Don't know about host."); System.exit(1); } catch (IOException e) { System.err.println("Couldn't get I/O for the connection."); System.exit(1); } //Create a new socket with IP address address on port# port this.clientSocket = new Socket(address, port); //Open an output stream. this.oosFromClient = new ObjectOutputStream(clientSocket.getOutputStream()); //Open an input stream. this.oisFromServer = new ObjectInputStream(clientSocket.getInputStream()); } //*************** // Methods //*************** public boolean login(String pswd) throws UnknownHostException, IOException, ClassNotFoundException { //Send username and password to the Server. this.oosFromClient.writeObject(this.name); this.oosFromClient.writeObject(pswd); this.oosFromClient.writeObject("login"); //Receive instructions back from the server. String loginTest = (String) this.oisFromServer.readObject(); if (loginTest.equals("Login Failed")) return false; return true; } public void setAccount(String pswd) throws UnknownHostException, IOException, ClassNotFoundException { //Send username and password to the Server. this.oosFromClient.writeObject(this.name); this.oosFromClient.writeObject(pswd); this.oosFromClient.writeObject("update"); } public void getInstructions(Action act) throws UnknownHostException, IOException, ClassNotFoundException { //Send interaction to the Server. this.oosFromClient.writeObject(act.getSubject()); this.oosFromClient.writeObject(act.getVerb()); if (act.getObject() == null) this.oosFromClient.writeObject("none"); else this.oosFromClient.writeObject(act.getObject()); } public void broadcast(String message, String[] listOfPeople) throws UnknownHostException, IOException, ClassNotFoundException { //Tell the server that there is an incoming broadcast. oosFromClient.writeObject("broadcast"); oosFromClient.writeObject(""); oosFromClient.writeObject(""); //Send the broadcast. oosFromClient.writeObject(message); oosFromClient.writeObject(listOfPeople); } //Get the input stream for the client //Note: Used for creating the ReceiveThread public ObjectInputStream getIS() { return this.oisFromServer; } public void closeConnection() throws UnknownHostException, IOException, ClassNotFoundException { //Tell the server to drop the connection with the client this.oosFromClient.writeObject("exit"); this.oosFromClient.writeObject(""); this.oosFromClient.writeObject(""); //Close input and output streams. this.oosFromClient.close(); this.oisFromServer.close(); //Close the connection to the server. this.clientSocket.close(); } }