Can be created a Socket client in a Servlet?
I am trying to create in a Servlet a Shocket client.
The client makes a request, I must itself connect to a machine, to a concrete port, to pass a String to him and to gather the String that she gives me like answer.
I always obtain an error:
Software caused connection abort: recv failed
It is had to create a thread by Socket obligatorily?
This my code:
DataOutputStream os = null;
DataInputStream is = null;
try{
if(clientSocket == null) {
clientSocket = new Socket(host, port);
}
is = new DataInputStream(clientSocket.getInputStream());
os = new DataOutputStream(clientSocket.getOutputStream());
os.writeBytes(cadena);
String response = is.readLine();
}
catch(Exception e) {
System.out.println("ERROR: "+e.getMessage()+"--> "+e);
}
|