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 +"***"+ ‘\n‘); 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 + ‘\n‘); 63 jta.append(" Message: " + message + ‘\n‘); 64 } 65 } catch (IOException ex) { 66 jta.append(" ***端口已被占用!*** " + ‘\n‘); 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 + ‘\n‘); 82 jta.append(" MyMessage: " + message + "\n"); 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 + ‘\n‘); 63 jta.append(" Message: " + message2 + ‘\n‘); 64 } 65 } catch (IOException ex) { 66 jta.append(" ***连接服务器失败!*** "+ ‘\n‘); 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 + ‘\n‘); 82 jta.append(" MyMessage: " + message + "\n"); 83 jtf.setText(null); 84 } catch (IOException ex) { 85 System.err.println(ex); 86 } 87 } 88 } 89 }
下载链接: http://pan.baidu.com/s/1mgBKGdU 密码: hkje
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。