这是从另一个论坛的代码修改的原来有很多错误我已经修改供大家欣赏!
这中间还用到了设计模式中的代理模式还有一些jframe入门的东西
客户端
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(); //新建面板
panelsetLayout(new BorderLayout()); //设置面板风格为BorderLayout
paneladd(inputFieldBorderLayoutNORTH); //放置控件
paneladd(outputAreaBorderLayoutCENTER);
paneladd(sendButtonBorderLayoutPAGE_END);
sendButtonaddActionListener(this);
setTitle(Java通讯客户端);
setContentPane(panel);
}
public static void main(String[] args)
{
JavaClient frame=new JavaClient();
framepack();//注意javaClient是JFrame的派生类调用基类方法
framesetVisible(true);
ca=new ClientAgent(); //传递服务器名称和端口号
}
public void actionPerformed(ActionEvent ae)
{
if(aegetSource()==sendButton) //判断事件源控件是否是发送按钮
{
try
{
casendRequest(inputFieldgetText()); //发送文本框中的文本
Systemoutprintln(send:+inputFieldgetText());
}catch(Exception ex)
{
exprintStackTrace();
}
outputAreaappend(\n+cagetResponse()); //接收服务器回应并写入文本域
}
}
}
客户代理端
import javaio*;
import *;
/**
* @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 serverNameint port)
{
try
{
Socket clientSocket=new Socket(serverNameport ); //根据服务器名和端口号建立Socket
ops=new PrintStream(clientSocketgetOutputStream());//获得Socket的输出流
//ips=new DataInputStream(clientSocketgetInputStream());//获得Socket的输入流
bf = new BufferedReader(new InputStreamReader(clientSocketgetInputStream()));
}
catch(Exception e)
{
Systemoutprintln(无法连接服务器!);
}
}
public void sendRequest(String request)
{
opsprintln(request); //向Socket的输出流写入字符串
}
public String getResponse()
{
String str=new String();
try
{
//str=ipsreadLine(); //从Socket的输入流读入字符串
str = bfreadLine();
Systemoutprintln(服务器返回+str);
}
catch(IOException e){} //必须捕获错误
return str;
}
}
服务器端
import javaio*;
import *;
/**
* @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()函数传递监听端口号
{
Systemoutprintln(服务器代理正在监听端口+port);
try
{
svrSkt=new ServerSocket(port); //开始监听
}catch(IOException e){Systemoutprintln(监听端口+port+失败);}
try
{
cltSkt=svrSktaccept(); //接收连接请求
}
catch(IOException e){Systemoutprintln(连接失败);}
try
{
//input=new DataInputStream(cltSktgetInputStream()); //获得输入流
inf = new BufferedReader(new InputStreamReader(cltSktgetInputStream()));
output=new PrintStream(cltSktgetOutputStream()); //获得输出流
}
catch(IOException e){}
outputprintln(欢迎);
}
public String getRequest()
{
Systemoutprintln(Server getRequest);
String frmClt=null;
try
{
//frmClt=inputreadLine();
frmClt = infreadLine();
Systemoutprintln(After Server getRequest:+frmClt);
}
catch(Exception e){
Systemoutprintln(无法读取端口);
Systemexit();
}
return frmClt;
}
public void sendResponse(String response)
{
Systemoutprintln(Server sendResponse);
try
{
outputprintln(response);
}
catch(Exception e){
Systemoutprintln(写端口失败);
Systemexit();
}
}
public static void main(String[] args) throws IOException
{
ServerAgent sa=new ServerAgent();
while(true)
{
sasendResponse(sagetRequest());
}
}
}