I am trying to write a basic client-server program to allow the client to traverse the files of the server. I wish to get the server to, on connection of the client, send a list of all the files in the current directory to the client. How would I go about doing this ? I have already created an array of all the filenames, and am trying to send these to the client it just sits in an infinite loop (as it is meant to !) and does nothing.
Attempting to have the server send a message on connection of the client :
DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
boolean found = false;
//Read the data from the input stream
for (int i=0;i < fileList.length;i++) {
outToClient.writeBytes(fileList[i].getName());
}
And to have the server receive this :
//Prepare an object for reading from the input stream
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//Read the data from the input stream
sentence = inFromServer.readLine();
but doing this just makes the client sit in an infinite loop and do nothing.
Help please?
First make sure that you have following handshaking between client and server:
... normal communication.
Sending 0 size messages might cause problems sometimes.
So using query-response approach seems to be accurate.
You primary problem is that you are attempting to send an empty string from the client to the server. An empty string translates to a 0 length byte array. In effect, you are not sending any data between the client and the server. In this situation, your server will continue to wait for data to be retrieved over the socket's InputStream. Since the server is still waiting, it does not move on to send the data back to the client. As a result, when you client tries to listen for data from the server, it waits indefinitely as the server never reaches that part of the code.
If your goal is to have the server simply send the data upon connect, you have a couple of options.
Example Server:
ServerSocket socket = new ServerSocket(8888);
Socket cSocket = socket.accept();
PrintWriter out = null;
try {
out = new PrintWriter(new OutputStreamWriter(cSocket.getOutputStream()));
for (String file : new File(".").list()) {
out.println(file);
}
}
finally {
if (out != null) {
out.close();
}
cSocket.close();
socket.close();
}
Example Client:
Socket s = new Socket("localhost", 8888);
BufferedReader in = null;
try {
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
finally {
if (in != null) {
in.close();
}
s.close();
}
In my basic server-client TCP example I make a way ...when a client is got accepted it got a welcome message . In that when from client side I send role , it gives me back name. Here is the link of source >> http://matrixsust.blogspot.com/2011/10/basic-tcp-server-client.html .
Hope it helps.
I don't believe it is possible to have a server send a message to a client unprompted. Send a message "dir" and listen on the server, then listen on client and reply.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.