Guest
Public paste!

Untitled

By: a guest | Mar 15th, 2010 | Syntax: None | Size: 2.58 KB | Hits: 71 | Expires: Never
Copy text to clipboard
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package simplechatclient;
  7.  
  8. import java.io.*;
  9. import java.net.*;
  10. import java.util.*;
  11. import javax.swing.*;
  12. import java.awt.*;
  13. import java.awt.event.*;
  14. // @author q
  15.  
  16. public class SimpleChatClient {
  17.   JTextArea incoming;
  18.   JTextField outgoing;
  19.   BufferedReader reader;
  20.   PrintWriter writer;
  21.   Socket sock;
  22.  
  23.   public static void main(String[] args) {
  24.     SimpleChatClient client = new SimpleChatClient();
  25.     client.go();
  26.   }
  27.  
  28.   public void go() {
  29.     JFrame frame = new JFrame("Clienten");
  30.     JPanel mainPanel = new JPanel();
  31.     incoming = new JTextArea(15, 50);
  32.     incoming.setLineWrap(true);
  33.     incoming.setWrapStyleWord(true);
  34.     incoming.setEditable(false);
  35.     JScrollPane qScroller = new JScrollPane(incoming);
  36.     qScroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
  37.     qScroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
  38.     outgoing = new JTextField(20);
  39.     JButton sendButton = new JButton("Spotta");
  40.     sendButton.addActionListener(new SendButtonListener());
  41.     mainPanel.add(qScroller);
  42.     mainPanel.add(outgoing);
  43.     mainPanel.add(sendButton);
  44.     setUpNetworking();
  45.  
  46.     Thread readerThread= new Thread(new IncomingReader());
  47.     readerThread.start();
  48.  
  49.     frame.getContentPane().add(BorderLayout.CENTER, mainPanel);
  50.     frame.setSize(640, 400);
  51.     frame.setVisible(true);
  52.   }
  53.  
  54.   private void setUpNetworking() {
  55.     try {
  56.       sock = new Socket("172.16.31.65", 443);
  57.       InputStreamReader streamReader = new InputStreamReader(sock.getInputStream());
  58.       reader = new BufferedReader(streamReader);
  59.       writer = new PrintWriter(sock.getOutputStream());
  60.       System.out.println("Network established!");
  61.     } catch(IOException ex) {
  62.       ex.printStackTrace();
  63.     }
  64.   }
  65.  
  66.   public class SendButtonListener implements ActionListener {
  67.     public void actionPerformed(ActionEvent av) {
  68.       try {
  69.         writer.println(outgoing.getText());
  70.         writer.flush();
  71.       } catch(Exception ex) {
  72.         ex.printStackTrace();
  73.       }
  74.       outgoing.setText("");
  75.       outgoing.requestFocus();
  76.     }
  77.   }
  78.  
  79.   public class IncomingReader implements Runnable {
  80.     public void run() {
  81.       String message;
  82.       try {
  83.         while ((message = reader.readLine()) != null) {
  84.           System.out.println("read" + message);
  85.           incoming.append(message + "\n");
  86.         }
  87.       } catch(Exception ex) {
  88.         ex.printStackTrace();
  89.       }
  90.     }
  91.   }
  92. }