java_net 网络聊天室及其问题解决

来源:互联网 发布:淘宝网前任总裁 编辑:程序博客网 时间:2024/06/05 20:53

昨天上课听老师讲跟着敲了一遍,然后晚上回宿舍自己整理思路敲了出来,但是却没有课堂上那么顺利。

自己找了大半天对比课堂上的笔记后发现,

PrintWriter  这个类里的 print 和 println 方法有小小不一样!

上网找了下,似乎是println 比 print 输出时加多了“\r\n”


首先创建了一个基类

import java.io.BufferedReader;import java.io.PrintWriter;import java.net.Socket;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JPanel;import javax.swing.JScrollPane;import javax.swing.JTextArea;import javax.swing.JTextField;public class Chat extends JFrame {/**创建带滚动条的文本区域*/protected JTextArea jta = new JTextArea();protected JScrollPane jsp = new JScrollPane(jta);protected JTextField jtf = new JTextField(20);protected JButton btn = new JButton("发送");protected JPanel  jp = new JPanel();protected Socket s ;protected PrintWriter pw;protected BufferedReader br;public Chat(String str){this.setTitle(str);this.initPane();this.setVisible(true);this.setSize(400,400);this.setDefaultCloseOperation(3);}protected void initPane() {this.add(jsp);this.jp.add(jtf);this.jp.add(btn);}}


服务端

import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.ServerSocket;import java.util.ArrayList;public class ChatServer extends Chat {//存放句柄ArrayList arr = new ArrayList();public ChatServer(String str){super(str);this.init();}private void init() {try{ServerSocket ss = new ServerSocket(3333);while(true){this.s = ss.accept();InputStream is = s.getInputStream();this.br = new BufferedReader(new InputStreamReader(is));OutputStream os = s.getOutputStream();this.pw = new PrintWriter(os);this.arr.add(pw);new Mythread().start();}}catch(Exception e){e.printStackTrace();}}class Mythread extends Thread{public void run(){try{while(true){String str = br.readLine();jta.append(str + "\r\n");for(int i=0;i<arr.size();i++){//取出每一个句柄,并把消息发送回去PrintWriter p = (PrintWriter)arr.get(i);p.println(str);p.flush();}}}catch(Exception e){e.printStackTrace();}}}public static void main(String[] args) {new ChatServer("服务端");}}

客户端
import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.PrintWriter;import java.net.Socket;public class ChatClient extends Chat{public ChatClient(String str){super(str);//添加显示面板this.add(jp,"South");btn.addActionListener(new Action());jtf.addActionListener(new Action());this.init();}private void init() {try{this.s = new Socket("127.0.0.1",3333);InputStream is = s.getInputStream();this.br = new BufferedReader(new InputStreamReader(is));OutputStream os = s.getOutputStream();this.pw = new PrintWriter(os);new Mythread().start();}catch(Exception e){e.printStackTrace();}}class Action implements ActionListener{public void actionPerformed(ActionEvent e) {if(e.getID()==1001||e.getActionCommand().equals("发送")){String str = jtf.getText();pw.println(str);pw.flush();jtf.setText("");}}}class Mythread extends Thread{public void run(){try{while(true){String str = br.readLine();jta.append(str +"\r\n");}}catch(Exception e){e.printStackTrace();}}}public static void main(String[] args) {new ChatClient("客户端");}}



以下是运行演示



流程大概就是:

客户端把消息输入到 JTextFiled 单行文本区域中,摁回车或者是发送就能把当前消息发送到服务端

服务端再把消息发还给每一个连接上的客户端,达到信息共享。


0 0