0

I have found an example of a simple program in java that suppose to work which implements a client-server example in Java.

Client code:

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

public class ClientDemo {

    public static void main(String[] args) {

        try {
            Socket sock = new Socket ("127.0.0.1", 4321);

            DataInputStream input = new DataInputStream(sock.getInputStream());

            boolean more_data = true;

            while (more_data) {

                String line = input.readLine();

                if(line==null) {
                    more_data = false;
                }
                else {
                    System.out.println(line);
                }            
            }
        }
        catch (IOException e) {
        }    
    }    
}

Server code:

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

public class SimpleServer
{

public static void main(String[] args)
{
    try
{
        ServerSocket server = new ServerSocket (4321);            
        Socket sock = server.accept();
        DataInputStream inp = new DataInputStream (sock.getInputStream());
        PrintStream out = new PrintStream (sock.getOutputStream());
        output.println("server message");
        output.println("QUIT to Quit");

        boolean more_data = true;

            while (more_data)
    {
                String line = inp.readLine();

                if(line==null)
        {
                    more_data = false;
                }
                else
                {
                    output.println("Server:" +line+"\n");
                    if(line.trim().equals("Exit")
        {
                        more_data = false;
                    }
                }            
            }
            sock.close();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }    
    }    
}

when running the server first and then the client, I get the welcome message, but when entering a text in the client console then pressing enter, nothing happens.

I tried replacing the DataInputStream in BufferedInputStream as in the docs it said readline was deprecated, but the same behavior, plus I tried to change to scanner object, with nextLine, bt the same behavior there too.

your input would be appreciated very much.

theexplorer
  • 359
  • 1
  • 5
  • 21
  • 1
    See this answer http://stackoverflow.com/questions/22881227/what-would-be-the-code-for-this/22881978#22881978 – drkunibar Apr 05 '14 at 19:30
  • @drkunibar : thank you, but I did not manage to associate that solution to the code I supplied. Can you please be more specific to my code ? what needs to be changed in order for this example to work? – theexplorer Apr 05 '14 at 20:17
  • 1
    There is no console reader and nobody sends data to the server – drkunibar Apr 05 '14 at 20:28
  • @drkunibar : I thank you for your effort and I don't want to sound ungrateful, I do appreciate your answer, but it seems that you took it again in the direction of the solution you suggested before (with threads, etc.) - I will really appreciate it if you can tell me the minimal changes needed to be done in my code in order for it to work (if it's possible), rather than make the code more robust, etc. – theexplorer Apr 05 '14 at 20:52
  • I have tried to make it your way. See my answer – drkunibar Apr 05 '14 at 21:33

2 Answers2

0

That is because your client doesn't even try to read from the console. It only reads from the socket.

kostya
  • 9,221
  • 1
  • 29
  • 36
0

Try this client. And have a look at your server. It stop with Exit not with QUIT

UPDATE

I have changed the code and now I have to change my name ;-)

The client is very, very simple and nobody should use it. The client have to know how many lines the server will send. There is no way to wait of an unknown count of lines without another thread.

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

public class ClientDemo {

    public static void main(String[] args) {

        try {
            Socket sock = new Socket("127.0.0.1", 4321);
            // Build a Buffered Reader - it is easier to read complete line
            BufferedReader input = new BufferedReader(new InputStreamReader(sock.getInputStream()));
            // After connecting we read the first 2 line
            // We know that the server send 2 lines
            System.out.println(input.readLine());
            System.out.println(input.readLine());
            // Wen must have a reader for our inputs
            InputStreamReader consoleReader = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(consoleReader);
            while (true) {
                // read one line from the console
                String inline = in.readLine();
                inline += "\n";
                // send the line to the server
                sock.getOutputStream().write(inline.getBytes());
                sock.getOutputStream().flush();
                // Wait for server response - we know that we get 2 lines
                for (int i = 0; i < 2; i++) {
                    String response = input.readLine();
                    if (response == null || inline.equals("Exit")) {
                        break;
                    } else {
                        System.out.println(response);
                    }

                }
            }
        } catch (IOException e) {
        }
    }
}
drkunibar
  • 1,327
  • 1
  • 7
  • 7
  • Thank you. you still changed more than I was hoping for, but I managed to get what I need from your code and leave the rest of the code as is and it works, so I'll accept :) – theexplorer Apr 05 '14 at 22:02
  • I'm sure. yet, I wanted to know which were the missing pieces for MY puzzle. thanks for your input. – theexplorer Apr 05 '14 at 22:59