Reputation: 321
I have a program Client
that sends the server the string LISTALL
.
After that, the server needs to check for all running processes on the system (Linux) using the ps -e
command, and then return the result to the client.
Obviously the result is composed of multiple strings, so how can i get the client to receive and store them all?
Right now, this is what i get from the client:
Message sent to the server: LISTALL
Message received from the server: null
And from the server:
Server started and listening to..
Message received from client: LISTALL
And then it lists the results from the ps -e
command as expected.
My code:
Server:
public class Server
{
private static Socket socket;
public static void main(String[] args)
{
try
{
int port = 25000;
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server Started and listening to the port 25000");
//Server is running always. This is done using this while(true) loop
while(true)
{
//Reading the message from the client
socket = serverSocket.accept();
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String list = br.readLine();
System.out.println("Message received from client is "+list);
//Sending the response back to the client.
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String returnMessage;
try
{
ProcessBuilder build = new ProcessBuilder("ps", "-e");
Process proc = build.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
while (reader.readLine() != null) {
returnMessage=reader.readLine();
bw.write(returnMessage);
proc.waitFor();
System.out.println("Message sent to the client is "+returnMessage);
bw.flush();}
}
catch(NumberFormatException e)
{
//Input was not a number. Sending proper message back to client.
returnMessage = "Please send a proper number\n";
}
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
socket.close();
}
catch(Exception e){}
}
}
}
Client:
public class Client
{
private static Socket socket;
public static void main(String args[])
{
try
{
String host = "localhost";
int port = 25000;
InetAddress address = InetAddress.getByName(host);
socket = new Socket(address, port);
//Send the message to the server
OutputStream os = socket.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os);
BufferedWriter bw = new BufferedWriter(osw);
String list = "LISTALL";
String sendMessage = list + "\n";
bw.write(sendMessage);
bw.flush();
System.out.println("Message sent to the server : "+sendMessage);
//Get the return message from the server
InputStream is = socket.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
while (br.readLine() != null) {
String message = br.readLine();
System.out.println("Message received from the server : " +message);
}}
catch (Exception exception)
{
exception.printStackTrace();
}
finally
{
//Closing the socket
try
{
socket.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
}
Upvotes: 2
Views: 2784
Reputation: 310893
Your read loops are incorrect. They should be of the general form:
String line;
while ((line = reader.readLine()) != null) {
// Do something with `line`
}
At present you are throwing away every odd line, and printing a null if there is an odd number of lines.
Upvotes: 2