PJF16

Chat Client + Thread

Jan 22nd, 2014
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.69 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package client;
  6.  
  7. import java.net.*;
  8. import java.io.*;
  9.  
  10. public class ChatClient implements Runnable
  11. {  private Socket socket              = null;
  12.    private Thread thread              = null;
  13.    private DataInputStream  console   = null;
  14.    private DataOutputStream streamOut = null;
  15.    private ChatClientThread client    = null;
  16.  
  17.    public ChatClient(String serverName, int serverPort)
  18.    {  System.out.println("Establishing connection. Please wait ...");
  19.       try
  20.       {  socket = new Socket(serverName, serverPort);
  21.          System.out.println("Connected: " + socket);
  22.          start();
  23.       }
  24.       catch(UnknownHostException uhe)
  25.       {  System.out.println("Host unknown: " + uhe.getMessage()); }
  26.       catch(IOException ioe)
  27.       {  System.out.println("Unexpected exception: " + ioe.getMessage()); }
  28.    }
  29.    public void run()
  30.    {  while (thread != null)
  31.       {  try
  32.          {  streamOut.writeUTF(console.readLine());
  33.             streamOut.flush();
  34.          }
  35.          catch(IOException ioe)
  36.          {  System.out.println("Sending error: " + ioe.getMessage());
  37.             stop();
  38.          }
  39.       }
  40.    }
  41.    public void handle(String msg)
  42.    {  if (msg.equals(".bye"))
  43.       {  System.out.println("Good bye. Press RETURN to exit ...");
  44.          stop();
  45.       }
  46.       else
  47.          System.out.println(msg);
  48.    }
  49.    public void start() throws IOException
  50.    {  console   = new DataInputStream(System.in);
  51.       streamOut = new DataOutputStream(socket.getOutputStream());
  52.       if (thread == null)
  53.       {  client = new ChatClientThread(this, socket);
  54.          thread = new Thread(this);                  
  55.          thread.start();
  56.       }
  57.    }
  58.    public void stop()
  59.    {  if (thread != null)
  60.       {  thread.stop();  
  61.          thread = null;
  62.       }
  63.       try
  64.       {  if (console   != null)  console.close();
  65.          if (streamOut != null)  streamOut.close();
  66.          if (socket    != null)  socket.close();
  67.       }
  68.       catch(IOException ioe)
  69.       {  System.out.println("Error closing ..."); }
  70.       client.close();  
  71.       client.stop();
  72.    }
  73.    public static void main(String args[])
  74.    {  ChatClient client = null;
  75.       if (args.length != 2)
  76.          System.out.println("Usage: java ChatClient host port");
  77.       else
  78.          client = new ChatClient(args[0], Integer.parseInt(args[1]));
  79.    }
  80. }
  81.  
  82.  
  83.  
  84. /*
  85.  * To change this template, choose Tools | Templates
  86.  * and open the template in the editor.
  87.  */
  88. package client;
  89.  
  90. /**
  91.  *
  92.  * @author Philipp
  93.  */
  94. import java.net.*;
  95. import java.io.*;
  96.  
  97. public class ChatClientThread extends Thread
  98. {  private Socket           socket   = null;
  99.    private ChatClient       client   = null;
  100.    private DataInputStream  streamIn = null;
  101.  
  102.    public ChatClientThread(ChatClient _client, Socket _socket)
  103.    {  client   = _client;
  104.       socket   = _socket;
  105.       open();  
  106.       start();
  107.    }
  108.    public void open()
  109.    {  try
  110.       {  streamIn  = new DataInputStream(socket.getInputStream());
  111.       }
  112.       catch(IOException ioe)
  113.       {  System.out.println("Error getting input stream: " + ioe);
  114.          client.stop();
  115.       }
  116.    }
  117.    public void close()
  118.    {  try
  119.       {  if (streamIn != null) streamIn.close();
  120.       }
  121.       catch(IOException ioe)
  122.       {  System.out.println("Error closing input stream: " + ioe);
  123.       }
  124.    }
  125.    public void run()
  126.    {  while (true)
  127.       {  try
  128.          {  client.handle(streamIn.readUTF());
  129.          }
  130.          catch(IOException ioe)
  131.          {  System.out.println("Listening error: " + ioe.getMessage());
  132.             client.stop();
  133.          }
  134.       }
  135.    }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment