| 关键词: nbsp new client public String import java IOException Panel buf |
C.java 客户端代码实现: import java.io.*; import java.awt.*; import java.awt.event.*; import java.net.*; public class C extends Frame implements ActionListener{ Label label=new Label("指令"); Panel panel=new Panel(); TextField tf=new TextField(20); TextArea ta=new TextArea(); Socket client; InputStream in; OutputStream out; public C(){ super("客户机"); setSize(250,250); panel.add(label); panel.add(tf); tf.addActionListener(this); add("South",panel); add("Center",ta); addWindowListener(new WindowAdapter(){ //退出 public void windowClosing(WindowEvent e) { System.exit(0); } }); show(); try{ client=new Socket(InetAddress.getLocalHost(),6000);//向6000端口发出客户请求 ta.append("服务器是:"+client.getInetAddress().getHostAddress()+"\n\n"); in=client.getInputStream(); out=client.getOutputStream(); }catch(IOException ioe){} while(true) { try{ byte[]buf=new byte[256]; in.read(buf); String str=new String(buf); ta.append(str+"\n"); } catch(IOException e){} } } public void actionPerformed(ActionEvent e){ try{ String str=tf.getText(); byte[]buf=str.getBytes(); tf.setText(null); out.write(buf); ta.append("\n指令:"+str+"\n"); }catch(IOException ioe){} } public static void main(String[]args){ new C(); } } S.JAVA 服务端代码实现: import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; public class S extends Frame { ServerSocket server; Socket client; InputStream in; OutputStream out; public S() { super("服务器"); setSize(250,250); addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { System.exit(0); } }); show(); try{ server=new ServerSocket(6000); client=server.accept(); in=client.getInputStream(); out=client.getOutputStream(); }catch(IOException ioe){} while(true){ try{ String Result=null; byte[]buf=new byte[256]; in.read(buf); String str=new String(buf); Process p = Runtime.getRuntime().exec("cmd /c "+str); BufferedReader br=new BufferedReader(new InputStreamReader(p.getInputStream())); while((Result=br.readLine())!=null) { out.write(Result.getBytes()); } }catch (IOException e){} } } public static void main(String[]args) { new S(); } } |
|
声明:文章版权归原作者所有 部分文章转自互联网 如有侵权请联系
[邮箱地址] 删除
|