0

I have 2 classes (Client and Server) used to implement simple communication in my application. My code is shown below:

Server:

public class Server {
    public static void main(String[] ar) {
        int port = 1025; // just a random port. make sure you enter something between 1025 and 65535.
        try {
            ServerSocket ss = new ServerSocket(port); // create a server socket and bind it to the above port number.
            System.out.println("Waiting for a client...");
            Socket socket = ss.accept();
            InputStream sin = socket.getInputStream();
            OutputStream sout = socket.getOutputStream();
            DataInputStream in = new DataInputStream(sin);
            DataOutputStream out = new DataOutputStream(sout);
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(
                    System.in));
            System.out.println("enter meter id ");
            String line = null;

            while (true) {
                line = in.readUTF(); // wait for the client to send a line of text.
                System.out.println("client send me this id number " + line);
                line = keyboard.readLine();
                out.writeUTF(line);
                out.flush();
                //line = in.readUTF(); 
                System.out.println("Waiting for the next line...");
                System.out.println();
            }
        } catch (Exception x) {
            x.printStackTrace();
        }
    }
}

Client:

public class Client {
    public static void main(String[] ar) {
        int serverPort = 1025;
        String address = "localhost";
        try {
            InetAddress ipAddress = InetAddress.getByName(address); // create an object that represents the above IP address.
            System.out.println(" IP address " + address + " and port "
                    + serverPort);
            Socket socket = new Socket(ipAddress, serverPort); // create a socket with the server's IP address and server's port.
            InputStream sin = socket.getInputStream();
            OutputStream sout = socket.getOutputStream();
            DataInputStream in = new DataInputStream(sin);
            DataOutputStream out = new DataOutputStream(sout);
            // Create a stream to read from the keyboard.
            BufferedReader keyboard = new BufferedReader(new InputStreamReader(
                    System.in));
            String line = null;
            System.out.println("ClientConnected.");
            System.out.println("enter meter id");

            while (true) {
                line = keyboard.readLine(); // wait for the user to type in something and press enter.
                System.out.println("Sending this number to the server...");
                out.writeUTF(line); // send the above line to the server.
                out.flush(); // flush the stream to ensure that the data reaches the other end.
                line = in.readUTF(); // wait for the server to send a line of text.
                System.out
                        .println("The server was very polite. It sent me this : "
                                + line);
                System.out.println();
            }
        }
        catch (Exception x) {
            x.printStackTrace();
        }
    }
}

My problem is that while testing the program I do get communication between the client and server, but while debugging, with a break point on the out.flush line in Server.java, it does not go to the intended destination. This intended destination being the line line = in.readUTF(); of Client.java. Can anyone help me to solve this?

Perception
  • 79,279
  • 19
  • 185
  • 195
Sai Sai
  • 115
  • 2
  • 4
  • 17
  • First of all, when using localhost for client/server testing, you should use the IP address `127.0.0.x`, since "localhost" causes weird issues in a wide variety of situations. Second, can you please tell us what you *are* getting? – L0j1k Feb 25 '13 at 12:06
  • 1
    @L0j1k: would you cite some examples of localhost "weird issues"? Using `localhost` is a perfectly acceptable thing to do and won't produce different results to `127.0.0.1` unless you've purposefully done something terrible to your machines networking configuration. – Greg Kopff Feb 25 '13 at 12:09
  • Purposefully? No. You don't need to purposefully "do something terrible" to your networking configuration for some source code not be able to resolve "localhost". Consider source that doesn't do ANY hostname resolution. What then? – L0j1k Feb 25 '13 at 12:16
  • 1
    Please indent your code properly for SO. It looks like a dog's breakfast. – Stephen C Feb 25 '13 at 12:16
  • 3
    Any host that doesn't know how to resolve "localhost" or that resolves it to a non-loopback address, or that doesn't have a loopback network interface enabled is badly misconfigured. You could view this use of "localhost" as a test for the network setup as well as for other stuff. The "now what" should be "fix the network configs" – Stephen C Feb 25 '13 at 12:18
  • @L0j1K,thank you so much for your response, actuaaly i am getting,i am able to send messages from client to server and server to client from keyboard,like for example any chat messenger,but in my problem is when ever i am keeping debug point at out.flush of server it is not going to it's intended destination,can you please help me to solve this. – Sai Sai Feb 25 '13 at 12:23
  • Yes, any HOST. Are you aware that we're talking about source code here? Or did you miss that? Source code doesn't always resolve hostnames. Eliminating that for testing isn't a terrible idea. But you sure sound like you know what you're doing, so have at it, pro. – L0j1k Feb 25 '13 at 12:30

2 Answers2

0

It is good practice to open the OutputStreams before the InputStreams, on your sockets, as said in this question.

This question also clarifies that.

Community
  • 1
  • 1
afsantos
  • 5,178
  • 4
  • 30
  • 54
0

What I suspect here is your client and server are running in two different JVM processes and java debugger cannot debug two JVM at the same time.

Yashdeep Hinge
  • 382
  • 2
  • 14