• java版两人聊天程序


    server.java

     1 import java.io.*;
     2 import java.net.*;
     3 import java.text.SimpleDateFormat;
     4 import java.util.*;
     5 import java.awt.*;
     6 import java.awt.event.ActionEvent;
     7 import java.awt.event.ActionListener;
     8 
     9 import javax.swing.*;
    10 
    11 @SuppressWarnings("serial")
    12 public class Server extends JFrame {
    13     DataInputStream inputFromClient;
    14     DataOutputStream outputToClient;
    15     // Button for send massage
    16     private JButton jbSend = new JButton("Send");
    17     // Text area for displaying contents
    18     private JTextArea jta = new JTextArea();
    19     // Text field for receiving radius
    20     private JTextField jtf = new JTextField();
    21     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    22     Date d = new Date();
    23     String time = format.format(d);
    24 
    25     public static void main(String[] args) {
    26         new Server();
    27     }
    28 
    29     public Server() {
    30         // Place text area on the frame
    31         setLayout(new BorderLayout());
    32         add(new JScrollPane(jta), BorderLayout.CENTER);
    33         JPanel p = new JPanel();
    34         p.setLayout(new BorderLayout());
    35         p.add(new JLabel("Message"), BorderLayout.WEST);
    36         p.add(jtf, BorderLayout.CENTER);
    37         jtf.setHorizontalAlignment(JTextField.LEFT);
    38         p.add(jbSend, BorderLayout.EAST);
    39         add(p, BorderLayout.SOUTH);
    40         jtf.addActionListener(new ButtonListener()); // Register listener
    41         jbSend.addActionListener(new ButtonListener()); // Register listener
    42         setTitle("Server");
    43         setSize(500, 300);
    44         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    45         setVisible(true); // It is necessary to show the frame here!
    46 
    47         try {
    48             // Create a server socket
    49             ServerSocket serverSocket = new ServerSocket(8001);
    50             jta.append(" ***Server started at  " + time +"***"+ '
    ');
    51 
    52             // Listen for a connection request
    53             Socket socket = serverSocket.accept();
    54 
    55             // Create data input and output streams
    56             inputFromClient = new DataInputStream(socket.getInputStream());
    57             outputToClient = new DataOutputStream(socket.getOutputStream());
    58 
    59             while (true) {
    60                 // Receive message from the client
    61                 String message = inputFromClient.readUTF();
    62                 jta.append(" " + time + '
    ');
    63                 jta.append(" Message: " + message + '
    ');
    64             }
    65         } catch (IOException ex) {
    66             jta.append(" ***端口已被占用!*** " + '
    ');
    67         }
    68     }
    69 
    70     public class ButtonListener implements ActionListener {
    71         public void actionPerformed(ActionEvent e) {
    72             try {
    73                 // Get the message from the text field
    74                 String message = jtf.getText().trim();
    75 
    76                 // Send the radius to the server
    77                 outputToClient.writeUTF(message);
    78                 outputToClient.flush();
    79 
    80                 // Display to the text area
    81                 jta.append(" " + time + '
    ');
    82                 jta.append(" MyMessage: " + message + "
    ");
    83                 jtf.setText(null);
    84             } catch (IOException ex) {
    85                 System.err.println(ex);
    86             }
    87         }
    88     }
    89 
    90 }

    client.java

     1 import java.io.*;
     2 import java.net.*;
     3 import java.text.SimpleDateFormat;
     4 import java.util.Date;
     5 import java.awt.*;
     6 import java.awt.event.*;
     7 import javax.swing.*;
     8 
     9 @SuppressWarnings("serial")
    10 public class Client extends JFrame {
    11     // Text field for receiving message
    12     private JTextField jtf = new JTextField();
    13 
    14     // Text area to display contents
    15     private JTextArea jta = new JTextArea();
    16     // Button for send massage
    17     private JButton jbSend = new JButton("Send");
    18     // IO streams
    19     private DataOutputStream toServer;
    20     private DataInputStream fromServer;
    21     SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    22     Date d = new Date();
    23     String time = format.format(d);
    24     
    25     public static void main(String[] args) {
    26         new Client();
    27     }
    28 
    29     public Client() {
    30         // Panel p to hold the label and text field
    31         JPanel p = new JPanel();
    32         p.setLayout(new BorderLayout());
    33         p.add(new JLabel("Message"), BorderLayout.WEST);
    34         p.add(jtf, BorderLayout.CENTER);
    35         jtf.setHorizontalAlignment(JTextField.LEFT);
    36         p.add(jbSend, BorderLayout.EAST);
    37         setLayout(new BorderLayout());
    38         add(p, BorderLayout.SOUTH);
    39         add(new JScrollPane(jta), BorderLayout.CENTER);
    40 
    41         jtf.addActionListener(new ButtonListener()); // Register listener
    42         jbSend.addActionListener(new ButtonListener()); // Register listener
    43 
    44         setTitle("Client");
    45         setSize(500, 300);
    46         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    47         setVisible(true); // It is necessary to show the frame here!
    48 
    49         try {
    50             // Create a socket to connect to the server
    51             Socket socket = new Socket("localhost", 8001);
    52 
    53             // Create an input stream to receive data from the server
    54             fromServer = new DataInputStream(socket.getInputStream());
    55 
    56             // Create an output stream to send data to the server
    57             toServer = new DataOutputStream(socket.getOutputStream());
    58 
    59             while (true) {
    60                 // Get message from the server
    61                 String message2 = fromServer.readUTF();
    62                 jta.append(" " + time + '
    ');
    63                 jta.append(" Message: " + message2 + '
    ');
    64             }
    65         } catch (IOException ex) {
    66             jta.append(" ***连接服务器失败!*** "+ '
    ');
    67         }
    68     }
    69 
    70     public class ButtonListener implements ActionListener {
    71         public void actionPerformed(ActionEvent e) {
    72             try {
    73                 // Get the message from the text field
    74                 String message = jtf.getText().trim();
    75 
    76                 // Send the message to the server
    77                 toServer.writeUTF(message);
    78                 toServer.flush();
    79                 
    80                 // Display to the text area
    81                 jta.append(" " + time + '
    ');
    82                 jta.append(" MyMessage: " + message + "
    ");
    83                 jtf.setText(null);
    84             } catch (IOException ex) {
    85                 System.err.println(ex);
    86             }
    87         }
    88     }
    89 }

    下载链接: http://pan.baidu.com/s/1mgBKGdU 密码: hkje

    作者:何海洋
    本博客内容主要以学习、研究和分享为主,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
  • 相关阅读:
    【Dart学习】-- Dart之匿名方法 & 回调函数 & 闭包
    【Dart学习】-- Dart之异常处理
    【Dart学习】-- Dart之操作符
    【Dart学习】-- Dart之注释
    【dart学习】-- Dart之元数据
    【Dart学习】-- Dart之泛型
    利用Aspose文档转图片
    结合仓库设计MVC控制器
    分享一个点赞超过100的漂亮ASP.NET MVC蓝色界面框架
    开发日记:接口开发设计
  • 原文地址:https://www.cnblogs.com/hehaiyang/p/3642718.html
Copyright © 2020-2023  润新知