I'm trying to send a detail of client system to server system but the server isn't receiving everthing. It stops printing once it has reached the first line. someone help me get through this problem.
//Server
void connect_clients() throws ClassNotFoundException, InterruptedException
{
try {
ServerSocket listener = new ServerSocket(7700);
jButton1.setText("Server Running!");
jButton1.setEnabled(false);
ObjectInputStream ois; // = new ObjectInputStream(socket.getInputStream());
while (true) {
socket = listener.accept();
socketList.add(socket);
ois = new ObjectInputStream(socket.getInputStream());
String message = (String) ois.readObject();
System.out.println("Message Received: " + message); */*/*this is not printing everything
}
}
catch(IOException ex)
{
JOptionPane.showMessageDialog(null, ex);
}
}
//Client
void connect_server() throws IOException
{
try {
// TODO code application logic here
String serverAddress = JOptionPane.showInputDialog(
"Enter IP Address of a machine that is\n" +
"running the date service on port 9090:");
s = new Socket(serverAddress, 7700);
ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());
ObjectInputStream ois = null;
BufferedReader input;
String answer;
while(true){
input =
new BufferedReader(new InputStreamReader(s.getInputStream()));
DataOutputStream dOut = new DataOutputStream(s.getOutputStream());
answer = input.readLine();
System.out.println(answer);
if(answer != null)
{
oos.reset();
String line = "";
String command = "powershell.exe Get-ItemProperty HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | \n" +
"Format-Table –AutoSize";
// Executing the command
Process powerShellProcess = Runtime.getRuntime().exec(command);
// Getting the results
powerShellProcess.getOutputStream().close();
System.out.println("Standard Output:");
BufferedReader stdout = new BufferedReader(new InputStreamReader(
powerShellProcess.getInputStream()));
while ((line = stdout.readLine()) != null) {
byte[] mybytearray = new byte[(int) line.length()];
oos.writeObject(mybytearray);
}
stdout.close();
System.out.println("Standard Error:");
BufferedReader stderr = new BufferedReader(new InputStreamReader(
powerShellProcess.getErrorStream()));
while ((line = stderr.readLine()) != null) {
oos.writeObject(line);
System.out.println("fdg"+line);
//printing the output to a file --start
try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("myfile.txt", true)))) {
out.println(line);
}
catch (IOException e)
{
JOptionPane.showMessageDialog(null, e);
}
}
stderr.close();
System.out.println("Done");
}
}
}
catch (ConnectException e) {
JOptionPane.showMessageDialog(null, e);
}
catch (SocketException e) {
JOptionPane.showMessageDialog(null, e);
}
}
You're calling writeObject
multiple times on the same ObjectOutputStream
- but in the reading code, you're only calling readObject
once.
You'll need to loop in the reading code, e.g.
while (true) {
String message = (String) ois.readObject();
System.out.println("Message Received: " + message);
}
Now that will throw an exception when it reaches the end of the stream, I suspect - you should probably have some sentinel value (e.g. null) to indicate "end of data". (I don't think ObjectInputStream
just does this for you.)
Additionally, in your client code, you're creating multiple InputStreamReader
objects wrapping the same socket's InputStream
- that's a bad idea.
Fundamentally, I think you need to think more carefully about the protocol between the server and the client - think about how the server is meant to know when the client is finished with one set of output, and when it should send the next process name, etc. Then try to redesign your code so that you only ever wrap any particular stream once (although that wrapper can then be wrapped itself, of course). Finally, it seems odd to use an ObjectInputStream
/ObjectOutputStream
in one direction, but just plain text in the other direction. Protocols should rarely be asymmetric to that extent, IMO.
See more on this question at Stackoverflow