在java 中实现通信是利用java.net包
利用服务端ServerSocket和客户端Socket类来通信,它是建立在tcp协议基础上,面向字节的稳定的通信
首先是客户端Socket,它是连接的发起者。
public class ClientAgent {
DataOutputStream ops; //输出流(指向服务器)
DataInputStream ips;//输入流(来自服务器)
String cltRequest;//客户端请求
String svrResponse;//服务器端回应
Socket clientSocket=null;
public ClientAgent(String serverName,int port)
{
try
{
clientSocket=new Socket(serverName,port ); //根据服务器名和端口号建立Socket
System.out.println("客户代理请求服务端成功!");
ops=new DataOutputStream(clientSocket.getOutputStream());//获得Socket的输出流
ips=new DataInputStream(clientSocket.getInputStream());//获得Socket的输入流
}
catch(Exception e)
{
System.out.println("无法连接服务器!");
}
}
public void sendRequest(String request)
{
try {
ops.writeUTF(request);
} catch (IOException e) {
System.out.println("客户端转换字节数组异常!");
}
}
public String getResponse()
{
String str=new String();
try
{
str=ips.readUTF();
clientSocket.close();
}
catch(IOException e){}
return str;
}
}
客户端界面
public class JavaClient extends JFrame implements ActionListener {
private JButton sendButton; //"发送"按钮
private JTextField inputField; // 输入框
private JTextArea outputArea; // 服务器返回框
private String serverAddress="localhost";//默认地址
private int port=1001;//默认端口
public JavaClient(String address,int port,String tittle)
{
this.serverAddress = address;
this.port=port;
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.SOUTH);
setTitle(tittle);
setContentPane(panel);
this.addWindowListener(new WindowListener(){
public void windowActivated(WindowEvent e) {
}
public void windowClosed(WindowEvent e) {
}
public void windowClosing(WindowEvent e) {
System.exit(0);
}
public void windowDeactivated(WindowEvent e) {
}
public void windowDeiconified(WindowEvent e) {
}
public void windowIconified(WindowEvent e) {
}
public void windowOpened(WindowEvent e) {
}
});
this.sendButton.addActionListener(this);
}
public void actionPerformed(ActionEvent e) {
ClientAgent clientAgent = new ClientAgent(serverAddress,port);
clientAgent.sendRequest(inputField.getText());
String response=clientAgent.getResponse();
outputArea.append("\n"+response);
}
public static void main(String[] args) {
JavaClient frame = new JavaClient("localhost",1001,"客户端");
frame.setBounds(100, 100, 100, 200);
frame.setVisible(true);
}
服务端处理客户端发出的请求,服务端应先打开 调用cltSkt=svrSkt.accept();
public class ServerAgent {
//private int port=1001;
private ServerSocket svrSkt=null;
private Socket cltSkt=null;
private DataInputStream input=null; //输入流,来自客户端
private DataOutputStream output=null; //输出流,指向客户端
public ServerAgent(int port)
{
System.out.println("服务器代理正在监听,端口:"+port);
try {
svrSkt = new ServerSocket(port);
} catch (IOException e) {
System.out.println("监听端口"+port+"失败");
}
try {
cltSkt=svrSkt.accept();//服务端当前线程挂起,等待客户请求
System.out.println("服务端获得客户端socket");
} catch (IOException e) {
System.out.println("连接失败");
}
try
{
input=new DataInputStream(cltSkt.getInputStream()); //获得输入流
output=new DataOutputStream(cltSkt.getOutputStream()); //获得输出流
}
catch(IOException e){}
}
public String getRequest()
{
String frmClt=null;
try
{
frmClt= input.readUTF();
System.out.println("readinfo...."+frmClt);
}
catch(Exception e){
System.out.println("无法读取端口.....");
System.exit(0);
}
return frmClt;
}
public void sendResponse(String response)
{
try
{
System.out.println("writeinfo...." +response);
output.writeUTF(response);
svrSkt.close();
}
catch(Exception e){
System.out.println("写端口失败......");
System.exit(0);
}
}
public Socket getCltSkt() {
return cltSkt;
}
public void setCltSkt(Socket cltSkt) {
this.cltSkt = cltSkt;
}
public ServerSocket getSvrSkt() {
return svrSkt;
}
public void setSvrSkt(ServerSocket svrSkt) {
this.svrSkt = svrSkt;
}
}
开起个线程为客户端服务
public class ServerThread extends Thread {
private boolean ifRun=true;
private int port;
public ServerThread(int port)
{
this.port=port;
}
public boolean isIfRun() {
return ifRun;
}
public void setIfRun(boolean ifRun) {
this.ifRun = ifRun;
}
public void run() {
while(true){
try {
sleep(200);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
System.out.println(this.currentThread());
ServerAgent serverAgent = new ServerAgent(port);
serverAgent.sendResponse(serverAgent.getRequest());
}
}
public static void main(String[] args) {
ServerThread operate = new ServerThread(1001);
operate.start();
}
}