java控制linux

package cn.connectTalk.com;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;

public class SocketClientExample extends JFrame implements ActionListener{
	Label label=new Label("输入聊天信息");
	TextField tf=new TextField(20);
	TextArea ta=new TextArea();
	Panel panel=new Panel();
	Socket Client;
	InputStream DataIn;
	OutputStream DataOut;
	
	
	public SocketClientExample()
	{
		super("这里是客户机");
		setSize(300,180);
		panel.add(label);
		panel.add(tf);
		tf.addActionListener(this);
		add("North",panel);
		add("Center",ta);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(){
			System.exit(0);
			}
		});
		show();
		try{
			Client=new Socket("127.0.0.1",5000);
			ta.append("已经和服务器建立连接");
			DataIn=Client.getInputStream();
			DataOut=Client.getOutputStream();
		}catch(IOException ioe){
		}
		while(true){
			try{
				byte buff[]=new byte[500];
				DataIn.read(buff);
				String str=new String(buff);
				ta.append("服务器说"+str+"\n");
			}catch(IOException ioe){
			}
		}
	}
	
	public static void main(String []args){
		new SocketClientExample();
	}
	
	
	public void actionPerformed(ActionEvent e){
		try{
			String str=new String(tf.getText());
			byte[] buff=str.getBytes();
			tf.setText("");
			DataOut.write(buff);
			ta.append(" 客户端说:"+str+"\n");
		}catch(IOException ioe){
			
		}
	}

}



下面为服务器端代码
 
package cn.connectTalk.com;
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

import javax.swing.JFrame;

public class SocketServerExample extends JFrame implements ActionListener{
	Label label=new Label("输入聊天信息");
	TextField tf=new TextField(20);
	TextArea ta=new TextArea();
	Panel panel=new Panel();
	ServerSocket server;
	Socket Client;
	InputStream DataIn;
	OutputStream DataOut;
	
	public SocketServerExample()
	{
		super("这里是服务器");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300,180);
		panel.add(label);
		panel.add(tf);
		tf.addActionListener(this);
		add("North",panel);
		add("Center",ta);
		addWindowListener(new WindowAdapter(){
			public void windowClosing(){
			System.exit(0);
			}
		});
		show();
		try{
			server=new ServerSocket(5000);
			Client=server.accept();
			ta.append("已经和客户端建立连接\n");
			DataIn=Client.getInputStream();
			DataOut=Client.getOutputStream();
		}catch(IOException ioe){
		}
		
		while(true){
			try{
				byte buff[]=new byte[500];
				for(int j=0;j<500;j++)
					buff[j]='\0';
				DataIn.read(buff);
				String str=new String(buff);
				if(str==null)continue;
				ta.append("客户机说"+str+"\n");
				/******************************************************/
				String str1="";
				for(int i=0;i<str.length();i++)
				{
					if(buff[i]=='\0')
						break;
						str1=str1+(char)buff[i];
				}
				Process pro = Runtime.getRuntime().exec(str1);
				System.out.println(str1);
				BufferedReader br = new BufferedReader(new InputStreamReader(pro
				.getInputStream()));
				String msg = null;
				String all ="";
				while ((msg = br.readLine()) != null) {
					all=all+msg;
					all=all+" ";
				System.out.println(msg);
				}
				pro.destroy();
				byte[] buff1=all.getBytes();
				DataOut.write(buff1);
				ta.append("服务器说:"+all+"\n");
				/******************************************************/
			}catch(IOException ioe){
			}
		}
		
	}
	
	
	public static void main(String []args){
		new SocketServerExample();
	}
	
	
	public void actionPerformed(ActionEvent e){
		try{
			String str=new String(tf.getText());
			/******************************************************/
			Process pro = Runtime.getRuntime().exec(str);
			BufferedReader br = new BufferedReader(new InputStreamReader(pro
			.getInputStream()));
			String msg = null;
			String all ="";
			while ((msg = br.readLine()) != null) {
				all=all+msg;
				all=all+" ";
			System.out.println(msg);
			}
			//System.out.println(all);
			pro.destroy();
			/******************************************************/
			byte[] buff=all.getBytes();
			tf.setText("");
			DataOut.write(buff);
			ta.append("服务器说:"+all+"\n");
		}catch(IOException ioe){
			
		}
	}
	

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值