日期:2014-05-20 浏览次数:20726 次
package xiti19;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import java.net.*;
import java.io.*;
class EchoServer{
private static ServerSocket svrSkt=null;
private Socket cltSkt=null;
private BufferedReader input=null; //输入流,来自客户端
private PrintStream output=null; //输出流,指向客户端
private JTextArea text = null;
public EchoServer(JTextArea serverText,int port){
text = serverText;
text.append("服务器代理正在监听,端口:"+port+"\n");
try{
svrSkt=new ServerSocket(port); //开始监听
}catch(IOException e){
text.append("监听端口"+port+"失败\n");}
try{
cltSkt=svrSkt.accept(); //接收连接请求
}catch(IOException e){
text.append("连接失败\n");}
try{
input=new BufferedReader(new InputStreamReader(cltSkt.getInputStream())); //获得输入流
output=new PrintStream(cltSkt.getOutputStream()); //获得输出流
}catch(IOException e){}
text.append("欢迎......\n");
}
public String getRequest(){
String frmClt=null;
try{
frmClt=input.readLine();
}catch(Exception e){
System.out.println("无法读取端口.....");
System.exit(0);
}
return frmClt;
}
public void sendResponse(String response){
try{
output.println("ECHO:"+response);
}catch(Exception e){
System.out.println("写端口失败......");
System.exit(0);
}
}
public static void CloseServer() throws Exception{
svrSkt.close();
}
}
class EchoClient{
private PrintStream ops; //输出流(指向服务器)
private BufferedReader ips;//输入流(来自服务器)
private JTextArea text = null;
public EchoClient(JTextArea clientText,String serverName,int port){
text = clientText;
try{
Socket clientSocket=new Socket(serverName,port); //根据服务器名和端口号建立Socket
ops=new PrintStream(clientSocket.getOutputStream());//获得Socket的输出流
ips=new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));//获得Socket的输入流
}catch(Exception e){
text.append("无法连接服务器!\n");
}
}
public void sendRequest(String request){
ops.println(request); //向Socket的输出流写入字符串
}
public String getResponse(){
String str=new String();
try{
str=ips.readLine(); //从Socket的输入流读入字符串
}catch(IOException e){} //必须捕获错误
return str;
}
}
public class XiTi19_1 {
public static void main(String args[]){
JFrame frame = new JFrame("ECHO图形界面");
final JButton startBut = new JButton("启动服务器");
final JButton stopBut = new JButton("停止服务器");
final JTextArea serverText = new JTextArea("服务器显示界面\n");
final JTextArea clientText = new JTextArea("客户端显示界面\n");
final JTextField inputField = new JTextField("这里输入信息",20);
final JButton sendBut = new JButton("发送");
//final EchoServer sa = new EchoServer(serverText,8888);
final EchoClient ca = new EchoClient(clientText,"localhost",8888);
JScrollPane scrServer = new JScrollPane(serverText,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
JScrollPane scrClient = new JScrollPane(clientText,JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
GridBagLayout gr = new GridBagLayout();
GridBagConstraints gc = new GridBagConstraints(); //创建一个名为gc的约束对象
frame.setLayout(gr); //将容器的布局设为GridBagLayout
gc.weighty =4;
gc.fill = GridBagConstraints.BOTH;
gc.gridwidth = GridBagConstraints.REMAINDER;//设置gridwidth参数的值为REMAINDER这样在后面使用该约束的组件将是该行的最后一个组件。
gr.setConstraints(scrServer, gc);
GridBagConstraints gc3 = new GridBagConstraints();
gc3.weighty =1;
gc3.weightx =3;
gr.setConstraints(startBut, gc3);
gc3.gridwidth = GridBagConstraints.REMAINDER;
gr.setConstraints(stopBut, gc3);