Я пытаюсь отправить деталь клиентской системы серверной системе, но сервер не получает ничего. Он прекращает печать после достижения первой строки. кто-то поможет мне преодолеть эту проблему.Строка, усеченная при передаче от клиента к серверу - Java
// Сервер
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);
}
}
// Клиент
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);
}
}
Вы обертываете 's.getOutputStream()' как в 'ObjectOutputStream' *, так и в' DataOutputStream'. Это кажется мне очень плохой идеей. Вероятно, это не проблема, учитывая, что вы никогда не используете 'dOut', но это все еще плохая идея ... –
И вы делаете это в цикле, что делает его еще хуже. Откройте эти потоки только один раз поверх цикла. – Thilo
Итак, каков правильный способ и решение для этого? – question