0
I am trying to carry out server and client code in Java for the client to send a String
, the server process and return it and the client receive.
The customer is kept always listening.
But after receiving the String
he hangs.
Server class
// porta do servidor
int serverDoor = 4000;
// numero maximo de conexões que o servidor aceita
int maxConnections = 10;
// servidor socket
ServerSocket server = null;
// conexão socket
Socket connection = null;
// saida dos dados
OutputStream output = null;
// entrada dos dados
InputStream input = null;
try {
server = new ServerSocket(serverDoor, maxConnections);
while (true) {
System.out.println("Esperando cliente");
connection = server.accept();
// abrindo o stream de saida
output = connection.getOutputStream();
output.flush();
// abrindo o stream de entrada
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// recebendo
String teste = bufferedReader.readLine();
output.write("Teste".getBytes());
output.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (output != null) {
output.close();
}
if (input != null) {
input.close();
}
if (connection != null) {
connection.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Customer class
Socket client;
OutputStream output;
InputStream input;
// Step 1: Create a Socket to make connection
client = new Socket(InetAddress.getByName(url), porta);
//client.setSoTimeout(15000); // 5 segundos
// Step 2: Get the input and output streams
output = client.getOutputStream();
output.flush();
input = client.getInputStream();
// Step 3: Process connection
output.write(msg.getBytes());
output.flush();
// recebendo
System.out.println("Antes de receber cliente");
StringBuilder sb = new StringBuilder();
int temp;
while ((temp = input.read()) > -1)
sb.append((char) temp);
System.out.println("Depois de receber cliente");
// Step 4: Close connection
if (output != null)
output.close();
if (input != null)
input.close();
if (client != null)
client.close();
return sb.toString();
}
After the method of receiving the String it to, I use Windows 8, I don’t know if this can represent something.
Why
new ServerSocket(serverDoor, maxConnections)
is inside an infinite loop?– pepper_chico
@pepper_chico arranged but still gives the problem.
– Macario1983
Where is
msg
, the message you send? As I do not see, I do not know if there is an end-of-line in it, since the server makes areadLine()
and will wait for the end-of-line to arrive.– pepper_chico
@pepper_chico it comes as parameter, is a static class method the client.
– Macario1983
The point is, she has an end-of-line?
– pepper_chico
write
the client will send data without adding extra end-of-line, and the server doesreadLine()
.– pepper_chico
@pepper_chico sorry, but I didn’t quite understand what you meant, could you reply with the edited code please? I changed the method to
readline
because I found it on the net and it seemed to be simpler than the client’s tie.– Macario1983
understand end-of-line as
'\n'
. Your server says that you understand an end of a message with this character, because you use areadLine
. But in the customer you send data viawrite
which will only write the characters you request, and if there is no'\n'
your server will be waiting for one. If there is a methodwriteLine
, this will add a'\n'
for you.– pepper_chico
@pepper_chico understood, talking to different objects if I can say so.
– Macario1983
see the tips and comments in the other answers also.
– pepper_chico