Java基于socket的进程间通信 聊天小程序

Client端:

  1 package mylab;
  2 
  3 import java.awt.BorderLayout;
  4 import java.awt.Dimension;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 import java.io.BufferedReader;
  8 import java.io.IOException;
  9 import java.io.InputStreamReader;
 10 import java.io.PrintWriter;
 11 import java.net.Socket;
 12 
 13 import javax.swing.JButton;
 14 import javax.swing.JFrame;
 15 import javax.swing.JPanel;
 16 import javax.swing.JScrollPane;
 17 import javax.swing.JTextArea;
 18 import javax.swing.ScrollPaneConstants;
 19 
 20 public class Client extends JFrame implements ActionListener {
 21     JTextArea input;
 22     JTextArea output;
 23     JButton close;
 24     JButton send;
 25     JScrollPane top = new JScrollPane();
 26     JPanel medium, bottom;
 27 
 28     String inMessage, outMessage;
 29 
 30     String s = null;
 31     Socket mysocket;
 32     BufferedReader br = null;
 33     PrintWriter pw = null;
 34 
 35     /**
 36      * @param args
 37      */
 38     public Client() {
 39         super();
 40         setTitle("客户端");
 41         setVisible(true);
 42         setBounds(200, 150, 350, 400);
 43         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 44         
 45         top = new JScrollPane();
 46         top.setPreferredSize(new Dimension(300, 200));
 47         top.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
 48         output = new JTextArea(6, 25);
 49         top.setViewportView(output);
 50 
 51         medium = new JPanel();
 52         medium.setPreferredSize(new Dimension(300, 120));
 53         input = new JTextArea(4, 27);
 54         medium.add(input);
 55 
 56         bottom = new JPanel();
 57         bottom.setPreferredSize(new Dimension(300, 60));
 58         close = new JButton("关闭");
 59         close.addActionListener(this);
 60         send = new JButton("发送");
 61         send.addActionListener(this);
 62         bottom.add(close);
 63         bottom.add(send);
 64 
 65         getContentPane().add(top, BorderLayout.NORTH);
 66         getContentPane().add(medium, BorderLayout.CENTER);
 67         getContentPane().add(bottom, BorderLayout.SOUTH);
 68     }
 69 
 70     public static void main(String[] args) {
 71         // TODO Auto-generated method stub
 72         Client client = new Client();
 73         client.run();
 74     }
 75 
 76     public void run() {
 77 
 78         try {
 79 
 80             mysocket = new Socket("127.0.0.1", 2222);
 81             br = new BufferedReader(new InputStreamReader(
 82                     mysocket.getInputStream()));
 83             pw = new PrintWriter(mysocket.getOutputStream(), true);
 84             while ((inMessage = br.readLine()) != null) {
 85                 output.append("服务器说:" + inMessage + "\n");
 86             }
 87 
 88         } catch (Exception e) {
 89         }
 90     }
 91 
 92     @Override
 93     public void actionPerformed(ActionEvent e) {
 94         // TODO Auto-generated method stub
 95         if (close.hasFocus()) {
 96             try {
 97                 mysocket.close();
 98                 System.out.println("客户端已关闭");
 99             } catch (IOException e1) {
100                 // TODO Auto-generated catch block
101                 e1.printStackTrace();
102             }
103         }
104         if (send.hasFocus()) {
105             outMessage = input.getText();
106             input.setText(null);
107             output.append("客户端说:" + outMessage + "\n");
108             pw.println(outMessage);
109             pw.flush();
110         }
111     }
112 
113 }
View Code

Server端:

  1 package mylab;
  2 
  3 import java.awt.BorderLayout;
  4 import java.awt.Dimension;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 import java.io.BufferedReader;
  8 import java.io.IOException;
  9 import java.io.InputStreamReader;
 10 import java.io.PrintWriter;
 11 import java.net.ServerSocket;
 12 import java.net.Socket;
 13 
 14 import javax.swing.JButton;
 15 import javax.swing.JFrame;
 16 import javax.swing.JPanel;
 17 import javax.swing.JScrollPane;
 18 import javax.swing.JTextArea;
 19 import javax.swing.ScrollPaneConstants;
 20 
 21 public class Server extends JFrame implements ActionListener {
 22     JTextArea input;//显示信息
 23     JTextArea output;//输入信息
 24     JButton close;//关闭按钮
 25     JButton send;//发动按钮
 26     JScrollPane top = new JScrollPane();//放input的滚动面板
 27     JPanel medium, bottom;//放output,关闭按钮,发送按钮
 28 
 29     String inMessage, outMessage;
 30     ServerSocket server = null;
 31     Socket you = null;
 32     String s = null;
 33     PrintWriter pw = null;
 34     BufferedReader br = null;
 35 
 36     /**
 37      * @param args
 38      */
 39     public Server() {
 40         super();
 41         setTitle("服务器");
 42         setVisible(true);
 43         setBounds(600, 150, 350, 400);
 44         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 45         
 46         top = new JScrollPane();
 47         top.setPreferredSize(new Dimension(300, 200));
 48         top.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
 49         output = new JTextArea(6, 25);
 50         top.setViewportView(output);
 51 
 52         medium = new JPanel();
 53         medium.setPreferredSize(new Dimension(300, 120));
 54         input = new JTextArea(4, 27);
 55         medium.add(input);
 56 
 57         bottom = new JPanel();
 58         bottom.setPreferredSize(new Dimension(300, 60));
 59         close = new JButton("关闭");
 60         close.addActionListener(this);
 61         send = new JButton("发送");
 62         send.addActionListener(this);
 63         bottom.add(close);
 64         bottom.add(send);
 65 
 66         getContentPane().add(top, BorderLayout.NORTH);
 67         getContentPane().add(medium, BorderLayout.CENTER);
 68         getContentPane().add(bottom, BorderLayout.SOUTH);
 69     }
 70 
 71     public static void main(String[] args) {
 72         // TODO Auto-generated method stub
 73         Server sever = new Server();
 74         sever.run();
 75     }
 76 
 77     public void run() {
 78 
 79         try {
 80 
 81             server = new ServerSocket(2222);
 82         } catch (IOException e) {
 83             System.out.println(e);
 84         }
 85         try {
 86             you = server.accept();
 87             br = new BufferedReader(new InputStreamReader(you.getInputStream()));
 88             pw = new PrintWriter(you.getOutputStream(), true);
 89             while ((inMessage = br.readLine()) != null) {
 90                 output.append("客户端说:" + inMessage + "\n");
 91             }
 92         } catch (Exception e) {
 93         }
 94 
 95     }
 96 
 97     @Override
 98     public void actionPerformed(ActionEvent e) {
 99         // TODO Auto-generated method stub
100         if (close.hasFocus()) {
101             try {
102                 server.close();
103                 System.out.println("服务器已关闭");
104             } catch (IOException e1) {
105                 // TODO Auto-generated catch block
106                 e1.printStackTrace();
107             }
108         }
109         if (send.hasFocus()) {
110             outMessage = input.getText();
111             input.setText(null);
112             output.append("服务器说:" + outMessage + "\n");
113             pw.println(outMessage);
114             pw.flush();
115         }
116     }
117 
118 }
View Code

 

转载于:https://www.cnblogs.com/lantian729308328/p/5659434.html

你可能感兴趣的:(Java基于socket的进程间通信 聊天小程序)