PDA

View Full Version : Java-Chat Program Help


Kura_kai
09-18-2005, 02:22 AM
I am looking for someone who can help me out in creating a java chat program.If anyone is good with sending/receiving of java fill free to reply. I am not to good with that portion so i am looking for someone to help me throught it/make it while i can do the rest.

Kura_kai
09-19-2005, 12:10 AM
I have made a few steps into what needs to happen and i have come up with a small program. I am stuck now at how to get this to work. What i want to do is have another program come in and change the text in the text area. I have seen this done before in some other examples but i am not able to finish it. If any one can build a sample program that can do this please show me the program. Other than that here is the small program i have done so far.

import java.awt.*;

class ChatWindow extends Frame
{
TextArea textArea;

public ChatWindow(String title, String message)
{
super(title);
this.setLayout(new BorderLayout(15, 15));
textArea = new TextArea(message);
this.add("Center", textArea);
this.pack();
}
public static void main(String[] args)
{
/*Frame f = new Frame("Chat Program");
f.resize(100, 100);
f.show();*/
ChatWindow d = new ChatWindow("Chat Program","Test");
d.show();
}
}

Also if anyone can get the frame thing i commented out for right now to work please fill free to show me how that would be done.

JPM
09-19-2005, 05:46 PM
Something like this maybe:
class AnotherClass
{
ChatWindow cw
public AnotherClass(ChatWindow cw)
{
this.cw =cw;
}

public void setText(String t)
{
cw.textArea.setText(t);
}
}
havent tested it but you get the idea...

Kura_kai
09-24-2005, 12:24 AM
Ok so far i have butchered the program and got it to do what i want. I am taking a sample and sort of bending it to do what i want it to but right now before i try to get the window on the server program to work i want it to send the messages clearly. So far it listens on the port and than sends the message back to the send in reverse. What i want it to do now is instead of sending it to that one computer i want it to send it to everyone client program that is connected to it. I will post the serve script and if anyone can show me how i can set that up please show me or tell me how i can do that. Please respond quickly i would rather have someone show me in a neater way besides me butchering the code and making it sloppy and buggy. Also if anyone could make two threads where one would recieve and send the messages, while the other on will do a readline and send that message to the computer would be nice. I would like to have it where i don't have to run a client one also.

Server.java

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

public class Server extends Thread {
public final static int DEFAULT_PORT = 6789;
protected int port;
protected ServerSocket listen_socket;
protected ThreadGroup threadgroup;
protected List connection_list;
protected Vector connections;
protected Vulture vulture;

// Exit with an error message, when an exception occurs.
public static void fail(Exception e, String msg) {
System.err.println(msg + ": " + e);
System.exit(1);
}

// Create a ServerSocket to listen for connections on; start the thread.
public Server(int port) {
// Create our server thread with a name.
super("Server");
if (port == 0) port = DEFAULT_PORT;
this.port = port;
try { listen_socket = new ServerSocket(port); }
catch (IOException e) { fail(e, "Exception creating server socket"); }
// Create a threadgroup for our connections
threadgroup = new ThreadGroup("Server Connections");

// Create a window to display our connections in
Frame f = new Frame("Server Status");
connection_list = new List();
f.add("Center", connection_list);
f.resize(400, 200);
f.show();

// Initialize a vector to store our connections in
connections = new Vector();

// Create a Vulture thread to wait for other threads to die.
// It starts itself automatically.
vulture = new Vulture(this);

// Start the server listening for connections
this.start();
}

// The body of the server thread. Loop forever, listening for and
// accepting connections from clients. For each connection,
// create a Connection object to handle communication through the
// new Socket. When we create a new connection, add it to the
// Vector of connections, and display it in the List. Note that we
// use synchronized to lock the Vector of connections. The Vulture
// class does the same, so the vulture won't be removing dead
// connections while we're adding fresh ones.
public void run() {
try {
while(true) {
Socket client_socket = listen_socket.accept();
Connection c = new Connection(client_socket, threadgroup,
3, vulture);
// prevent simultaneous access.
synchronized (connections) {
connections.addElement(c);
connection_list.addItem(c.toString());
}
}
}
catch (IOException e) {
fail(e, "Exception while listening for connections");
}
}

// Start the server up, listening on an optionally specified port
public static void main(String[] args) {
int port = 0;
if (args.length == 1) {
try { port = Integer.parseInt(args[0]); }
catch (NumberFormatException e) { port = 0; }
}
new Server(port);
}
}

// This class is the thread that handles all communication with a client
// It also notifies the Vulture when the connection is dropped.
class Connection extends Thread {
static int connection_number = 0;
protected Socket client;
protected Vulture vulture;
protected DataInputStream in;
protected PrintStream out;

// Initialize the streams and start the thread
public Connection(Socket client_socket, ThreadGroup threadgroup,
int priority, Vulture vulture)
{
// Give the thread a group, a name, and a priority.
super(threadgroup, "Connection-" + connection_number++);
this.setPriority(priority);
// Save our other arguments away
client = client_socket;
this.vulture = vulture;
// Create the streams
try {
in = new DataInputStream(client.getInputStream());
out = new PrintStream(client.getOutputStream());
}
catch (IOException e) {
try { client.close(); } catch (IOException e2) { ; }
System.err.println("Exception while getting socket streams: " + e);
return;
}
// And start the thread up
this.start();
}

// Provide the service.
// Read a line, reverse it, send it back.
public void run() {
String line;
StringBuffer revline;
int len;

// Send a welcome message to the client
out.println("Line Reversal Server version 1.0");
out.println("A service of O'Reilly & Associates");

try {
for(;;) {
// read in a line
line = in.readLine();
if (line == null) break;
// reverse it
len = line.length();
revline = new StringBuffer(len);
for(int i = len-1; i >= 0; i--)
revline.insert(len-1-i, line.charAt(i));
// and write out the reversed line
out.println(revline);
}
}
catch (IOException e) { ; }
// When we're done, for whatever reason, be sure to close
// the socket, and to notify the Vulture object. Note that
// we have to use synchronized first to lock the vulture
// object before we can call notify() for it.
finally {
try { client.close(); } catch (IOException e2) { ; }
synchronized (vulture) { vulture.notify(); }
}
}

// This method returns the string representation of the Connection.
// This is the string that will appear in the GUI List.
public String toString() {
return this.getName() + " connected to: "
+ client.getInetAddress().getHostName()
+ ":" + client.getPort();
}
}

// This class waits to be notified that a thread is dying (exiting)
// and then cleans up the list of threads and the graphical list.
class Vulture extends Thread {
protected Server server;

protected Vulture(Server s) {
super(s.threadgroup, "Connection Vulture");
server = s;
this.start();
}

// This is the method that waits for notification of exiting threads
// and cleans up the lists. It is a synchronized method, so it
// acquires a lock on the `this' object before running. This is
// necessary so that it can call wait() on this. Even if the
// the Connection objects never call notify(), this method wakes up
// every five seconds and checks all the connections, just in case.
// Note also that all access to the Vector of connections and to
// the GUI List component are within a synchronized block as well.
// This prevents the Server class from adding a new conenction while
// we're removing an old one.
public synchronized void run() {
for(;;) {
try { this.wait(5000); } catch (InterruptedException e) { ; }
// prevent simultaneous access
synchronized(server.connections) {
// loop through the connections
for(int i = 0; i < server.connections.size(); i++) {
Connection c;
c = (Connection)server.connections.elementAt(i);
// if the connection thread isn't alive anymore,
// remove it from the Vector and List.
if (!c.isAlive()) {
server.connections.removeElementAt(i);
server.connection_list.delItem(i);
i--;
}
}
}
}
}
}