For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2006 > Multiple InputStreamReaders









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Multiple InputStreamReaders
deerhake44

2006-07-31, 3:35 pm

k i made a simple chat server but my prolem is with the buffered/input stream readers. Since i would have more than one client open whenever i do the readLine() command it has to wait for both clients to output something code is below

code:
import java.util.*; import java.io.*; import java.net.*; public class server { public static Socket[] sockets = new Socket[10]; public static String Message; public static int amount = 0; public static void main(String[] args) throws IOException { boolean yes = true; ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(7777); } catch (IOException e) { System.err.println("Could not listen on port: 7777."); System.exit(1); } while (yes) { Socket socket1 = serverSocket.accept(); amount++; sockets[amount-1] = socket1; new multiServerThread(socket1).start(); } serverSocket.close(); } } class multiServerThread extends Thread { private Socket socket = null; private Socket socket2 = null; public multiServerThread(Socket socket1) { socket = socket1; socket2 = socket1; } public void run() { try { BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), socket2.getInputStream())); String message = br.readLine(); if(!message.equals("*close*")) { new outputMessage(message); message = br.readLine(); } } catch(IOException e){} } } class outputMessage { public outputMessage(String message) { server dusnd = new server(); int amt = dusnd.amount; Socket[] socks = dusnd.sockets; if(!message.equals("*close*")) { for(int x = 0; x < amt-1; x++) { try { PrintWriter out = new PrintWriter(socks[x].getOutputStream(), true); out.println(message); }catch(IOException e){} } } } }
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com