Socket套接字及多线程实现在线群聊示例
Posted dustar96
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Socket套接字及多线程实现在线群聊示例相关的知识,希望对你有一定的参考价值。
代码:
Server.java
package com.etc; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.ServerSocket; import java.net.Socket; import java.util.Date; public class Service { //回车符 static final String CRLF=" "; //空格符 static final String BLANK=" "; public static void main(String[] args) throws IOException { //新建服务端 ServerSocket server=new ServerSocket(3001); while(true) { //接收请求 Socket socket = server.accept(); System.out.println("server start..."); //拿到请求流 InputStream inputStream = socket.getInputStream(); //存储流的字节数组 byte[] datas = new byte[1024 * 1024 * 10]; int length = inputStream.read(datas); //拿到请求信息 if(length > 0) { String str = new String(datas, 0, length); System.out.println(str); } //响应流 BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); //请求体 StringBuilder str1 = new StringBuilder(); str1.append("<html>"); str1.append("<head>"); str1.append("<title>respose</title>"); str1.append("</head>"); str1.append("<body>"); str1.append("我是中国人Hello World!"); str1.append("</body>"); str1.append("</html>"); //请求头 StringBuilder str2 = new StringBuilder(); str2.append("HTTP/1.1").append(BLANK).append(200).append(BLANK); str2.append("OK"); str2.append(CRLF); //2) 响应头(Response Head) str2.append("Server:bjsxt Server/0.0.1").append(CRLF); str2.append("Date:").append(new Date()).append(CRLF); str2.append("Content-type:text/html;charset=utf-8").append(CRLF); //正文长度 :字节长度 str2.append("Content-Length:").append(length).append(CRLF); str2.append(CRLF); //分隔符 //将请求头请求体写出 bw.append(str2.toString()); bw.append(str1.toString()); bw.flush(); } } }
Client.java
package com.du; import javax.swing.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.Socket; /** * 图形界面客户端 */ public class Client extends JFrame { private JTextField textField; private JTextArea textArea; private JButton button; private DataOutputStream dos = null;// 数据的输出流,用来写入数据 public Client() { setTitle("在线群聊"); getContentPane().setLayout(null); JScrollPane scrollPane = new JScrollPane(); scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); scrollPane.setBounds(12, 10, 506, 242); setBounds(10, 10, 544, 418);//设置窗口的出现坐标及它的宽与长 setLocationRelativeTo(null);//居中显示 getContentPane().add(scrollPane); textArea = new JTextArea(); scrollPane.setViewportView(textArea); textArea.setEditable(false); textArea.setRows(10); textField = new JTextField(); textField.setBounds(23, 308, 240, 21); getContentPane().add(textField); textField.setColumns(10); button = new JButton("发言"); button.addActionListener(new ActionListener() {//点击按钮,发言聊天的内容 public void actionPerformed(ActionEvent e) { String info = textField.getText(); try { dos.writeUTF(info);//传到服务器上去内容 dos.flush(); textField.setText("");//清空文本框 } catch (IOException e1) { e1.printStackTrace(); } } }); button.setBounds(330, 308, 93, 23); getContentPane().add(button); setVisible(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public static void main(String[] args) { Client client = new Client(); client.clientTest(); } public void clientTest() { Socket client = null; DataInputStream dis = null;// 数据的输入流,用来读取数据 try { // 得到套接字对象第一个参数是服务端的地址,第二个参数是端口号 client = new Socket("127.0.0.1", 6666); // 得到数据的输出流 dos = new DataOutputStream(client.getOutputStream()); // 得到数据的输入流 dis = new DataInputStream(client.getInputStream()); new ClientThread(dis).start();//专门读取从服务器端传递过来的信息 } catch (Exception e) { e.printStackTrace(); } } //客户端读取服务器端发送过来的内容以线程的形式 class ClientThread extends Thread{ private DataInputStream dis; public ClientThread(DataInputStream dis) { this.dis = dis; } @Override public void run() { try { while(true) { String info = dis.readUTF();//读取服务端传递过来信息 textArea.append(info+" ");//在多文本框中显示内容,并加回车换行符 } }catch(Exception e) { e.printStackTrace(); }finally { if(dis != null) try { dis.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
以上是关于Socket套接字及多线程实现在线群聊示例的主要内容,如果未能解决你的问题,请参考以下文章