0

The following is a simple socket-level program. Once a connection is established, the server speaks for as long as he/she wants provided that the message does not end in a period - then the client can speak for as long as he/she wants provided that the conversation does not end in a period - the conversation alternates like this until someone shuts the program down --

I can't get the until there is a period part down ... Else, I would not have a problem - there would be a one-one interaction

Once one person writes, it stays their turn forever ...

import java.io.*;
import java.net.*;

public class ChatterServer {

    final static int SERVER_PORT = 3333;
    public static void main(String [] args) throws Exception {

        ServerSocket serverSocket = new ServerSocket(SERVER_PORT); 
        System.err.println("Waiting for a client");
        Socket clientSocket = serverSocket.accept();

        System.out.println("Connection requested from: " + clientSocket.getLocalAddress());

        PrintStream toClient = new PrintStream(clientSocket.getOutputStream(), true);
        BufferedReader fromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

        toClient.println("Whatcha want?"); 
        String incoming = fromClient.readLine();

        while(incoming != null) {

            System.out.println(incoming);
            System.out.print("Your turn>"); 
            String myReply="";

            //this part does not work
            while ( myReply.substring( myReply.length() ) .equals(".") == false){

                myReply = keyboard.readLine(); 
                toClient.println(myReply); 
            }

            incoming = fromClient.readLine();
        }
    }
}

import java.io.*;
import java.net.*;

public class ChatterClient {
    final static int SERVER_PORT = 3333;
    public static void main(String [] args) throws Exception {

        Socket serverSocket = new Socket(args[0], SERVER_PORT);
        PrintStream toServer =
                new PrintStream(serverSocket.getOutputStream(), true);
        BufferedReader fromServer = new BufferedReader(new InputStreamReader(serverSocket.getInputStream()));
        BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));

        String incoming = fromServer.readLine();

        while(incoming != null) { 
            System.out.println(incoming);
            System.out.print("Your turn>"); 
            String myReply="";

            while ( myReply.substring( myReply.length() ) .equals(".") == false){
                myReply = keyboard.readLine(); 
                toServer.println(myReply); 
            }//end while

            incoming = fromServer.readLine();
        }//end while
    }//end main

}//end ChatterClient class

2 Answers 2

2

Better would be to use the endsWith method. It will work just fine, and is cleaner to look at.

 while (!myReply.endsWith(".")){...}
0

While I agree with using String.endsWith the actual problem in the code is that someString.substring(someString.length()) will always be an empty string. You wanted someString.substring(someString.length()-1).

2
  • That change yields the following error message:Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1943) at java.lang.String.substring(String.java:1916) at ChatterClient.main(ChatterClient.java:21)
    – RuneScape
    May 17, 2013 at 23:48
  • You must, of course, validate that you have some input - i.e. someString.length()>0. May 18, 2013 at 1:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.