4 Replies - 1118 Views - Last Post: 19 March 2011 - 10:27 PM Rate Topic: -----

#1 eskemes21  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 25
  • Joined: 26-January 11

how to run this chatting app

Posted 19 March 2011 - 12:06 AM

This is the error:
Exception in thread "main" java.lang.RuntimeException: Syntax: ChatClient <host> <port>


ChatClient Code:

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

public class ChatClient extends Frame implements Runnable {
    // public ChatClient (String title, InputStream i, OutputStream o) ...
    // public void run () ...
    // public boolean handleEvent (Event e) ...
    // public static void main (String args[]) throws IOException ...

    protected DataInputStream i;
    protected DataOutputStream o;
    protected TextArea output;
    protected TextField input;
    protected Thread listener;

    public ChatClient(String title, InputStream i, OutputStream o) {
        super(title);
        this.i = new DataInputStream(new BufferedInputStream(i));
        this.o = new DataOutputStream(new BufferedOutputStream(o));
        setLayout(new BorderLayout());
        add("Center", output = new TextArea());
        output.setEditable(false);
        add("South", input = new TextField());
        pack();
        show();
        input.requestFocus();
        listener = new Thread(this);
        listener.start();
    }

    public void run() {
        try {
            while (true) {
                String line = i.readUTF();
                output.appendText(line + "\n");
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            listener = null;
            input.hide();
            validate();
            try {
                o.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public boolean handleEvent(Event e) {
        if ((e.target == input) && (e.id == Event.ACTION_EVENT)) {
            try {
                o.writeUTF((String) e.arg);
                o.flush();
            } catch (IOException ex) {
                ex.printStackTrace();
                listener.stop();
            }
            input.setText("");
            return true;
        } else if ((e.target == this) && (e.id == Event.WINDOW_DESTROY)) {
            if (listener != null) {
                listener.stop();
            }
            hide();
            return true;
        }
        return super.handleEvent(e);
    }

    public static void main(String args[]) throws IOException {
        if (args.length != 2) {
            throw new RuntimeException("Syntax: ChatClient <host> <port>");
        }
        Socket s = new Socket(args[0], Integer.parseInt(args[1]));
        new ChatClient("Chat " + args[0] + ":" + args[1],
                s.getInputStream(), s.getOutputStream());
    }
}


ChatServer Code:

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

public class ChatServer {
    // public ChatServer (int port) throws IOException ...
    // public static void main (String args[]) throws IOException ...

    public ChatServer(int port) throws IOException {
        ServerSocket server = new ServerSocket(port);
        while (true) {
            Socket client = server.accept();
            System.out.println("Accepted from " + client.getInetAddress());
            ChatHandler c = new ChatHandler(client);
            c.start();
        }
    }

    public static void main(String args[]) throws IOException {
        if (args.length != 1) {
            throw new RuntimeException("Syntax: ChatServer <port>");
        }
        new ChatServer(Integer.parseInt(args[0]));
    }
}


ChatHandler Code:

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

public class ChatHandler extends Thread {
    // public ChatHandler (Socket s) throws IOException ...
    // public void run () ...

    protected Socket s;
    protected DataInputStream i;
    protected DataOutputStream o;

    public ChatHandler(Socket s) throws IOException {
        this.s = s;
        i = new DataInputStream(new BufferedInputStream(s.getInputStream()));
        o = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
    }
    protected static Vector handlers = new Vector();

    public void run() {
        try {
            handlers.addElement(this);
            while (true) {
                String msg = i.readUTF();
                broadcast(msg);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            handlers.removeElement(this);
            try {
                s.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    protected static void broadcast(String message) {
        synchronized (handlers) {
            Enumeration e = handlers.elements();
            while (e.hasMoreElements()) {
                ChatHandler c = (ChatHandler) e.nextElement();
                try {
                    synchronized (c.o) {
                        c.o.writeUTF(message);
                    }
                    c.o.flush();
                } catch (IOException ex) {
                    c.stop();
                }
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: how to run this chatting app

#2 MaverickDavidian  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 67
  • Joined: 15-April 09

Re: how to run this chatting app

Posted 19 March 2011 - 12:14 AM

Wow...just, wow.
Was This Post Helpful? 2
  • +
  • -

#3 eskemes21  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 25
  • Joined: 26-January 11

Re: how to run this chatting app

Posted 19 March 2011 - 12:22 AM

Do you know how to run this program ?
Was This Post Helpful? 0
  • +
  • -

#4 ludjer  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 186
  • Joined: 31-October 08

Re: how to run this chatting app

Posted 19 March 2011 - 07:51 AM

View Posteskemes21, on 19 March 2011 - 09:22 AM, said:

Do you know how to run this program ?

when it is compiled you need to input the parameters when you run it like if its in a jar file
test.jar ipaddress port
eg test.jar 192.168.0.1 4567

if you dont under stand java i would not attempt to try use this "advanced" code try from the basics and go through a few books. Copping code will not get you anywhere trust me. rather learn java from the beginning and then try again.

the error is coming from the following code: as we can see it gets 2 paramaters from String args[]
so args[0] = Host and args[1] will be the port.

Remember string args is the input parameters when the class is run.

public static void main(String args[]) throws IOException {
    if (args.length != 2) {
        throw new RuntimeException("Syntax: ChatClient <host> <port>");
    }
    Socket s = new Socket(args[0], Integer.parseInt(args[1]));
    new ChatClient("Chat " + args[0] + ":" + args[1],
        s.getInputStream(), s.getOutputStream());
}


This post has been edited by ludjer: 19 March 2011 - 07:55 AM

Was This Post Helpful? 1
  • +
  • -

#5 eskemes21  Icon User is offline

  • New D.I.C Head

Reputation: -8
  • View blog
  • Posts: 25
  • Joined: 26-January 11

Re: how to run this chatting app

Posted 19 March 2011 - 10:27 PM

Ohh, thanks for the instructions you give :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1