Advertisement
Guest User

ChatClient

a guest
Dec 4th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. import java.io.*;
  6. import java.net.*;
  7.  
  8. public class lxim extends Applet implements Runnable {
  9.  
  10.     public TextField eingabe;
  11.     public Label ausgabe;
  12.  
  13.     public Socket sock;
  14.     public DataOutputStream out;
  15.     public InputStream in;
  16.     public BufferedReader br;
  17.  
  18.     public StringBuffer chat = new StringBuffer();
  19.     public String input = new String();
  20.     public String hattergsagt = new String();
  21.  
  22.     public char[] buffer = new char[1000];
  23.  
  24.     public void start ()
  25.     {
  26.         Thread t = new Thread(this);
  27.         t.start();
  28.     }
  29.     public void init ()
  30.     {
  31.         verbinden ();
  32.         eingabe = new TextField ("");
  33.         ausgabe = new Label ();
  34.        
  35.         setLayout ( new BorderLayout() );
  36.         add (BorderLayout.SOUTH, eingabe);
  37.         add (BorderLayout.NORTH, ausgabe);
  38.        
  39.         eingabe.addActionListener(
  40.                 new ActionListener () {
  41.                     public void actionPerformed (ActionEvent ev) {
  42.                         senden ();
  43.                     }
  44.                 }
  45.         );
  46.     }
  47.  
  48.     public void run () {
  49.         while (true) {
  50.             lesen ();
  51.             try {
  52.                 Thread.sleep(100);
  53.             } catch (InterruptedException e) {
  54.                
  55.             }
  56.         }
  57.     }
  58.    
  59.     public void verbinden () {
  60.         try {
  61.             sock = new Socket ("djtraumwelt.de", 6000);
  62.             out = new DataOutputStream (sock.getOutputStream() );
  63.             //out.flush();
  64.            
  65.             in = sock.getInputStream();
  66.             InputStreamReader inr = new InputStreamReader(in);
  67.             br = new BufferedReader(inr);
  68.            
  69.         } catch (UnknownHostException e) {
  70.            
  71.         } catch (IOException e) {
  72.            
  73.         }
  74.     }
  75.    
  76.     public void senden () {
  77.         hattergsagt = "SAY " + eingabe.getText() + "\0";
  78.         try {
  79.             out.write (hattergsagt.getBytes());
  80.             out.flush();
  81.         } catch (IOException e) {
  82.            
  83.         }
  84.         eingabe.setText ("");
  85.     }
  86.    
  87.     public void lesen () {
  88.         try {
  89.             int i;
  90.             if (br.ready() ) {
  91.                 i = br.read(buffer, 0, 1000);
  92.                 chat.append( buffer );
  93.                 ausgabe.setText (chat.toString() );
  94.             }
  95.         } catch (IOException e) {
  96.            
  97.         }
  98.     }
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement