博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java基于socket的进程间通信 聊天小程序
阅读量:4586 次
发布时间:2019-06-09

本文共 7339 字,大约阅读时间需要 24 分钟。

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 block101                 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 stub100         if (close.hasFocus()) {101             try {102                 server.close();103                 System.out.println("服务器已关闭");104             } catch (IOException e1) {105                 // TODO Auto-generated catch block106                 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

你可能感兴趣的文章
Git使用3(Git操作完整版)
查看>>
sql报错注入:extractvalue、updatexml报错原理
查看>>
C# this.Hide()
查看>>
sqlmap的学习之路-自动化测试SQL注入工具
查看>>
Java 内存管理、JVM 工作原理与 Java 运行时系统
查看>>
矩阵分解(matrix factorization)
查看>>
大型网站的架构设计与演进
查看>>
二值化函数
查看>>
‘3 sigma’rule(68–95–99.7 rule)
查看>>
内存、时间复杂度、CPU/GPU以及运行时间
查看>>
DES加密解决算法
查看>>
【并发编程】延时初始化
查看>>
编程珠玑--左旋字符串
查看>>
【黑金原创教程】【FPGA那些事儿-驱动篇I 】实验十四:储存模块
查看>>
模板 - 字符串 - Manacher
查看>>
2017.1.2
查看>>
Ice_cream's world I
查看>>
串并行数据结构实验--MAC下SML环境安装1
查看>>
java取整和java四舍五入方法
查看>>
学习linux-基础-操作系统结构
查看>>