4 Replies - 6432 Views - Last Post: 03 May 2011 - 06:50 AM Rate Topic: -----

#1 TopdeK  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 02-April 11

MultiThreaded Chat Server

Posted 03 May 2011 - 04:48 AM

Hi,

I'm trying to write the server side of a chat application but I'm failing to get it broadcasting to all clients. It will broadcast to individual clients independently but not to everyone. I'm storing the clients in an ArrayList. I also have a class called ChatHandler which I'm using as a middle ground to relay the message to all. It takes in a global Queue where all the chat history is stored, then removes and broadcasts the most recent message to all clients. It won't work though. I'm sure I've made a simple error and probably made things more complicated than necessary but can anyone offer a solution?
I'm still only learning ...

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

class ChatHandler
{
	private String theMessage;

	public ChatHandler(Queue<String> q)
	{
		theMessage = q.remove();
	}
	
	public synchronized void shoutToAll()
	{
		Iterator itr = FEchoServerMulti.clientsConnected.iterator();
		while(itr.hasNext())
		{
			Speaker temp = (Speaker) itr.next();
			temp.shout(theMessage);
		}
	}
}


class Speaker extends Thread
{
	Socket inbound;
	int identity;
	String username;
	PrintWriter out;
	BufferedReader in;

	Speaker(Socket inbound, int identity)
	{
		this.inbound = inbound;
		this.identity = identity;
		username = "User " + identity + ": ";

		try{
			in = new BufferedReader(new InputStreamReader(inbound.getInputStream()));
			out = new PrintWriter(inbound.getOutputStream());
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}

	public void shout(String theMessage)
	{
		if(out != null){
			out.println(username + theMessage);
			out.flush();
		}
	}

	public void run()
	{
		try{
			while(true)
			{
					String message = in.readLine();
					if(message == "exit")
						break;

					FEchoServerMulti.chat.add(message);
					ChatHandler clone = new ChatHandler(FEchoServerMulti.chat);
					clone.shoutToAll();
			}
			inbound.close();
			FEchoServerMulti.clientsConnected.remove(this);
		}
		catch(IOException e)
		{
			e.printStackTrace();
		}
	}
}

public class FEchoServerMulti 
{
	static protected ArrayList<Speaker> clientsConnected = new ArrayList<Speaker>();
	static protected Queue<String> chat = new LinkedList<String>();


	public static void main(String[] args) throws IOException
	{
		ServerSocket serverSocket = null;		
		try 
		{
			serverSocket = new ServerSocket(7777);
			int i = 1;
			while(true)
			{
				Socket incomingRequest = serverSocket.accept();
				Speaker bc = new Speaker(incomingRequest, i);
				clientsConnected.add(bc);				
				bc.start();
				i++;
			}
			 
		}
		catch(IOException e)
		{
		   System.err.println("Could not listen on port: 7777.");
    	   System.exit(-1);
		}
	}
}



Is This A Good Question/Topic? 1
  • +

Replies To: MultiThreaded Chat Server

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: MultiThreaded Chat Server

Posted 03 May 2011 - 05:33 AM

Ya, you are making more complicated then required
There are tutorials and examples in this forum
Was This Post Helpful? 1
  • +
  • -

#3 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2734
  • View blog
  • Posts: 11,514
  • Joined: 20-September 08

Re: MultiThreaded Chat Server

Posted 03 May 2011 - 06:44 AM

I must say i'm inclined to agree with pbl. For instance, why is a class called Speaker given listening functionality? Incidentally that functionality doesn't look like it will work - i see nothing calling start() on the Thread (which is what causes run() to be called)
Was This Post Helpful? 1
  • +
  • -

#4 qxc  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 22-April 11

Re: MultiThreaded Chat Server

Posted 03 May 2011 - 06:48 AM

I agree with g00se and pbl, you must rethink your approach.
Was This Post Helpful? 0
  • +
  • -

#5 v0rtex  Icon User is offline

  • Caffeine: db "Never Enough!"
  • member icon

Reputation: 223
  • View blog
  • Posts: 773
  • Joined: 02-June 10

Re: MultiThreaded Chat Server

Posted 03 May 2011 - 06:50 AM

I found WolfCoder's tutorial quite good, It should be able to help you.
WolfCoder's tutorial: Creating a server to serve clients
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1