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){
}
}
}