0
두 개의 간단한 응용 프로그램을 사용하여 클라이언트 컴퓨터를 원격으로 종료, 다시 시작 및 로그 오프 할 수 있습니다. 클라이언트가 서버에 연결할 때, 연결이 종료 될 때까지 Jlabel 구성 요소 (statuslabel)는 더 이상 편집 할 수 없습니다 (서버 응용 프로그램을 닫음으로써) socket.isConnected 메서드를 사용하는 것과 같이 많은 것을 시도했지만 결과가 없습니다.클라이언트와 서버 간의 연결이 종료 될 때까지는 더 이상 Jlabel 구성 요소를 편집 할 수 없습니다.
이package homework_5.client;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.*;
import javax.swing.JOptionPane;
/**
*
* @author حسين
*/
public class ClientSideGUI extends javax.swing.JFrame {
Socket socket=null;
String status;
String recievedCommand;
BufferedReader in=null;
Process child;
/**
* Creates new form ClientSide
*/
public ClientSideGUI() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
statuslabel = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Client.");
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowOpened(java.awt.event.WindowEvent evt) {
formWindowOpened(evt);
}
});
statuslabel.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
statuslabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(19, 19, 19)
.addComponent(statuslabel, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(20, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(55, Short.MAX_VALUE)
.addComponent(statuslabel, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(53, 53, 53))
);
pack();
}// </editor-fold>
private void formWindowOpened(java.awt.event.WindowEvent evt) {
try{
socket=new Socket("127.0.0.1", 1234);
status="Online";
}catch(Exception e){status="Offline";}
//Display status @حسين
if(status.equals("Online"))
{
statuslabel.setForeground(Color.GREEN);
statuslabel.setText(status);
}
if(status.equals("Offline"))
{
statuslabel.setForeground(Color.RED);
statuslabel.setText(status);
}
//Getting the work done :) @حسين
{
while(status.equals("Online"))
{
try{
in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
recievedCommand=in.readLine();
}catch(IOException e2){JOptionPane.showMessageDialog(rootPane,"Connected to server,\n but facing problems with recieving command\n Exception:"+e2.getClass().getName());}
if(recievedCommand.equals("shutdown -s"))
{try{
child=Runtime.getRuntime().exec(recievedCommand);
}catch(Exception e3){JOptionPane.showMessageDialog(rootPane, "Command Recieved but an error occured on local execution.\n Exception:"+e3.getClass().getName());}
}
if(recievedCommand.equals("shutdown -r"))
{try{
child=Runtime.getRuntime().exec(recievedCommand);
}catch(Exception e4){JOptionPane.showMessageDialog(rootPane, "Command Recieved but an error occured on local execution.\n Exception:"+e4.getClass().getName());}
}
if(recievedCommand.equals("shutdown -l"))
{try{
child=Runtime.getRuntime().exec(recievedCommand);
}catch(Exception e5){JOptionPane.showMessageDialog(rootPane, "Command Recieved but an error occured on local execution.\n Exception:"+e5.getClass().getName());}
}
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(ClientSideGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(ClientSideGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(ClientSideGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(ClientSideGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new ClientSideGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
public javax.swing.JLabel statuslabel;
// End of variables declaration
}
내 프로그램의 창이 충돌 할 때 내 문제가 해결됩니다. 서버에서 데이터를 가져올 때 '스레드'를 추가합니다. – bullprog