Very simple chat - problem with message sending

A

Ali

I'm trying to write a really basic chat program that allows 2 client
programs to send messages to each other via a server. I've managed to
write the code so that both clients can connect to the server and send
and recieve messages to/from the server but when I send a message from
one client I can't get the message to appear in the second client. I
think it's something to do with threads but I don't know what! I've
been looking at the source code for loads of different chat programs
online but they all seem far too complicated and I can't figure out
what I'm doing wrong.

Can anyone give me any pointers as to what I need to do? Any help will
be very gratefully received.

This is the code I have so far...

ThreadedChatServer.java

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

public class ThreadedChatServer
{

public static void main(String[] args) throws IOException
{
ServerSocket server = null;
boolean listening = true;

try
{
server = new ServerSocket(8000);
}
catch(IOException e)
{
System.out.println("Error connecting " + e);
}

while (listening)
{
new ChatHandler(server.accept()).start();
}

server.close();
}
}
**************

ChatHandler.java

import java.io.*;
import java.net.*;
import javax.swing.*;

public class ChatHandler extends Thread
{
private Socket connectToClient = null;
private BufferedReader in;
private PrintWriter out;
private String message = "";
private String reply = "";

public ChatHandler(Socket connectToClient)
{
this.connectToClient = connectToClient;
}

public void run()
{
try
{
in = new BufferedReader(new
InputStreamReader(connectToClient.getInputStream()));
out = new PrintWriter(connectToClient.getOutputStream(),true);

// A variable that will say if the chat is over //
boolean finished = false;

while (!finished)
{
try
{
message = in.readLine();
out.println(message);
Thread.sleep(100);
}
catch(IOException e)
{
System.out.println("Error connecting " + e);
}
catch(InterruptedException i)
{
System.out.println("Thread interrupted " + i);
}
}

try
{
in.close();
out.close();
connectToClient.close();
}
catch(IOException e)
{
System.out.println("Error connecting " + e);
}
}
catch(IOException e)
{
System.out.println("Error connecting " + e);
}
}
}
******************

ChatClient.java

.... code for text areas etc...
....press send message button then ...

// Sends the message to the server //
out.println(userName + "> " + messageField.getText());

// Prints out the server's reply //
chatHistory.append("\n" + in.readLine());

// Clears the message field //
messageField.setText("");

.... etc ...


Thanks,
AyCee :eek:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top