RuneScape
RuneScape

Reputation: 75

Simple socket-level program - client/server

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

Upvotes: 0

Views: 396

Answers (2)

L. Cornelius Dol
L. Cornelius Dol

Reputation: 64065

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).

Upvotes: 0

greedybuddha
greedybuddha

Reputation: 7507

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

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

Upvotes: 2

Related Questions