Main
Topics
Community
Members
Visitor Number: 8902, Total Hits: 69692.


import java.io.*;
import java.net.*;
import javax.swing.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class imc extends JFrame{
        public host theServer;
        public JTextArea chatArea;
        public JTextField messageField;
        public JButton enter, clear;
        public JScrollPane theScrollPane;
        public static void main(String args[]){
            (new imc()).show();
        }
    public imc(){
                JOptionPane thepane = new JOptionPane();
                String ip = thepane.showInputDialog(null,"Enter the host's IP Address: ","Host IP", JOptionPane.QUESTION_MESSAGE);
                String name = thepane.showInputDialog(null,"Enter your name: ","Screen Name",JOptionPane.QUESTION_MESSAGE);
                theScrollPane = new JScrollPane();
                this.setSize(400,450);
                this.setVisible(false);
                chatArea = new JTextArea("Welcome "+name+".\n", 24, 50);
                chatArea.setEditable(false);
                messageField = new JTextField("<insert message>",50);
                enter = new JButton("Enter");
                clear = new JButton("Clear");
                theScrollPane.getViewport().setView(chatArea);
                this.getContentPane().add(messageField, java.awt.BorderLayout.NORTH);
                this.getContentPane().add(enter, java.awt.BorderLayout.WEST);
                this.getContentPane().add(clear, java.awt.BorderLayout.EAST);
                this.getContentPane().add(theScrollPane,java.awt.BorderLayout.SOUTH);
                this.pack();
                messageField.addActionListener(new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        messageFieldAction(e);
                    }
                });
                enter.addMouseListener(new MouseAdapter() {
                   public void mouseClicked(MouseEvent e){
                       enterClicked(e); 
                   }
                });
                clear.addMouseListener(new MouseAdapter() {
                   public void mouseClicked(MouseEvent e){
                       clearClicked(e); 
                   }
                });        
                addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent evt) {
                        exitForm(evt);
                    }
                });
        System.out.println("Welcome "+name);
        System.out.println("Connecting to "+ip+"...");
        theServer = new host(ip,name, this);
        System.out.println("Connected to host.");
                messageField.selectAll();
                theServer.send(".list");
    }    
        private void exitForm(java.awt.event.WindowEvent evt) {
            theServer.send(".exit");
            theServer.close();
            System.exit(0);
        }
        public void addString(String output){
            chatArea.append(output+"\n");
        }
        
        public void messageFieldAction(ActionEvent e) {
            theServer.send(messageField.getText());
            messageField.selectAll();
        }
        public void enterClicked(MouseEvent e){
            theServer.send(messageField.getText());
            messageField.selectAll();
        }
        public void clearClicked(MouseEvent e){
            chatArea.setText("Screen cleared.\n");
        }
        
}

class host implements Runnable{
    private Socket theServer;
    private BufferedReader sIn;
    private PrintWriter sOut;
    private Thread reader;
        private imc theframe;
    public boolean running;
        //creates socket and input/output to the host server and starts the thread
    public host(String ip, String name, imc aframe){
                theframe = aframe;
        try {
            theServer = new Socket(ip, 45654);
            sOut = new PrintWriter(theServer.getOutputStream(),true);
            sIn = new BufferedReader(new InputStreamReader(
                theServer.getInputStream()));
        }
        catch(UnknownHostException xe){
            System.out.println("Could not find host.");
            return;
        }
        catch(IOException e){
            System.out.println("Error getting I/O from server.");
            return;
        }
        sOut.println(name);
        running = true;
        reader = new Thread(this);
        reader.start();
    }
        //loops through getting lines of text from the server
    public void run(){
                String input;
        while(running){
            try {
                                input = sIn.readLine();
                                if(input==null) break;
                theframe.addString(input);
            }
            catch(IOException e){
                if(running) System.out.println("Error handling server message.");
                break;
            }
        }
    }
        //sends string to the server
    public void send(String output){
        if(running) sOut.println(output);
    }
        //closes the outputs and stops the thread
    public void close(){
                System.out.println("Disconnecting...");
        running = false;
        try {
            theServer.close();
            sIn.close();
            sOut.close();
        }
        catch(IOException e){
            System.out.println("Error disconnecting from server.");
        }
    }
}

Display PI Counter   

Contact me at: tralbrecht@gmail.com