chat app


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: chat app

  1. #1
    Join Date
    Jun 2004
    Posts
    1

    chat app

    i found this code on the web its a chat server, wanted to see how it works, but cant seem to get it working
    can someone please tell me why?
    Thanx

    ====================================
    import java.awt.Color;
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import javax.swing.*;

    import java.io.*;
    import java.net.*;

    class ClientWorker implements Runnable{
    private Socket client;
    private JTextArea textArea;

    ClientWorker(Socket client, JTextArea textArea) {
    this.client = client;
    this.textArea = textArea;
    }

    public void run(){
    String line;
    BufferedReader in = null;
    PrintWriter out = null;
    try{
    in = new BufferedReader(new InputStreamReader(client.getInputStream()));
    out = new PrintWriter(client.getOutputStream(), true);
    } catch (IOException e) {
    System.out.println("in or out failed");
    System.exit(-1);
    }

    while(true){
    try{
    line = in.readLine();
    //Send data back to client
    out.println(line);
    textArea.append(line);
    } catch (IOException e) {
    System.out.println("Read failed");
    System.exit(-1);
    }
    }
    }
    }

    class SocketThrdServer extends JFrame{

    JLabel label = new JLabel("Text received over socket:");
    JPanel panel;
    JTextArea textArea = new JTextArea();
    ServerSocket server = null;

    SocketThrdServer(){ //Begin Constructor
    panel = new JPanel();
    panel.setLayout(new BorderLayout());
    panel.setBackground(Color.white);
    getContentPane().add(panel);
    panel.add("North", label);
    panel.add("Center", textArea);
    } //End Constructor

    public void listenSocket(){
    try{
    server = new ServerSocket(4444);
    } catch (IOException e) {
    System.out.println("Could not listen on port 4444");
    System.exit(-1);
    }
    while(true){
    ClientWorker w;
    try{
    w = new ClientWorker(server.accept(), textArea);
    Thread t = new Thread(w);
    t.start();
    } catch (IOException e) {
    System.out.println("Accept failed: 4444");
    System.exit(-1);
    }
    }
    }

    protected void finalize(){
    //Objects created in run method are finalized when
    //program terminates and thread exits
    try{
    server.close();
    } catch (IOException e) {
    System.out.println("Could not close socket");
    System.exit(-1);
    }
    }

    public static void main(String[] args){
    SocketThrdServer frame = new SocketThrdServer();
    frame.setTitle("Server Program");
    WindowListener l = new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    };
    frame.addWindowListener(l);
    frame.pack();
    frame.setVisible(true);
    frame.listenSocket();
    }
    }
    www.guidescentral.com
    Guides Central

  2. #2
    Join Date
    Feb 2004
    Posts
    808
    because it's retarded... it merely repeats back to you what you say, and also puts the text to the main window (without any newlines)

    example:

    compile it
    run it
    open another command box and type TELNET LOCALHOST 4444
    telnet connects
    write some garbage
    garbage is repeated back to you and also appears in the gui
    open another telnet to the same place
    write some garbage
    garbage only returned to this telnet (should go to all telnets? who knows.. )

    either way.. it works. kinda, except, its no good for chatting
    The 6th edict:
    "A thing of reference thing can hold either a null thing or a thing to any thing whose thing is assignment compatible with the thing of the thing" - ArchAngel, www.dictionary.com et al.
    JAR tutorial GridBag tutorial Inherited Shapes Inheritance? String.split(); FTP?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center