views:

334

answers:

2
+1  Q: 

Java Chat Server

A: 

Your first problem is with the parsing of the message from the user.

You loop through the string, and it has one of two options, either the character is a dash or it is invalid.

So, ideally, you should be getting an invalid message for the number of a characters in the username before the dash.

You should use String.indexOf to determine where the dash is, and then split the message into it's two parts, and if the result of indexOf is -1 then it is an invalid message.

James Black
+1  A: 

So far I found this to be a problem, but I don't think this is the only problem..

This will help the NoSuchElementException On around line 90 Change this...

while( (s=fromClient.nextLine().trim()) != null )
{

to this...

while(fromClient.hasNext())
{
   s = fromClient.nextLine().trim();

OK just found another problem in ClientToThread.run()... You are closing the client connections after you send the first message. I commented them both out and it seems to be working a little better.

public void run()
  {
     toClient.println(mesg);
     try {
        //toClient.close();
        //client.close();
     }
     catch (Exception e)  {
        e.printStackTrace();
     }
  }
delux247
ok. I fixed that. I cant believe I didnt catch that. That part was copied from an example my professor gave.
Josh Curren
now I'm getting... java.net.SocketException: Software caused connection abort: recv failed on this line in the ReverseClient...System.out.println( fromServer.readLine() );
delux247
Ok. That took care of the errors... Now the message are sending but they are not sending to the receiver in the order that they were sent... and the receiver doesn't get it until they send a message...
Josh Curren