java网络编程(三):一个类似QQ的聊天程序
客户端:
package QQ; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; /** * Created by hu on 2015/11/8. */ //QQ类似聊天程序的客户端 public class Client extends JFrame { static JTextArea area; JTextField field; JButton button; static PrintWriter writer; public Client() { this.setTitle("客户端"); this.setSize(400,500); area=new JTextArea(25,30); area.setEditable(false); field=new JTextField(20); button=new JButton("提交"); JScrollPane sp=new JScrollPane(area); JPanel panel=new JPanel(); this.add(sp, BorderLayout.CENTER); panel.add(field); panel.add(button); this.add(panel,BorderLayout.SOUTH); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String text=field.getText(); writer.println(text); area.append("me:"+text+"\n"); field.setText(""); } }); } public static void main(String[] args)throws Exception{ Socket socket=new Socket("127.0.0.1",8899); Client client=new Client(); OutputStream out=socket.getOutputStream(); BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); writer=new PrintWriter(out,true); client.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) { } @Override public void windowClosing(WindowEvent e) { System.out.println("closing"); try { socket.shutdownInput(); socket.shutdownOutput(); socket.close(); writer.close(); reader.close(); }catch (Exception exception){ exception.printStackTrace(); } } @Override public void windowClosed(WindowEvent e) { System.out.println("closed"); } @Override public void windowIconified(WindowEvent e) { } @Override public void windowDeiconified(WindowEvent e) { } @Override public void windowActivated(WindowEvent e) { } @Override public void windowDeactivated(WindowEvent e) { } }); while (true){ try{ String line=reader.readLine(); area.append("服务器:"+line+"\n"); }catch (Exception exception){ System.out.println("客户端程序已经关闭!"); break; } } } }
服务端
package QQ; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; /** * Created by hu on 2015/11/8. */ /* * qq类似聊天程序的服务端 * */ public class Server extends JFrame{ static JTextArea area; JTextField field; JButton button; static PrintStream writer; public Server(){ //设置窗口的大小和标题 this.setTitle("服务器"); this.setSize(400,500); //设置文本区域的大小并且设置为不可编辑 area=new JTextArea(25,30); area.setEditable(false); //设置文本输入框的大小 field=new JTextField(20); //设置按钮的 button=new JButton("提交"); /* * JPanel默认布局的是FlowLayout * 可以new的时候就定义成其他布局: * JPanel p=new JPanel(new GridLayout(2,1));//两行一列 * JPanel p=new JPanel(new BorderLayout()); * */ JPanel panel=new JPanel(); JScrollPane sp=new JScrollPane(area); this.add(sp, BorderLayout.CENTER); panel.add(field); panel.add(button); this.add(panel,BorderLayout.SOUTH); this.setVisible(true); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //为按钮添加事件 button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String text=field.getText(); writer.println(text); area.append("seven_hu:"+text+"\n"); field.setText(""); } }); } public static void main(String[] args)throws Exception{ Server server=new Server(); ServerSocket serverSocket=new ServerSocket(8899); System.out.println("Starting listening..."); while (true){ Socket socket=serverSocket.accept(); //获取客户端主机名 InetAddress address=socket.getInetAddress(); String name=address.getLocalHost().getHostName(); System.out.println(name+"已连接"); BufferedReader reader=new BufferedReader(new InputStreamReader(socket.getInputStream())); writer=new PrintStream(socket.getOutputStream(),true); String line=null; line=reader.readLine(); if(line!=null){ area.append("client:"+line+"\n"); } } } }