I'm trying to write a simple server-client program, but I have a problem:
I can send data from client to server, but I can't send data from the server (I can't recive it in the client) :(
So how to send data from the server, and recive it in the client?
Server:
//this is in a thread
try {
server = new ServerSocket(1365);
} catch (IOException e) {
e.printStackTrace();
}
while (!exit) {
try {
clientSocket = server.accept();
is = new DataInputStream(clientSocket.getInputStream());
os = new PrintStream(clientSocket.getOutputStream());
while ((line = is.readLine()) != null) {
System.out.println("Message from client: " + line);
//if (line.equals("exit")) {
// exit = true;
//}
if (line.equals("say something")) {
os.write("something".getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
}
try {
is.close();
} catch (IOException ex) {
ex.printStackTrace();
}
os.close();
}
Client:
try {
socket = new Socket(host, 1365);
os = new DataOutputStream(socket.getOutputStream());
is = new DataInputStream(socket.getInputStream());
} catch (UnknownHostException e) {}
if (socket != null && os != null && is != null) {
try {
os.writeBytes("say something");
//get the answer from server
os.close();
is.close();
socket.close();
} catch (IOException e) {}
}
(sorry for the long code)
Thank you in advance.