This question already has an answer here:
I have a java application which uses sockets to communicate with another computer. I´m using a DataOutputStream to write data to the server and a BufferedReader to read from server. Before reaching the problem, I can succesfully send and read responses several times from the server.
However there´s a part where I send two, writeBytes()
functions one after the other. At this point, my server just reads the first string and stays stuck on readline()
even though I´ve send another string on the second writeBytes()
.
Here´s my snippet:
InetAddress ipServer = InetAddress.getByName(direcciones.get(0).host);
Socket clientSocket = new Socket(ipServer,25);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String response = inFromServer.readLine();
Here is where I´m getting just the email.data String and not the "." that comes afterwards.
outToServer.writeBytes(email.data+"\r\n");
outToServer.writeBytes("."+ "\r\n");
response = inFromServer.readLine();
This is how the data is read on the server:
while(mailReady == false){
String linea;
linea = input.readLine();
System.out.println("INPUT Paso5: "+linea);
email.data += linea+"\n";
if(linea.equals(".")){
mailReady = true;
...
Where input is a BufferedReader.
Can anyone tell me what is happening here?
You are reading lines but you aren't writing lines. Instead of
DataOutoutStream.writeBytes()
you should be usingBufferedWriter.write()
followed byBufferedWriter.newline().