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);
}
}
}