Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i dont know how to create the main method in the chathandler file that can has GUI.. anybody please help me :((

i get this code at this http://www.acm.org/crossroads/xrds6-1/ovp61.html site. and still dont understand how to make the GUI and main method.please help me

Java
//ChatHandler.java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Vector;

public class ChatHandler extends Thread {
  static Vector handlers = new Vector( 10 );
  private Socket socket;
  private BufferedReader in;
  private PrintWriter out;

    public ChatHandler(Socket socket) throws IOException {
        this.socket = socket;
	in = new BufferedReader(
	    new InputStreamReader(socket.getInputStream()));
	out = new PrintWriter(
	    new OutputStreamWriter(socket.getOutputStream()));
    }
    public void run() {
        String line;
	synchronized(handlers) {
	    handlers.addElement(this);
 // add() not found in Vector class
	}
	try {
	    while(!(line = in.readLine()).equalsIgnoreCase("/quit")) {
		for(int i = 0; i < handlers.size(); i++) {	
			synchronized(handlers) {
		            ChatHandler handler =
			        (ChatHandler)handlers.elementAt(i);
		        handler.out.println(line + "\r");
			handler.out.flush();
			}
		}
	    }
	} catch(IOException ioe) {
	    ioe.printStackTrace();
	} finally {
	    try {
		in.close();
		out.close();
		socket.close();
	    } catch(IOException ioe) {
	    } finally {
		synchronized(handlers) {
		    handlers.removeElement(this);
		}
	    }
	}
    }
}


Java
//server file
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ChatServer {
    public static final int DEFAULT_PORT = 9800;

    public static void main(String[] args) {
	int port = DEFAULT_PORT;
	ServerSocket serverSocket = null;
	Socket socket = null;
	try {
	    if(args.length > 0)
	        port = Integer.parseInt(args[0]);
	} catch(NumberFormatException nfe) {
	    System.err.println("Usage: java ChatServer [port]");
	    System.err.println("Where options include:");
	    System.err.println("\tport the port on which to listen.");
	    System.exit(0);
	}
	try {
	    serverSocket = new ServerSocket(port);
	    while(true) {
	        socket = serverSocket.accept();
	        ChatHandler handler = new ChatHandler(socket);
	        handler.start();
	    }
	} catch(IOException ioe) {
	    ioe.printStackTrace();
	} finally {
	    try {
	        serverSocket.close();
	    } catch(IOException ioe) {
		ioe.printStackTrace();
	    }
	}
    }
}
Posted
Updated 2-Apr-10 17:53pm
v4

This looks like the code from this article[^], so I guess you just need to reread the article to see how it fits together. Note that the server uses an instance of the ChatHandler class.
 
Share this answer
 
Take a look at the Java Tutorials[^], where you will fnd lots of sample code to create the sort of application you are trying to build.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900