java+socket 简易聊天工具
1、服务器端程序
package com.test3; import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class MyServer3 extends JFrame implements ActionListener{ /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new MyServer3(); } JTextArea jta = null; JTextField jtf = null; JButton sendBtn = null; JPanel jp = null; JScrollPane jsp = null; //把信息发给客户端的对象 PrintWriter pw = null; public MyServer3() { // jta = new JTextArea(); jtf = new JTextField(20); sendBtn = new JButton("send"); sendBtn.addActionListener(this); sendBtn.setActionCommand("send"); jsp = new JScrollPane(jta); jp = new JPanel(); jp.add(jtf); jp.add(sendBtn); this.add(jsp,BorderLayout.CENTER); this.add(jp,BorderLayout.SOUTH); this.setSize(400, 300); this.setTitle("QQ简易聊天-服务器端"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(false); try { //服务器监听 ServerSocket ss = new ServerSocket(9988); //等待客户端连接 Socket s = ss.accept(); BufferedReader br = new BufferedReader (new InputStreamReader(s.getInputStream())); pw = new PrintWriter(s.getOutputStream(),true); //读取从客户端发来的信息 while(true) { String info = br.readLine(); jta.append("client "+getTime()+"\r\n"+info+"\r\n"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().equals("send")) { //把服务器在jtf写的内容发送给客户端 String info = jtf.getText(); jta.append("server "+getTime()+"\r\n"+info+"\r\n"); pw.println(info); //清空发送框内容 jtf.setText(""); } } //获取系统时间 public String getTime() { Calendar c = Calendar.getInstance();//可以对每个时间域单独修改 int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); return hour+":"+ minute+":"+second; } }
2、客户端程序
package com.test3; import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; public class MyClient3 extends JFrame implements ActionListener{ /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new MyClient3(); } JTextArea jta = null; JTextField jtf = null; JButton sendBtn = null; JPanel jp = null; JScrollPane jsp = null; PrintWriter pw = null; public MyClient3() { // jta = new JTextArea(); jtf = new JTextField(20); sendBtn = new JButton("send"); sendBtn.addActionListener(this); sendBtn.setActionCommand("send"); jsp = new JScrollPane(jta); jp = new JPanel(); jp.add(jtf); jp.add(sendBtn); this.add(jsp,BorderLayout.CENTER); this.add(jp,BorderLayout.SOUTH); this.setSize(400, 300); this.setTitle("QQ简易聊天-客户端"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(false); try { Socket s = new Socket("127.0.0.1",9988); BufferedReader br = new BufferedReader (new InputStreamReader(s.getInputStream())); pw = new PrintWriter(s.getOutputStream(),true); while(true) { //不停的读取服务器端发来的信息 String info = br.readLine(); jta.append("server "+getTime()+"\r\n"+info+"\r\n"); } } catch (Exception e) { // TODO: handle exception } } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub if(e.getActionCommand().equals("send")) { String info = jtf.getText(); //把客户端发送的信息显示到jta jta.append("client "+getTime()+"\r\n"+info+"\r\n"); pw.println(info); jtf.setText(""); } } //获取系统时间 public String getTime() { Calendar c = Calendar.getInstance();//可以对每个时间域单独修改 int hour = c.get(Calendar.HOUR_OF_DAY); int minute = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); return hour+":"+ minute+":"+second; } }
3、演示