基本的Socket编程-基于Swing和AWT的客户端



import
java.net.ServerSocket; import java.net.Socket; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.IOException; import java.io.BufferedReader; import java.io.PrintStream; /** ServerSocket服务端 */ public class SimpleServer{ //服务端 ServerSocket serverSkt = null; //服务端accept返回的客户端对象 Socket clientSkt = null; //服务端Socket的输入流和输出流,客户端和服务端的输入流、输出流都是以对方为对象,都需要转换成可打印的字符流 BufferedReader in = null; PrintStream out = null; //Socker服务端是基于Socket类型的端口,所以在创建对象时需要参数:端口,还需要接收客户端请求和返回响应 public SimpleServer(int port){ System.out.println("服务端正在监听端口:"+port); try{ serverSkt = new ServerSocket(port); }catch(IOException e){ System.out.println("服务端端口"+port+"监听失败..."); e.printStackTrace(); } try{ //服务端返回的Socket对象 clientSkt = serverSkt.accept(); System.out.println("连接成功"); }catch(IOException e){ System.out.println("连接失败..."); e.printStackTrace(); } try{ in = new BufferedReader(new InputStreamReader(clientSkt.getInputStream())); out = new PrintStream(clientSkt.getOutputStream()); System.out.println("初始化成功"); }catch(IOException e){ e.printStackTrace(); } } //通过服务端Socket对象,获取客户端的请求内容 public String getRequest(){ String frmClt = null; try{ frmClt = in.readLine(); System.out.println("server收到的请求:"+frmClt); }catch(IOException e){ System.out.println("无法获取端口"); e.printStackTrace(); System.exit(1); } return frmClt; } //通过服务端socket对象,返回响应给客户端,响应内容自定义 public void sendResponse(String response){ //Prints a String and then terminate the line. System.out.println("服务端响应:"+response); out.println(response); } public static void main(String[] args){ SimpleServer sa = new SimpleServer(8888); //等待客户端的请求并自动返回全部响应 while(true){ sa.sendResponse(sa.getRequest()); } } }
import java.net.Socket;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

/**
Socket客户端
*/

public class SimpleClient{
    Socket clientSkt = null;
    //客户端Socket的输入流和输出流,客户端和服务端的输入流、输出流都是以对方为对象
    PrintStream out = null;
    BufferedReader in = null;
    
    //通过服务端IP和监听的端口进行连接
    public SimpleClient(String host, int port){
        try{
            clientSkt = new Socket(host, port);
            System.out.println("创建客户端实例对象");
            in = new BufferedReader(new InputStreamReader(clientSkt.getInputStream()));
            out = new PrintStream(clientSkt.getOutputStream());
        }catch(IOException e){
            System.out.println("无法连接服务器");
            e.printStackTrace();
            }
        }
    
    //发送请求
    public void sendRequest(String request){
        out.println(request);
        System.out.println("向服务端发送请求:"+request);
        }
        
    //接收响应
    public String getResponse(){
        String frmSer = null;
        try{
            frmSer = in.readLine();
            System.out.println("接收服务端响应:"+frmSer);
        }catch(IOException e){
            e.printStackTrace();
            }
        return frmSer;
        }    
    }
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.lang.System;

public class ClientFrame extends JFrame implements ActionListener{
    JButton sendButton;            //"发送"按钮
//    JTextField sendTextField;        //使用单行发送单个请求
    JTextArea sendTextField;            //使用多行发多个送请求
    static JTextArea responseTextArea;    //接收响应
    static SimpleClient client;            //客户端
    
    public ClientFrame(){
        JLabel sendLable = new JLabel();
        sendLable.setText("请求:");
//        sendTextField = new JTextField(20);        //声明单行请求编辑框的列
        sendTextField = new JTextArea(6, 30);        //声明多行请求编辑框的行和列
        sendTextField.setEditable(true);                //声明多行编辑框可编辑
        JScrollPane scrollPane1= new JScrollPane(sendTextField);
        
        JPanel panel1 = new JPanel();
        panel1.add(sendLable);
        panel1.add(scrollPane1);
        
        JLabel receiveLable = new JLabel();
        receiveLable.setText("响应:");
        responseTextArea = new JTextArea(6, 30);    //声明多行响应编辑框的行和列
        responseTextArea.setEditable(false);            //声明多行编辑框不可编辑
        JScrollPane scrollPane2= new JScrollPane(responseTextArea);
        JPanel panel2 = new JPanel();
        panel2.setLayout(new BorderLayout());
        panel2.add(receiveLable, BorderLayout.NORTH);
        panel2.add(scrollPane2, BorderLayout.CENTER);
        
        sendButton = new JButton();
        sendButton.setText("发送");
        sendButton.addActionListener(this);        //给"发送"按钮进行监听
        
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.add(panel1, BorderLayout.NORTH);
        panel.add(sendButton, BorderLayout.CENTER);
        panel.add(panel2, BorderLayout.SOUTH);
        
        this.setTitle("Socket客户端");
        this.getContentPane().add(panel);
        //关闭JFrame窗口时默认是隐藏窗口,需要更改默认行为
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
        
    public void actionPerformed(ActionEvent e){
        //The object on which the Event initially occurred.
        if(e.getSource()==sendButton){        //判断是否是"发送"按钮触发的事件
            client.sendRequest(sendTextField.getText());
            //界面responseTextArea动态更新展示内容 是以 按钮操作触发为参照,所以以换行发送的多请求,客户端每次顺序从发送队列获取请求发送,所以仅能顺序获取一个响应
            //Socket请求是以'\r'或者'\n'作为间隔符,换行也是多请求的间隔符
//            responseTextArea.append(client.getResponse() + "\n");
            }
        }
        
    public static void main(String[] args){
        ClientFrame cliFrame = new ClientFrame();
        //组件自动匹配大小
        cliFrame.pack();
        //建立和服务端的连接
        cliFrame.client = new SimpleClient("127.0.0.1",8888);
        cliFrame.setVisible(true);
        //接收服务端所有响应并等待服务端响应
        //按钮操作完以后,服务端返回的所有响应,客户端都全部接收,而且等待新的请求响应
        while(true){
            responseTextArea.append(client.getResponse() + "\n");
            }
        }        
    }

后台客户端执行结果:

G:\maul keyboard\network programming\base socket programming\ClientFrame>java ClientFrame
创建客户端实例对象
向服务端发送请求:sfa
接收服务端响应:sfa
向服务端发送请求:sfa
faf
gak
接收服务端响应:sfa
接收服务端响应:faf
接收服务端响应:gak

Swing和AWT的客户端执行结果:

服务端执行结果:

G:\maul keyboard\network programming\base socket programming\ClientFrame>java SimpleServer
服务端正在监听端口:8888
连接成功
初始化成功
server收到的请求:sfa
服务端响应:sfa
server收到的请求:sfa
服务端响应:sfa
server收到的请求:faf
服务端响应:faf
server收到的请求:gak
服务端响应:gak

猜你喜欢

转载自www.cnblogs.com/celine/p/10017356.html
今日推荐