java在线聊天项目0.5版 解决客户端向服务器端发送信息时只能发送一次问题 OutputStreamWriter DataOutputStream socket.getOutputStream()
没有解决问题之前客户端代码:
package com.swift; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedWriter; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class ChatClientFrame2 extends JFrame { private static final long serialVersionUID = -118470059355655240L; Socket s=null; JLabel label_shang=new JLabel(); JLabel label_xia=new JLabel(); JTextField tf = new JTextField(38); JTextArea ta = new JTextArea(15, 50); JButton button=new JButton(); public ChatClientFrame2() { setBounds(200, 200, 500, 400); setTitle("客户端聊天工具 —— 0.4"); //对窗口进行大的布局,分为三行一列,在pBasic面板上添加三个面板shang zhong xia JPanel pBasic=new JPanel(); pBasic.setLayout(new BorderLayout());//不设置默认也是这种布局模式 setContentPane(pBasic);//把面板放在窗口上,不记得用this.关键字 JPanel shang=new JPanel(); JPanel zhong=new JPanel(); JPanel xia=new JPanel(); //设置JPanel面板的大小 shang.setSize(470, 25); zhong.setSize(470, 180); xia.setSize(470, 40); pBasic.add(shang,BorderLayout.NORTH); pBasic.add(zhong,BorderLayout.CENTER); pBasic.add(xia,BorderLayout.SOUTH); shang.setBackground(Color.red); zhong.setBackground(Color.yellow); xia.setBackground(Color.blue); /* * 三个面板,上边放一个标签“聊天记录”,中间放一个文本域, * 下边分为左中右——分别放标签“输入信息“,文本框和”发送“按钮 */ label_shang.setText("聊天记录"); shang.add(label_shang); ta.setLineWrap(true);// 自动换行 JScrollPane scroll=new JScrollPane(ta);// 增加滚动条,以便不增加行数 zhong.add(scroll); label_xia.setText("输入信息"); xia.add(label_xia,BorderLayout.WEST); /* * 增加功能,窗口监听事件,窗口打开时设置光标焦点在tf文本域中 */ this.addWindowListener(new WindowAdapter() { @Override public void windowOpened(WindowEvent e) { tf.requestFocus(); } }); xia.add(tf,BorderLayout.CENTER); button.setText("发送"); xia.add(button,BorderLayout.EAST); /* * 增加“发送”按钮的功能,增加回车的功能,监听相同, * 则使用内部类实现,以提高代码复用性 */ final class ShareListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { String taText=ta.getText(); String tfText=tf.getText()+"\r\n"; ta.setText(taText+tfText); tf.setText(""); //当回车或发送按钮时,tfText发送到服务器 try { //可以尝试用DataOutputStream里的writeUTF方法 // DataOutputStream ds=new DataOutputStream(s.getOutputStream()); // ds.writeUTF(tfText); // ds.flush(); // ds.close(); BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream(),"utf-8")); bw.write(tfText); bw.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } button.addActionListener(new ShareListener()); tf.addActionListener(new ShareListener()); //通过压缩自动调整各个面板 pack(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 点关闭按钮同时退出程序 setVisible(true); //创建窗体直接调用连接服务器 connect(); } /* * 增加一个连接到服务器的方法 */ public void connect() { try { s=new Socket("127.0.0.1",8888); System.out.println("connected!"); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // 别忘了创建窗体对象,也可以用生成对象调用其他的方法如launchFrame() new ChatClientFrame2(); } }
没有解决问题之前服务端代码:
package com.swift; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class ChatServer { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8888); for (;;) { Socket s = ss.accept(); //当有连接,则显示,用于测试 System.out.println("a client connected success"); //可以尝试用DataInputStream中readUTF方法 // DataInputStream dis=new DataInputStream(s.getInputStream()); // String str=dis.readUTF(); // dis.close(); BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8")); String str=br.readLine(); System.out.println(str); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
演示过程
出现问题:
java.net.SocketException: Socket is closed
如果不关闭流
把这句删掉bw.close();
是可以的,程序不会出错,但总要关闭,可以在客户端窗口关闭的时候执行这句。
继续运行发现还是只能发送一条信息,但没有出错提示。这时为什么呢?
查看程序发现,当每次点击回车或者发送按钮,都会有一条信息发送,但服务器就是没有显示,因为客户端发送了多次,服务端只读取了一次,所以显示一条,
下边代码是读取多次,显示多次:
package com.swift; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class ChatServer { public static void main(String[] args) { try { ServerSocket ss = new ServerSocket(8888); for (;;) { Socket s = ss.accept(); //当有连接,则显示,用于测试 System.out.println("a client connected success"); BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8")); //问题:客户端多次发送信息,而服务器端只读取了一次,所以只输出一次,多次输出下面的办法也不好 String str=br.readLine(); System.out.println(str); String str1=br.readLine(); System.out.println(str1); String str2=br.readLine(); System.out.println(str2); String str3=br.readLine(); System.out.println(str3); } } catch (IOException e) { e.printStackTrace(); } } }
接收了四次
可以增加一个死循环来无限接收
成功解决接收次数问题,可后边的关闭不能执行了
修改服务端代码如下:
0.5版效果图如下:
0.5版服务端代码:
package com.swift; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; public class ChatServer { public static void main(String[] args) { boolean started=false; try { ServerSocket ss = new ServerSocket(8888); started=true; while(started) { boolean connected=false; Socket s = ss.accept(); System.out.println("a client connected success"); connected=true; BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream(),"utf-8")); while(connected) { String str=br.readLine(); System.out.println(str); } br.close(); } } catch (IOException e) { e.printStackTrace(); } } }
0.5版客户端代码:(增加disconnect()方法,放入输出流和端口的关闭,优化输出流创建次数)
package com.swift; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedWriter; import java.io.DataOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.Socket; import java.net.UnknownHostException; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; import javax.swing.JTextField; public class ChatClientFrame extends JFrame { private static final long serialVersionUID = -118470059355655240L; Socket s=null; BufferedWriter bw=null; JLabel label_shang=new JLabel(); JLabel label_xia=new JLabel(); JTextField tf = new JTextField(38); JTextArea ta = new JTextArea(15, 50); JButton button=new JButton(); public ChatClientFrame() { setBounds(200, 200, 500, 400); setTitle("客户端聊天工具 —— 0.5"); //对窗口进行大的布局,分为三行一列,在pBasic面板上添加三个面板shang zhong xia JPanel pBasic=new JPanel(); pBasic.setLayout(new BorderLayout());//不设置默认也是这种布局模式 setContentPane(pBasic);//把面板放在窗口上,不记得用this.关键字 JPanel shang=new JPanel(); JPanel zhong=new JPanel(); JPanel xia=new JPanel(); //设置JPanel面板的大小 shang.setSize(470, 25); zhong.setSize(470, 180); xia.setSize(470, 40); pBasic.add(shang,BorderLayout.NORTH); pBasic.add(zhong,BorderLayout.CENTER); pBasic.add(xia,BorderLayout.SOUTH); shang.setBackground(Color.red); zhong.setBackground(Color.yellow); xia.setBackground(Color.blue); label_shang.setText("聊天记录"); shang.add(label_shang); ta.setLineWrap(true);// 自动换行 JScrollPane scroll=new JScrollPane(ta);// 增加滚动条,以便不增加行数 zhong.add(scroll); label_xia.setText("输入信息"); xia.add(label_xia,BorderLayout.WEST); /* * 增加功能,窗口监听事件,窗口打开时设置光标焦点在tf文本域中 */ this.addWindowListener(new WindowAdapter() { @Override public void windowOpened(WindowEvent e) { tf.requestFocus(); } }); xia.add(tf,BorderLayout.CENTER); button.setText("发送"); xia.add(button,BorderLayout.EAST); final class ShareListener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { String taText=ta.getText(); String tfText=tf.getText()+"\r\n"; ta.setText(taText+tfText); tf.setText(""); //当回车或发送按钮时,tfText发送到服务器 try { bw.write(tfText); bw.flush(); } catch (IOException e1) { e1.printStackTrace(); } } } button.addActionListener(new ShareListener()); tf.addActionListener(new ShareListener()); //通过压缩自动调整各个面板 pack(); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { //disconnect();//出错 System.exit(0); } }); setVisible(true); //创建窗体直接调用连接服务器 connect(); } public void connect() { try { s=new Socket("127.0.0.1",8888); System.out.println("connected!"); bw=new BufferedWriter(new OutputStreamWriter(s.getOutputStream(),"utf-8")); } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void disconnect() { try { s.close(); bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { new ChatClientFrame(); } }
Never waste time any more, Never old man be a yong man