Advertisement
donut188

TCPClient.java

Dec 8th, 2013
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.46 KB | None | 0 0
  1. import java.net.*;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. public class Client
  6. {
  7.     private ObjectInputStream ois;
  8.     private ObjectOutputStream oos;
  9.     private Socket s;
  10.     private GUIForClient gfc;
  11.     private String server;
  12.     private String username;
  13.     private int port;
  14.  
  15.    
  16.     public Client(String server, int port, String username)
  17.     {
  18.         this(server, port, username, null);
  19.     }
  20.  
  21.     public Client(String server, int port, String username, GUIForClient gfc)
  22.     {
  23.         this.server = server;
  24.         this.port = port;
  25.         this.username = username;
  26.         this.gfc = gfc;
  27.     }
  28.    
  29.     public boolean start()
  30.     {
  31.         try{
  32.             s = new Socket(server, port);
  33.         }
  34.         catch(Exception e) {
  35.             displayMessage("Error connecting to server: " + e);
  36.             return false;
  37.         }
  38.        
  39.         String msg = "Connection accepted on IP address " + s.getInetAddress() + " On port: " + s.getPort();
  40.         displayMessage(msg);
  41.        
  42.         try
  43.         {
  44.             ois = new ObjectInputStream(s.getInputStream());
  45.             oos = new ObjectOutputStream(s.getOutputStream());
  46.         }
  47.         catch(IOException ioe)
  48.         {
  49.             displayMessage("Exception creating new input/output streams: " + ioe);
  50.             return false;
  51.         }
  52.        
  53.         new serverListenerThread().start();
  54.        
  55.         try
  56.         {
  57.             oos.writeObject(username);
  58.         }
  59.         catch(IOException ioe) {
  60.             displayMessage("Exception logging in: " + ioe);
  61.             exit();
  62.             return false;
  63.         }
  64.        
  65.         return true;
  66.     }
  67.    
  68.     private void displayMessage(String msg){
  69.         if(gfc == null){
  70.             System.out.println(msg);
  71.         }
  72.         else
  73.         {
  74.             gfc.appendStuff(msg + "\n");
  75.         }
  76.     }
  77.    
  78.     public void sendMessage(messageType msg) {
  79.         try
  80.         {
  81.             oos.writeObject(msg);
  82.         }
  83.         catch(IOException ioe)
  84.         {
  85.             displayMessage("Exception writing to the server: " + ioe);
  86.         }
  87.     }
  88.    
  89.     private void exit() {
  90.         try
  91.         {
  92.             if(ois != null)
  93.             {
  94.                 ois.close();
  95.             }
  96.         }
  97.         catch(Exception e){
  98.         }
  99.        
  100.         try
  101.         {
  102.             if(oos != null)
  103.             {
  104.                 oos.close();
  105.             }
  106.         }
  107.         catch(Exception e)
  108.         {
  109.         }
  110.        
  111.         try
  112.         {
  113.             if(s != null)
  114.             {
  115.                 s.close();
  116.             }
  117.         }
  118.         catch(Exception e)
  119.         {
  120.         }
  121.        
  122.         if(gfc != null)
  123.         {
  124.             gfc.ifConnectionFails();
  125.         }
  126.        
  127.     }
  128.    
  129.     public static void main(String[] args) {
  130.         int portNum = 7257;
  131.         String address = "localhost";
  132.         String name = "Unknown";
  133.        
  134.         switch(args.length)
  135.         {
  136.             case 0:
  137.             break;
  138.            
  139.             case 1:
  140.             name = args[0];
  141.            
  142.             case 2:
  143.             try
  144.             {
  145.                 portNum = Integer.parseInt(args[1]);
  146.             }
  147.             catch(Exception e)
  148.             {
  149.                 System.out.println("Invalid port number.");
  150.                 System.out.println("Usage of this program is as follows: -> java Client (name) (portNum) (address)");
  151.                 return;
  152.             }
  153.            
  154.             case 3:
  155.             address = args[2];
  156.            
  157.             default:
  158.             System.out.println("Usage of this program is as follows: -> java Client (name) (portNum) (address)");            
  159.             return;
  160.         }
  161.        
  162.         Client client = new Client(address, portNum, name);
  163.        
  164.         if(!client.start())
  165.         {
  166.             return;
  167.         }
  168.        
  169.         Scanner scan = new Scanner(System.in);
  170.        
  171.         while(true)
  172.         {
  173.             System.out.print("Type your message here: -> ");
  174.             String msg = scan.nextLine();
  175.            
  176.             if(msg.equalsIgnoreCase("EXIT"))
  177.             {
  178.                 client.sendMessage(new messageType(messageType.EXIT, ""));
  179.                 break;
  180.             }
  181.             else
  182.             {
  183.                 client.sendMessage(new messageType(messageType.MESSAGE, msg));
  184.             }
  185.         }  
  186.         client.exit();
  187.     }
  188.    
  189.     public class serverListenerThread extends Thread
  190.     {
  191.         public void run()
  192.         {
  193.             while(true)
  194.             {
  195.                 try
  196.                 {
  197.                     String msg = (String) ois.readObject();
  198.                     if(gfc == null)
  199.                     {
  200.                         System.out.println(msg);
  201.                         System.out.print("Type your next message: ");
  202.                     }
  203.                     else
  204.                     {
  205.                         gfc.appendStuff(msg);
  206.                     }
  207.                 }
  208.                 catch(IOException ioe)
  209.                 {
  210.                     displayMessage("Server has closed the connection: " + ioe);
  211.                     if (gfc != null)
  212.                     {
  213.                         gfc.ifConnectionFails();                        
  214.                     }
  215.                     break;
  216.                 }
  217.                 catch(ClassNotFoundException cnfe)
  218.                 {
  219.                 }
  220.             }
  221.         }
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement