I'm going to give you a rough outline, you can fill in the rest of the details.
Up vote 0 down vote favorite 1 share g+ share fb share tw.
I am having a problem implementing a simple multi threaded server in Java. My idea is for a server to broadcast a message to all clients(without the use of a multicast). For this I am implementing a broadcast method.
This method will use a for loop, and loop through each client Thread, which I have stored in a collection. It will call each threads send(), which outputs writeUTF(). My problem is for 2 clients A+B.
A outputs: hello B outputs: hi there B will not recieve hello, and when B type again, it will the recieve A's message. Code example: import java.net. *; import java.io.
; import java.util. ; public class ServerThreadHandler extends Thread { private AuctionServer server = null; private Socket socket = null; private String name = null; private int ID = -1; private DataInputStream dataIn = null; public DataOutputStream dataOut = null; private Thread thread; protected static Vector handlers = new Vector(); // reason server is used here is because ian was calling a server method broadcast // from inside the public ServerThreadHandler(AuctionServer server, Socket socket, String name) throws IOException{ this. Server = server; this.
Socket = socket; this. Name = name; dataIn = new DataInputStream( new BufferedInputStream(socket.getInputStream())); dataOut = new DataOutputStream(socket.getOutputStream()); } // handles a specific client. Public void run(){ System.out.
Println("Server running.."); while(true){ try{ // broadcast to all clients. This will only be one client in this case. Server.
Broadcast(dataIn.readUTF()); int pause = (int)(Math.random() * 3000); Thread. Sleep(pause); } catch(IOException e){ System.out. Println(e.getMessage()); } catch(InterruptedException e){ System.out.
Println(e.getMessage()); } } } public void send(String msg){ try{ dataOut. WriteUTF(msg); dataOut.flush(); } catch(IOException e){ } } Server code: // broadcast this to clients. Public void broadcast(String msg){ for(int I = 0; I Send(msg); } } Where clients is private ServerThreadHandler clients = new ServerThreadHandler3; java multithreading server client link|improve this question edited Nov 21 '11 at 19:42 asked Nov 21 '11 at 18:17Jnanathan635 56% accept rate.
4 Why don't you give us an example of what you've written so far, and tell us where it's crashing. – Jon Skeet Nov 21 '11 at 18:22 Sure, to clarify I have gotten the server to accept multiple clients, but now the broadcast is not to each client, only to one. And example is in the code above.
– Jnanathan Nov 21 '11 at 18:45 2 You haven't provided any code... – Jon Skeet Nov 21 '11 at 18:45 Ok, I have tried to edit the post to provide code but have been getting errors each time. I will continue to try so bear with me if you would. I have also updated the problem question.
– Jnanathan Nov 21 '11 at 19:16 You are creating a thread inside the run of another thread (?), but never starting it. This is very confusing... – Tudor Nov 21 '11 at 19:39.
I'm going to give you a rough outline, you can fill in the rest of the details: public class Server { private ServerSocket serverSocket; private List clientSockets; // constructor(s) private class ThreadTask implements Runnable { private Socket clientSocket; public ThreadTask(Socket clientSocket) { this. ClientSocket = clientSocket; } @Override public void run() { while(true) { // read message from clientSocket for(Socket peerSocket: clientSockets) { // send message on peerSocket } } } } private void acceptConnections() { while(true) { try { Socket clientSocket = serverSocket.accept(); clientSockets. Add(clientSocket); Thread t = new Thread(new ThreadTask(clientSocket)); t.start(); } catch (IOException e) { e.printStackTrace(); } } } }.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.