-2

I am working on a problem that will create a TCP server and client using sockets. For the client code, my objective is to repeatedly prompt the user to enter a sentence S, send the sentence S to the server, receive the response from the server, and display the message received and the round trip time expressed in milliseconds. On the server, my objective is to create a TCP server socket, wait for a client to connect, receive a message, display it with the IP address and port # of the client, capitalize the message, display the message, and echo back the "capitalized" message.

I am trying to use a while (!(input.equals("done"){ ...do something }, however, whatever I do is getting stuck in an infinite loop. I hope its something simple I am just overlooking, but I don't see it.

TCPServer.java

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

public class myFirstTCPServer {
    public static void main(String[] args) throws IOException {

        int servPort = 4999;
        ServerSocket Sy = new ServerSocket(servPort);
        Socket servSocket = Sy.accept();

        InputStreamReader in = new InputStreamReader(servSocket.getInputStream());
        BufferedReader bf = new BufferedReader(in);
        String str = bf.readLine();

        while (!(str.equals("done"))){

            System.out.println("client connected");


            InetAddress address = InetAddress.getLocalHost();
            String ip = address.getHostAddress();
            System.out.println("IP: " + ip);
            System.out.println("Port: " + servPort);

            System.out.println("Message from client: " + str.toUpperCase());

            PrintWriter pr = new PrintWriter(servSocket.getOutputStream());
            pr.println(str);
            pr.flush();
        }
        servSocket.close();
    }
}

TCPClient.java

import java.net.*;
import java.io.*;
import java.util.Scanner;

public class myFirstTCPClient {
    public static void main(String[] args) throws IOException {
        String S;
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a sentence");
        S = input.nextLine();
        Socket clntSocket = new Socket(InetAddress.getLocalHost(), 4999);

        while (!(S.equals("done"))){
            double sent = System.nanoTime();
            PrintWriter pr = new PrintWriter(clntSocket.getOutputStream());
            pr.println(S);
            pr.flush();

            InputStreamReader in = new InputStreamReader(clntSocket.getInputStream());
            BufferedReader bf = new BufferedReader(in);

            String str = bf.readLine();
            System.out.println("Message from server: " + str);
            double received = System.nanoTime();
            double total = received - sent;
            System.out.println("Round Trip Time: " + (total/1000000.0));
        }
        clntSocket.close();
    }
}
2
  • 1
    the value of str never changes, so maybe put str = bf.readLine(); into your loop. You have a similar problem with the client too. May 29, 2019 at 1:53
  • 1
    You don't seem to be carefully thinking about what goes inside the loop versus what should be outside it in TCPServer May 29, 2019 at 1:54

1 Answer 1

-2

you need to move reader into the while loop. Because this is where server waits for reading clients input.

    public class myFirstTCPServer {
    public static void main(String[] args) throws IOException {

        int servPort = 4999;
        ServerSocket Sy = new ServerSocket(servPort);
        Socket servSocket = Sy.accept();
        System.out.println("client connected");

        InputStreamReader in = new InputStreamReader(servSocket.getInputStream());
        BufferedReader bf = new BufferedReader(in);

        String str ="";
        while (true)){

            str = bf.readLine();

            if(str.equals("done")) break;

            InetAddress address = servSocket.getInetAddress();
            String ip = address.getHostAddress();
            System.out.println("IP: " + ip);
            System.out.println("Port: " + servPort);

            System.out.println("Message from client: " + str);

            PrintWriter pr = new PrintWriter(servSocket.getOutputStream());
            pr.println(str.toUpperCase());
            pr.flush();
        }
        servSocket.close();
    }
    }  

And then change client side:

public class myFirstTCPClient {
    public static void main(String[] args) throws IOException {
        String S="";
        Scanner input = new Scanner(System.in);

        // you need to provide your server ip/domain
        // InetAddress.getLocalHost() , still works but only works when 
        // your client is in the same machine. 
        Socket clntSocket = new Socket("127.0.0.1", 4999);

        while (!(S.equals("done"))){
            System.out.println("Enter a sentence");
            S = input.nextLine();
            double sent = System.nanoTime();
            PrintWriter pr = new PrintWriter(clntSocket.getOutputStream());
            pr.println(S);
            pr.flush();

            InputStreamReader in = new InputStreamReader(clntSocket.getInputStream());
            BufferedReader bf = new BufferedReader(in);

            String str = bf.readLine();
            System.out.println("Message from server: " + str);
            double received = System.nanoTime();
            double total = received - sent;
            System.out.println("Round Trip Time: " + (total/1000000.0));
        }
        clntSocket.close();
    }
}
2
  • Still wrong. If the first str read is done, it will be treated as a client message, echoed, etc. The test must be after the readLine().
    – user207421
    May 29, 2019 at 3:58
  • Thanks for pointing it. However objective is to indicating what is wrong with this code not to cover all corner cases. Anyway I've updated the code. May 29, 2019 at 4:14

Your Answer

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

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