I am making a chat client/server app. When i run the program only the client that sent the message is getting it back from the server. I think it is sitting on the userInput line waiting for keyboard input, how do i make it continue to loop?
Thanks and i hope i put this in the right forum code(server and Client) is below
Client Code
|
Code:
|
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author bcoates
*/
import java.net.*;
import java.io.*;
public class chatClient {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Socket echoSocket=null;
PrintWriter out=null;
BufferedReader in = null;
// TODO code application logic here
try
{
echoSocket = new Socket ("localhost", 8888);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));
}
catch(UnknownHostException e)
{
System.out.println("Dont know about that host");
}
catch(IOException e)
{
System.out.println("something blew up:"+e);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String userInput=null;
String serverOut=null;
while(true)
{
try
{
userInput=stdIn.readLine();
}
catch(IOException e)
{
System.out.println("something blew up" +e);
System.exit(1);
}
if(userInput!=null)
out.println(userInput);
try
{
serverOut = in.readLine();
}
catch(IOException e)
{
System.out.println("something blew up" +e);
System.exit(1);
}
if(serverOut!=null)
System.out.println(serverOut);
}
}
} |
Server Code
|
Code:
|
/**
*
* @author bcoates
*/
import java.net.*;
import java.io.*;
import java.util.*;
public class multiServer
{
// HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
static protected List<PrintWriter> writers = new ArrayList<PrintWriter>();
public static void main(String[] args)
{
int i=1;
try
{
ServerSocket servsock = new ServerSocket(8888, 5);
Socket incoming;
System.out.println("The chat server is running.");
while(true)
{
incoming = servsock.accept();
multiServerThread connection = new multiServerThread(incoming);
Thread t = new Thread(connection);
t.start();
}
}
catch(IOException e)
{
System.out.println("couldnt make socket");
}
}
}
class multiServerThread implements Runnable
{
Socket incoming;
PrintWriter out=null;
PrintWriter broad=null;
BufferedReader in = null;
String cliString=null;
int id;
multiServerThread(Socket incoming)
{
this.incoming=incoming;
//this.id=id;
}
public void run()
{
try
{
out = new PrintWriter(incoming.getOutputStream(), true);
//multiServer.writers.add(out);
System.out.println("Number of Clients: " +multiServer.writers.size());
in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
multiServer.writers.add(out);
while(true)
{
try
{
cliString = in.readLine();
if(cliString !=null)
{
System.out.println("Got message: "+cliString);
for (int i=0; i<multiServer.writers.size();i++)
{
broad=multiServer.writers.get(i);
broad.println(cliString);
}
//out.println(cliString);
}
}
catch (IOException e)
{
System.out.println("yup it blew up here"+e);
}
}
} catch(IOException e)
{
System.out.print("boom goes the dynamite"+ e);
}
}
} |