全部博文(403)
分类: Java
2006-08-01 19:08:23
这中间还用到了设计模式中的代理模式,还有一些jframe入门的东西
1 客户端
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JavaClient extends JFrame implements ActionListener//以JFrame为基类,实现ActionListener接口
{
JButton sendButton; //"发送"按钮
JTextField inputField; // 输入框
JTextArea outputArea; // 服务器返回框
static ClientAgent ca;
public JavaClient() //在建构函数中完成图形界面的初始化
{
inputField=new JTextField("这里输入..."); //供客户端输入的文本框
outputArea=new JTextArea("服务器返回"); //显示服务器返回数据的文本域
sendButton=new JButton("发送");
JPanel panel=new JPanel(); //新建面板
panel.setLayout(new BorderLayout()); //设置面板风格为BorderLayout
panel.add(inputField,BorderLayout.NORTH); //放置控件
panel.add(outputArea,BorderLayout.CENTER);
panel.add(sendButton,BorderLayout.PAGE_END);
sendButton.addActionListener(this);
setTitle("Java通讯客户端");
setContentPane(panel);
}
public static void main(String[] args)
{
JavaClient frame=new JavaClient();
frame.pack();//注意javaClient是JFrame的派生类,调用基类方法
frame.setVisible(true);
ca=new ClientAgent("127.0.0.1",1001); //传递服务器名称和端口号
}
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==sendButton) //判断事件源控件是否是"发送"按钮
{
try
{
ca.sendRequest(inputField.getText()); //发送文本框中的文本
System.out.println("send:"+inputField.getText());
}catch(Exception ex)
{
ex.printStackTrace();
}
outputArea.append("n"+ca.getResponse()); //接收服务器回应并写入文本域
}
}
}
2客户代理端
import java.io.*;
import java.net.*;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ClientAgent {
PrintStream ops; //输出流(指向服务器)
DataInputStream ips;//输入流(来自服务器)
BufferedReader bf;
String cltRequest;//客户端请求
String svrResponse;//服务器端回应
public ClientAgent(String serverName,int port)
{
try
{
Socket clientSocket=new Socket(serverName,port ); //根据服务器名和端口号建立Socket
ops=new PrintStream(clientSocket.getOutputStream());//获得Socket的输出流
//ips=new DataInputStream(clientSocket.getInputStream());//获得Socket的输入流
bf = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}
catch(Exception e)
{
System.out.println("无法连接服务器!");
}
}
public void sendRequest(String request)
{
ops.println(request); //向Socket的输出流写入字符串
}
public String getResponse()
{
String str=new String();
try
{
//str=ips.readLine(); //从Socket的输入流读入字符串
str = bf.readLine();
System.out.println("服务器返回:"+str);
}
catch(IOException e){} //必须捕获错误
return str;
}
}
3 服务器端
import java.io.*;
import java.net.*;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class ServerAgent {
ServerSocket svrSkt=null;
Socket cltSkt=null;
//DataInputStream input=null; //输入流,来自客户端
BufferedReader inf=null;
PrintStream output=null; //输出流,指向客户端
public ServerAgent(int port) //main()函数传递监听端口号
{
System.out.println("服务器代理正在监听,端口:"+port);
try
{
svrSkt=new ServerSocket(port); //开始监听
}catch(IOException e){System.out.println("监听端口"+port+"失败");}
try
{
cltSkt=svrSkt.accept(); //接收连接请求
}
catch(IOException e){System.out.println("连接失败");}
try
{
//input=new DataInputStream(cltSkt.getInputStream()); //获得输入流
inf = new BufferedReader(new InputStreamReader(cltSkt.getInputStream()));
output=new PrintStream(cltSkt.getOutputStream()); //获得输出流
}
catch(IOException e){}
output.println("欢迎......");
}
public String getRequest()
{
System.out.println("Server getRequest");
String frmClt=null;
try
{
//frmClt=input.readLine();
frmClt = inf.readLine();
System.out.println("After Server getRequest:"+frmClt);
}
catch(Exception e){
System.out.println("无法读取端口.....");
System.exit(0);
}
return frmClt;
}
public void sendResponse(String response)
{
System.out.println("Server sendResponse");
try
{
output.println(response);
}
catch(Exception e){
System.out.println("写端口失败......");
System.exit(0);
}
}
public static void main(String[] args) throws IOException
{
ServerAgent sa=new ServerAgent(1001);
while(true)
{
sa.sendResponse(sa.getRequest());
}
}
}