J
JS
Hi everyone. I'm trying to write a 20 client chat server which is not web
based (the only reason I say that is because all the examples I have found
so far are applets, JSP etc)
In the client side I'm trying to listen for input from the user and for
messages from the server at the same time. Well almost, there is a SoTimeout
of 50milliseconds which swaps between listening from the System.in and the
server.
A similar story on the server side but this calls methods for listening for
new clients connecting and messages from the existing clients. The server
has this code:
public void receive()
{
System.out.println("Waiting for new clients");
try
{
socket = serverSocket.accept();
socket.setSoTimeout(50);
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String clientSentence = inFromClient.readLine();
System.out.println("someone said "+clientSentence);
process(clientSentence, socket);
}
catch(IOException e)
{
System.out.println(e);
System.out.println("A new client could not be found");
listenFromClients();
}
listenFromClients();
}
public void listenFromClients()
{
System.out.println("Waiting for a known client to speak");
for(int i = 0; i < clients.size(); i++)
{
Tuple t = (Tuple)clients.get(i);
socket = t.getSocket();
try
{
socket.setSoTimeout(50);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String clientSentence = in.readLine();
System.out.println(t.getName()+ " said "+clientSentence);
process(clientSentence, socket);
}
catch(IOException e)
{
System.out.println(e);
System.out.println("Known client "+i+" timed out");
continue;
}
}
receive();
}
and the client has this:
public void start()
{
String first = connect();
if(first != null)
{
send(first);
}
while(true)
{
receive();
getClientInput();
}
}
public String connect()
{
try
{
String toServer = InetAddress.getLocalHost().getHostName();
return ("connect: "+toServer+" "+name+"\n");
}
catch(IOException e)
{
System.out.println(e);
System.out.println("The host name of your computer could not be
found");
System.out.println("The system will now exit");
System.exit(3);
}
return null;
}
public void send(String message)
{
try
{
DataOutputStream outToServer = new
DataOutputStream(socket.getOutputStream());
outToServer.writeBytes(message);
receive();
}
catch(IOException e)
{
System.out.println(e);
System.out.println("The message could not be sent");
}
}
public void receive()
{
try
{
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String sentence = inFromServer.readLine();
process(sentence);
}
catch(IOException e)
{
System.out.println(e);
System.out.println("The message could not be received hello");
//getClientInput();
}
}
public void getClientInput()
{
try
{
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter some text....");
String in = inFromClient.readLine();
send("message: "+name+": "+in);
}
catch(IOException e)
{
System.out.println(e);
System.out.println("That message could not be read from you");
//receive();
}
}
The socket keeps timing out and the errors are caught but neither class
seems to carry on listening with the other method. I would use threads but
Sockets and networks are new to me let alone threads as well, although it
might be possible with some collaboration.
There are a lot of System.out.println() statments here purely so we could
see what was happening. Now in theory the server should print out a new line
each time it swaps methods but it doesnt.
Any help is greatly appreciated and if you need any further details then
feel free to ask.
Thanks a lot everyone
JS
based (the only reason I say that is because all the examples I have found
so far are applets, JSP etc)
In the client side I'm trying to listen for input from the user and for
messages from the server at the same time. Well almost, there is a SoTimeout
of 50milliseconds which swaps between listening from the System.in and the
server.
A similar story on the server side but this calls methods for listening for
new clients connecting and messages from the existing clients. The server
has this code:
public void receive()
{
System.out.println("Waiting for new clients");
try
{
socket = serverSocket.accept();
socket.setSoTimeout(50);
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String clientSentence = inFromClient.readLine();
System.out.println("someone said "+clientSentence);
process(clientSentence, socket);
}
catch(IOException e)
{
System.out.println(e);
System.out.println("A new client could not be found");
listenFromClients();
}
listenFromClients();
}
public void listenFromClients()
{
System.out.println("Waiting for a known client to speak");
for(int i = 0; i < clients.size(); i++)
{
Tuple t = (Tuple)clients.get(i);
socket = t.getSocket();
try
{
socket.setSoTimeout(50);
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String clientSentence = in.readLine();
System.out.println(t.getName()+ " said "+clientSentence);
process(clientSentence, socket);
}
catch(IOException e)
{
System.out.println(e);
System.out.println("Known client "+i+" timed out");
continue;
}
}
receive();
}
and the client has this:
public void start()
{
String first = connect();
if(first != null)
{
send(first);
}
while(true)
{
receive();
getClientInput();
}
}
public String connect()
{
try
{
String toServer = InetAddress.getLocalHost().getHostName();
return ("connect: "+toServer+" "+name+"\n");
}
catch(IOException e)
{
System.out.println(e);
System.out.println("The host name of your computer could not be
found");
System.out.println("The system will now exit");
System.exit(3);
}
return null;
}
public void send(String message)
{
try
{
DataOutputStream outToServer = new
DataOutputStream(socket.getOutputStream());
outToServer.writeBytes(message);
receive();
}
catch(IOException e)
{
System.out.println(e);
System.out.println("The message could not be sent");
}
}
public void receive()
{
try
{
BufferedReader inFromServer = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
String sentence = inFromServer.readLine();
process(sentence);
}
catch(IOException e)
{
System.out.println(e);
System.out.println("The message could not be received hello");
//getClientInput();
}
}
public void getClientInput()
{
try
{
BufferedReader inFromClient = new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter some text....");
String in = inFromClient.readLine();
send("message: "+name+": "+in);
}
catch(IOException e)
{
System.out.println(e);
System.out.println("That message could not be read from you");
//receive();
}
}
The socket keeps timing out and the errors are caught but neither class
seems to carry on listening with the other method. I would use threads but
Sockets and networks are new to me let alone threads as well, although it
might be possible with some collaboration.
There are a lot of System.out.println() statments here purely so we could
see what was happening. Now in theory the server should print out a new line
each time it swaps methods but it doesnt.
Any help is greatly appreciated and if you need any further details then
feel free to ask.
Thanks a lot everyone
JS