views:

44

answers:

2

Hi guys.

It's a simple client-server where Server using a BufferedWriter object would send to the Client receiving in the object BufferedReader.

When I use OutputStream and PrintWriter for the Server and InputStream and Scanner for the Client it works well.

What happens is that the client in Buffered way reads -1 if I'm sending an int and null for String.

I hope my English makes sense. ;P

That's the code:

Server:

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

public class Server {

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

        ServerSocket server = new ServerSocket(8189);
        Socket incoming;

        incoming = server.accept();
        try {
            // OutputStream output = incoming.getOutputStream();
            // PrintWriter outStream = new PrintWriter(output, true /*autoFlush*/);
            // outStream.println("ENTER");
            BufferedWriter output = new BufferedWriter(new           
                                    OutputStreamWriter(incoming.getOutputStream()));
            output.write(3);
            System.out.println("\nSent");

        } finally {
            incoming.close();
        }
    }
}

Client:

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

public class Client {

    public static void main(String[] args) throws UnknownHostException, IOException {

        //Client theClient= new Client();
        Socket RTSPSocket;
        int ServerPort = 8189;
        //server name address
        String ServerHost = "localhost";

        //get Server IP Address
        InetAddress ServerIPAddress = InetAddress.getByName(ServerHost);

        RTSPSocket = new Socket(ServerIPAddress, ServerPort);

        try {
            /*
            InputStream input = theClient.RTSPSocket.getInputStream();
            Scanner in = new Scanner(input);
            String line = in.nextLine();
            System.out.println(line);
            */

            BufferedReader input = new BufferedReader(new  
                                   InputStreamReader(RTSPSocket.getInputStream()));

            //String line = input.readLine();
            //System.out.println("\nRicevuto: " + line);
            System.out.println(input.read());
        } catch (Exception e) {
            System.err.println("Error: " + e);
        }
    }
}
+3  A: 

try putting the following code after output.write(3);

output.flush();

The BufferedOutputStream is built to only send the data when the buffer is full (I believe the default 1024 bytes). So, to force the data to be sent you need to flush the stream.

Codemwnci
Yup. Also, you'll want to close the BufferedOutputWriter in your finally block. Doing so will implicitly close the Socket as well, though its usually considered good form to explicitly close both.
romacafe
Thank you guys. Now it works. I didn't know it!
soneangel
+1  A: 

You have to flush data to receive them in the client part.

        output.write(3);
        output.flush();
        System.out.println("\nSent");

When you have an OutputStream (or a Writer) you have to flush your data, this way you're 100% sure that what you wanted to be send has been sent.

Most (if not all) the OutputStream subclasses use a "mini buffer" which is flushed only when it's full or you manually call flush(). In your case, it's even more flagrant because you're using a BufferedWriter.

Another thing, when you use streams/writers, you really should close them after you're finished, one of the main thing close() do (most of the time) is flushing the last data remaining in the "mini buffer".


Resources :

Colin Hebert
Thank you guys. Now it works. I didn't know it!
soneangel