Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.*;
- import java.io.*;
- import java.util.*;
- public class Client
- {
- private ObjectInputStream ois;
- private ObjectOutputStream oos;
- private Socket s;
- private GUIForClient gfc;
- private String server;
- private String username;
- private int port;
- public Client(String server, int port, String username)
- {
- this(server, port, username, null);
- }
- public Client(String server, int port, String username, GUIForClient gfc)
- {
- this.server = server;
- this.port = port;
- this.username = username;
- this.gfc = gfc;
- }
- public boolean start()
- {
- try{
- s = new Socket(server, port);
- }
- catch(Exception e) {
- displayMessage("Error connecting to server: " + e);
- return false;
- }
- String msg = "Connection accepted on IP address " + s.getInetAddress() + " On port: " + s.getPort();
- displayMessage(msg);
- try
- {
- ois = new ObjectInputStream(s.getInputStream());
- oos = new ObjectOutputStream(s.getOutputStream());
- }
- catch(IOException ioe)
- {
- displayMessage("Exception creating new input/output streams: " + ioe);
- return false;
- }
- new serverListenerThread().start();
- try
- {
- oos.writeObject(username);
- }
- catch(IOException ioe) {
- displayMessage("Exception logging in: " + ioe);
- exit();
- return false;
- }
- return true;
- }
- private void displayMessage(String msg){
- if(gfc == null){
- System.out.println(msg);
- }
- else
- {
- gfc.appendStuff(msg + "\n");
- }
- }
- public void sendMessage(messageType msg) {
- try
- {
- oos.writeObject(msg);
- }
- catch(IOException ioe)
- {
- displayMessage("Exception writing to the server: " + ioe);
- }
- }
- private void exit() {
- try
- {
- if(ois != null)
- {
- ois.close();
- }
- }
- catch(Exception e){
- }
- try
- {
- if(oos != null)
- {
- oos.close();
- }
- }
- catch(Exception e)
- {
- }
- try
- {
- if(s != null)
- {
- s.close();
- }
- }
- catch(Exception e)
- {
- }
- if(gfc != null)
- {
- gfc.ifConnectionFails();
- }
- }
- public static void main(String[] args) {
- int portNum = 7257;
- String address = "localhost";
- String name = "Unknown";
- switch(args.length)
- {
- case 0:
- break;
- case 1:
- name = args[0];
- case 2:
- try
- {
- portNum = Integer.parseInt(args[1]);
- }
- catch(Exception e)
- {
- System.out.println("Invalid port number.");
- System.out.println("Usage of this program is as follows: -> java Client (name) (portNum) (address)");
- return;
- }
- case 3:
- address = args[2];
- default:
- System.out.println("Usage of this program is as follows: -> java Client (name) (portNum) (address)");
- return;
- }
- Client client = new Client(address, portNum, name);
- if(!client.start())
- {
- return;
- }
- Scanner scan = new Scanner(System.in);
- while(true)
- {
- System.out.print("Type your message here: -> ");
- String msg = scan.nextLine();
- if(msg.equalsIgnoreCase("EXIT"))
- {
- client.sendMessage(new messageType(messageType.EXIT, ""));
- break;
- }
- else
- {
- client.sendMessage(new messageType(messageType.MESSAGE, msg));
- }
- }
- client.exit();
- }
- public class serverListenerThread extends Thread
- {
- public void run()
- {
- while(true)
- {
- try
- {
- String msg = (String) ois.readObject();
- if(gfc == null)
- {
- System.out.println(msg);
- System.out.print("Type your next message: ");
- }
- else
- {
- gfc.appendStuff(msg);
- }
- }
- catch(IOException ioe)
- {
- displayMessage("Server has closed the connection: " + ioe);
- if (gfc != null)
- {
- gfc.ifConnectionFails();
- }
- break;
- }
- catch(ClassNotFoundException cnfe)
- {
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement