Advertisement
Guest User

ChatApplication.java

a guest
Oct 1st, 2013
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.28 KB | None | 0 0
  1. package chatapplication;
  2.  
  3. public class ChatApplication {
  4.    
  5.     public static void main (String args[]){
  6.        
  7.         ChatController  c   =   null;
  8.        
  9.         try {
  10.             c = new ChatController(3666);
  11.            
  12.             System.out.println("connected");
  13.            
  14.             c.startReading();
  15.             c.startWriting();
  16.            
  17.             c.close();
  18.         } catch (java.io.IOException e){
  19.             System.out.format("Couldn't not estabilish a connection (%s)", e.getMessage());
  20.         }
  21.     }
  22.    
  23. }
  24.  
  25. <<<<<<<<<< ChatController class >>>>>>>>>>>>>>>>>>
  26.  
  27. package chatapplication;
  28.  
  29. import java.io.IOException;
  30. import java.net.ServerSocket;
  31. import java.net.Socket;
  32. import java.util.Scanner;
  33.  
  34. public class ChatController extends Thread {
  35.    
  36.     private ServerSocket    server  = null;
  37.     private Host            host    = null;
  38.     private Scanner         reader  = null;
  39.    
  40.     public ChatController(int port) throws IOException{
  41.         this.server = new ServerSocket(port);
  42.         this.reader = new Scanner(System.in);
  43.         this.host = new Host(server.accept());
  44.     }
  45.    
  46.     public ChatController(String address, int port) throws IOException{
  47.         this.host = new Host(new Socket(address, port));
  48.         this.reader = new Scanner(System.in);
  49.     }
  50.    
  51.     @Override
  52.     public void run (){
  53.         while (this.isConnected()){
  54.             System.out.println(this.host.read());
  55.         }
  56.     }
  57.    
  58.     public void startReading () {
  59.         this.start();
  60.     }
  61.    
  62.     public void startWriting (){
  63.         while (this.isConnected()){
  64.             this.host.write(this.reader.nextLine());
  65.         }
  66.     }
  67.    
  68.     public boolean isConnected(){
  69.         return this.host.isConnected();
  70.     }
  71.    
  72.     public void close () throws IOException{
  73.         this.host.close();
  74.         if (server != null){
  75.             server.close();
  76.         }
  77.     }
  78.    
  79. }
  80.  
  81. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Host class >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  82. package chatapplication;
  83.  
  84. import java.util.Scanner;
  85. import java.io.PrintStream;
  86. import java.net.Socket;
  87.  
  88. public class Host {
  89.    
  90.     private Socket              socket;
  91.     private Scanner             reader;
  92.     private PrintStream         writer;
  93.    
  94.     public Host (Socket socket) throws java.io.IOException{
  95.         this.socket = socket;
  96.         this.reader = new Scanner(socket.getInputStream());
  97.         this.writer = new PrintStream(socket.getOutputStream());
  98.     }
  99.    
  100.     /**
  101.      * Writes content to the connected socket.
  102.      * @param text, the content to send to the other socket.
  103.      */
  104.     public void write(String text){
  105.         this.writer.println(text);
  106.         this.writer.flush();
  107.     }
  108.    
  109.     /**
  110.      * Reads content from the connected socket.
  111.      * @return the content read.
  112.      */
  113.     public String read(){
  114.         return this.reader.nextLine();
  115.     }
  116.    
  117.     /**
  118.      * Close the connection.
  119.      * @throws java.io.IOException
  120.      */
  121.     public void close() throws java.io.IOException {
  122.         this.reader.close();
  123.         this.writer.close();
  124.         this.socket.close();
  125.     }
  126.    
  127.     public boolean isConnected (){
  128.         return !(this.socket.isInputShutdown() && this.socket.isOutputShutdown());
  129.     }
  130.  
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement