I want to send a simple charArray
via a socket, from a Java application.
My problem is that without adding an '\n' at the end of a charArray
, the writing doesn't work.
Do I have to add this '\n' or is there a more elegant solution?
Here is my code:
public class ClientThread extends Thread {
private static final String TAG = "ClientThread";
private Socket socket = null;
private BufferedWriter outputStream = null;
private BlockingQueue<Message> queue = null;
private static final int SERVERPORT = 8080;
private static final String SERVER_IP = "localhost";
public ClientThread(BlockingQueue<Message> queue) {
this.queue = queue;
}
@Override
public void run() {
try {
InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
//send a request to connect to the server is listening on machine 'localhost', port 8080
socket = new Socket(serverAddr, SERVERPORT);
//create output stream to send data to the server
outputStream = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while(socket.isConnected()) {
//write message to socket
char[] test = {'t', 'e', 's', 't'};
for(int i=0; i<test.length; i ) {
outputStream.write(test[i]);
}
outputStream.write("\n"); //does not work without this line
outputStream.flush();
}
} catch (UnknownHostException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
CodePudding user response:
You have not said what is reading your socket, but given that it is port 8080, I imagine it could be a web server and what you are sending is not a valid HTTP request. I don't know what response you are expecting or how you are determining if the message has been received, but again, I imagine it might be some debug logging of some sort. If the recipient is some server that you have written, perhaps you are reading the message with some method that expects a new-line.
The flush()
method call should be sufficient to send the message. A new-line is certainly not required by IP which is a binary protocol and doesn't know about lines of text.
But probably whatever is reading your socket is waiting for a new-line before it decides to display your message.
CodePudding user response:
It seems to me you are using many things that you don't fully understand
OutputStreamWriter
OutputStreamWriter is used for specifying the text format you want your output to be in. But since you have not filled in this parameter, you will get whatever the platform's default is (do you know what it is? If so, why not just enter it in the constructor?)
BufferedWriter
Buffers are useful for inefficient reading and writing. Inefficient means reading line by line rather than gobbling bytes as fast as possible. For writers, it means waiting to send data to an output. Is this what you want?
In any case adding the new line character shouldn't be necessary. Flush() should do the trick. But if \n is necessary, it means on the server side it is waiting for this character (probably using BufferedReader.readLine())
All that said, I'd advise you to start with PrintStream
(you probably don't need a Writer
if you're not specifying text encoding) and go from there.