Forum Stats

  • 3,872,055 Users
  • 2,266,372 Discussions
  • 7,911,041 Comments

Discussions

Servlet writing and reading String from Socket

914465
914465 Member Posts: 2
edited Jan 31, 2012 8:43PM in Java Servlet
Hello,

I have a webpage with one link to one Servlet.

this Servlet is supposed to connect to one (working) server on port 4445.

I send one string to the server and he invokes one method and respondes with another String.

The servlet correctly sends the string to the server the method on the server is working but the Servlet is not getting the response from the server. The server method for sending is well implemented.

The servlet is supposed to print HTML code after receiving something. I'm using TOMCAT7 as the servlet container.

Can you please help?
thank you very much!


THREAD ON THE SERVER:

public void run() {


try {

BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

String input = in.readLine();

if (input.equals("listar"))
{
System.out.println("RECEIVED"+input);
out.print(ServidorSubs.listaEcrasValues());
System.out.println("SENT"+ServidorSubs.listaEcrasV alues());
}
else
System.out.println("RECEIVED"+input);

in.close();
out.close();
socket.close();

}catch (IOException e){
e.printStackTrace();
}
}

SERVLET CODE:

/**
* envia para o servidor o pedido de listar
* o que este tem no HashMap de ecrãs
*/

import java.net.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

public class ListarEcras extends HttpServlet{

public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

// IP do servidor
String servidor = "127.0.0.1";

listar(servidor,4444);

}

public void listar (String servidor, int porta)
{
String ecras = null;
Socket socket = null;
PrintWriter out = null;
BufferedReader in = null;

try { //1.Open a socket.
socket = new Socket(servidor, porta);

//Open an output stream to the socket.
out = new PrintWriter(socket.getOutputStream(), true);

//Open an intput stream to the socket.
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));

} catch (UnknownHostException e) {
System.err.println("Don't know about host: " +servidor);
System.exit(1);
} catch (IOException e) {
System.err.println("Não consigo criar o OutputStream para o :" +servidor);
System.exit(1);
}

//write to the stream
out.println("listar");

//WAIT FOR RESPONSE FROM SERVER
try{

ecras = in.readLine();
out.println("<html>");
out.println("<head>");
out.println("<title> " + ecras + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Hello World!</h1>");
out.println("</body>");
out.println("</html>");

}catch (IOException e){
System.err.println("Erro: "+e.getMessage());
System.exit(1);
}

try{

//close stream
out.close();
in.close();

//close o socket
socket.close();

}catch (IOException e) {
System.err.println("Não consegui fechar os sockets");
System.exit(1);
}
}

}

Answers

  • EJP
    EJP Member Posts: 32,920 Gold Crown
    out.print(ServidorSubs.listaEcrasValues());
    Change that to println(). You are reading lines but you aren't sending them.
  • 914465
    914465 Member Posts: 2
    Thank you very very much.

    Still cant understand why it didnt work because the method ServidorSubs.listaEcrasValues() returns one string....and I was printing it to the Socket Stream with the PrintWriter...

    I replaced that with out.println("Info: "+ServidorSubs.listaEcrasValues()); and it was sent to the socket and received by the servlet.

    I want to be able to send to the servlet several strings using a while loop and then print it on the browser.

    once again thank you very much.
  • EJP
    EJP Member Posts: 32,920 Gold Crown
    You are reading lines but you aren't sending them
    Still cant understand why it didnt work
    Strange, I told you.
This discussion has been closed.