Reputation: 3381
I have a basic Server and a Client coded in Java. It's a code I saw on the internet and modified a bit to suit my needs. Now the thing is, I'm basically sending a message from the client to the server. This message would be "hello there". The server checks port 6789 for a message that contains the word "hello" and if that's true, it has to get the names of all files/sub-directories in a certain directory. The first of these names is sent back to the client and the client prints it. I've opened port 6789 and sending the message from the client to the server works perfectly. The server receives it and executes the necessary action, but it won't send back the data to the client. I don't know whether the client isn't receiving it or whether the server isn't sending it. Here's both codes:
Client
String sentence = "hello";
Socket clientSocket = new Socket("localhost", 6789);
DataOutputStream outToServer = new DataOutputStream(
clientSocket.getOutputStream());
outToServer.writeBytes(sentence + '\n');
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(
clientSocket.getInputStream()));
System.out.println(inFromServer.readLine());
clientSocket.close();
Server
String sentence;
ServerSocket welcomeSocket = new ServerSocket(6789);
while (true) {
Socket connSocket = welcomeSocket.accept();
BufferedReader inFromClient = new BufferedReader(
new InputStreamReader(connSocket.getInputStream()));
sentence = inFromClient.readLine();
if (sentence.contains("hello")) {
File dir = new File("D:/ServerMap/");
String[] children = dir.list();
if (children == null) {
JOptionPane.showMessageDialog(frame,
"Either no directory or it doesn't exist");
} else {
DataOutputStream outToClient = new DataOutputStream(
connSocket.getOutputStream());
outToClient.writeBytes(children[0]);
JOptionPane.showMessageDialog(frame, children[0]);
}
}
}
I'm executing the server through a separately exported Runnable Jar file, and the client is compiled and run in Eclipse.
Just to clarify my issue: Client sends "hello" to server -> Server receives the message and it contains "hello" so if-statement returns true -> Server checks for file names in the specified directory and finds a couple -> Server sends back the first file name which it found -> Client is supposed to receive that file name and print it in the Eclipse console (this is the part that doesn't work)
Any suggestions?
PS: This is all done on the local host. It's done on 1 computer, Windows 7 x64, port 6789 is open (didn't work at all before I manually opened it) and that's pretty much it.
Upvotes: 1
Views: 1512
Reputation: 548
The problem is that your client is waiting for the a line feed '\n' that server doesn't send. In the client you have inFromServer.readLine() and in the server side you have outToClient.writeBytes(children[0]) so you have to change it to outToClient.writeBytes(children[0] + "\n") as suggested by Reimeus. The method readLine from BufferedReader class blocks until it reads a "\n".
Upvotes: 0
Reputation: 159844
You should flush the data from your client output stream in your server:
outToClient.writeBytes(children[0] + "\n");
outToClient.flush();
// etc.
Upvotes: 3