I am currently learning Java Network programming. In one of my Programs I just have an EchoServer which sends the message of the client. But I recognized in the client that the Printwriter.write() method just sends when I close the writer while the .println() method works fine. I also tried it with and without auto-flush.
Works:
public static void main(String[] args) {
System.out.println("Simple Echo Client");
try{
System.out.println("Waiting for Connection ...");
InetAddress localAdress = InetAddress.getLocalHost();
try(Socket clientSocket = new Socket(localAdress,6000);
PrintWriter out =new PrintWriter(clientSocket.getOutputStream(),true);
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))){
System.out.println("Connected to Server");
Scanner scanner = new Scanner(System.in);
while(true){
System.out.print("Enter text: ");
String inputLine = scanner.nextLine();
if("quit".equals(inputLine)){
break;
}
out.println(inputLine);
String response = br.readLine();
System.out.println("Server response" + response);
}
}
}catch(IOException ex){
ex.printStackTrace();
}
}
Doesn't work:
public static void main(String[] args) {
System.out.println("Simple Echo Client");
try{
System.out.println("Waiting for Connection ...");
InetAddress localAdress = InetAddress.getLocalHost();
try(Socket clientSocket = new Socket(localAdress,6000);
PrintWriter out =new PrintWriter(clientSocket.getOutputStream(),true);
BufferedReader br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()))){
System.out.println("Connected to Server");
Scanner scanner = new Scanner(System.in);
while(true){
System.out.print("Enter text: ");
String inputLine = scanner.nextLine();
if("quit".equals(inputLine)){
break;
}
out.write(inputLine);
String response = br.readLine();
System.out.println("Server response" + response);
}
}
}catch(IOException ex){
ex.printStackTrace();
}
}
Could somebody explain to me why this is the case?
the Printwriter.write() method just sends when I close the writer while the .println() method works fine
There seems there may be two problems here, the first having to do with writing and the second with reading:
The code creates a PrintWriter with automatic line flushing. When you use println, the new line results in the writer flushing. When using write without a new line, the PrintWriter does not flush (you can call out.flush after out.write to force a flush of the Writer).
Presuming the receiving end is using Scanner.readLine(), it expects a new line or will wait until it receives one. println automatically appends the new line to the end of the String, with write you must explicitly send the new line out.write(line + "\n");
Yes I've absolutely seen this before. If you are using a PrintWriter or another Writer that has autoflush, you do not need to call flush(). But otherwise, to get the message to send, you've got to call flush() to get the content in the Writer / OutputStream to send.
Related
I am new to socket programming. I have to write a program where client accepts filename from a user and sends it to the server. The server reads corresponding file and sends its content back to client. Now my problem is server program freezes on 'String file = br.readLine()'. When I terminate my client program, further lines of server code get executed. If I comment out while loop at the end of my client code, server code works perfectly fine(it prints data to standard output). Can you tell what could be wrong with my code?
Server Code:
public class SocketServer {
public static void main(String[] args) throws Exception{
System.out.println("Server is started.");
ServerSocket ss = new ServerSocket(9999);
System.out.println("Server is waiting for a client.");
Socket server = ss.accept();
System.out.println("Client is connected.");
BufferedReader br = new BufferedReader(new InputStreamReader(server.getInputStream()));
String file = br.readLine();
System.out.println("Requested file is: " + file);
OutputStreamWriter os = new OutputStreamWriter(server.getOutputStream());
PrintWriter writer = new PrintWriter(os);
BufferedReader fr = new BufferedReader(new FileReader(file));
String line;
while((line = fr.readLine()) != null) {
writer.write(line);
writer.flush();
System.out.println(line);
}
}
}
Client Code:
public class SocketClient {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
Socket client = new Socket("localhost", 9999);
OutputStreamWriter os = new OutputStreamWriter(client.getOutputStream());
PrintWriter writer = new PrintWriter(os);
System.out.print("Enter filename: ");
String file = in.nextLine();
writer.write(file);
writer.flush();
BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
System.out.println("Content of " + file + ":");
String str;
while((str = br.readLine()) != null) {
System.out.print(str);
}
System.out.println("File transfer is complete.");
}
}
br.readLine(); will wait for input till it finds a new-line .
writer.write(file); You are writing file name without a new-line.
So in order to make it work either write a newline char at client or read it char by char at server.
Hope this helps.
I'm trying to learn socket programming in Java but unfortunately I'm running into some behaviour that I don't understand. I have a very simple client program that connect to a server socket and sends some text that gets echoed back. Said client program looks like this:
try(
Socket socket = new Socket("127.0.0.1", 5001);
OutputStreamWriter writer = new OutputStreamWriter(socket.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
){
String userInput = "";
while (!userInput.toLowerCase().equals("quit")) {
userInput = stdIn.readLine();
writer.write(userInput);
writer.flush();
System.out.println("Server response: " + reader.readLine());
}
}
catch(Exception e) {e.printStackTrace();}
When I run this program the first line that I enter gets sent to the server but after that I can enter as many lines as I want and nothing gets sent. I also never see anything printed out by the System.out.println() line.
But if I switch out the OutputStreamWriter for a PrintWriter everything works as it should! Here's the code with PrintWriter:
try(
Socket socket = new Socket("127.0.0.1", 5001);
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
){
String userInput = "";
while (!userInput.toLowerCase().equals("quit")) {
userInput = stdIn.readLine();
writer.println(userInput);
System.out.println("Server response: " + reader.readLine());
}
}
catch(Exception e) {e.printStackTrace();}
Anyone have any idea why the first of the above two programs acts weird while the second one works? If anyone can tell me what the difference between writing with an OutputStreamWriter vs a PrintWriter is then that might tell me what's going on.
Note that the difference between write() and println() is that println() adds a linebreak after the data while write() does not.
So if your server uses readLine() to receive the data with a client using write() it might wait forever for the end of the line to read without receiving it.
So writer.write(userInput + "\n") might do the trick.
I have a problem getting the output that i want from the code that i have implemented in order to create the server/client program...it's just a really simple one, and i don't know why i don't get what i want.
Here is the code of the server:
ServerSocket serverSocket = new ServerSocket(1025);
System.out.println("Porting...");
Socket socket = serverSocket.accept();
PrintWriter out = new PrintWriter(socket.getOutputStream());
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String s = in.readLine();
System.out.println("Server read: " + s);
out.write("Got it");
socket.close();
System.out.println("Server Exit");
The client:
System.out.print("Connecting...");
Socket socket = new Socket("localhost",1025);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
out.write("Hello, Server");
String s = in.readLine();
System.out.println("Client Recieved: " + s);
socket.close();
System.out.println("Client Exit");
I try to get the Hello, Server output, instead i just get the "connecting" syso from the client (which i just did to see if it works)
Once you wrote on the stream you have to flush the stream by calling flush() method on the outputstream. Else the stream will be flushed once the stream buffer is full.
out.flush();
Also you have to make sure that enter the new line character to mention the end of line. Because readLine() waits for string with newline().
A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r')
out.write("Hello, Server\n");
out.flush();
I've the following Server.java:
public class Main {
public static void main(String args[]) throws Exception{
ServerSocket server = new ServerSocket(12345);
Socket client = server.accept();
OutputStream out = client.getOutputStream();
BufferedWriter writer = new BufferedWriter(new PrintWriter(out));
writer.write("Hello client");
writer.flush(); //After executing of that instruction there is no any output on the client
client.close(); //The client prints "Hello client"
}
}
and the Client.java:
public class Main {
#SuppressWarnings("resource")
public static void main(String args[]) throws Exception{
Socket s = new Socket("localhost", 12345);
BufferedReader r = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println(r.readLine());
s.close();
}
}
The issue is I can't get the reason why the client prints the string only after the connection was closed, but not after the stream was flushed. I thought flush() send a signal to the client that data-transfering process is over. Since, the client have to read all the data that was being send to it before calling flush(). What's wrong?
readLine() reads a complete line. To know that the line is complete, the reader needs to either find a newline character sequence, or the end of the stream. So it blocks until it sees one of those.
Realize that your client might very well do the following:
out.write("Hello ");
out.flush();
out.write("world!\n");
out.flush();
This sends a single line: "Hello world". And readLine() as its name indicates, is supposed to return that, not 2 lines "Hello " and "World".
So, if you want to send a line, you need to end its line terminator. Otherwise you're not sending a line, but only some characters.
Note that it would be easier if you used a PrintWriter correctly:
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(client.getOutputStream())));
out.println("Hello client");
I sending strings to a server but the server get strange chars before the string i've send, I'v tried to flush before send, after send and after inicializating the outputstream variable but the result is the same.I Heard a lot about flush() and also search about it, but still didnt find how to solve my problem, maybe its simple but i cant get it.
please Help me!
Client Side
InetAddress endereco = InetAddress.getByName(null);
socket= new Socket(endereco, SServer.PORTO);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new ObjectOutputStream(socket.getOutputStream()),true);
while(true){
System.out.println("Write your Thoughs.. ");
Scanner scanner = new Scanner(System.in);
String Msg = scanner.nextLine();
System.out.println("I Said: ");
System.out.println(Msg);
out.println(Msg);
out.flush();
String s = in.readLine();
System.out.println("Echo: "+s);
}
//Serve Side
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.flush();
}
private void serve() throws IOException {
while(true){
String s = in.readLine();
System.out.println("Server Received: " + s);
if(s.equals("END"))
break;
System.out.println("Server Send: " + s);
out.println(s);
}
I think what is happening is that when you write your strings, you are pushing them through the ObjectOutputStream, which is attempting to serialize them (I think). You are making things more difficult than necessary. Try using
out = new PrintWriter(socket.getOutputStream());
That will just dump the text directly through the line without all the additional stuff.