當我的客戶端關閉之后,服務端就會報錯,有沒有大佬能幫我解答下這個問題
package com.java;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
/**
* 服務端
* 功能:接收客戶端發送的訊息,然后發送給所有在線的客戶端
*/
public class Server {
public static List<Socket> sockets=new ArrayList<Socket>();
public static void main(String[] args) throws IOException {
System.out.println("服務器啟動");
ServerSocket server=new ServerSocket(2000);
while (true){
System.out.println("等待客戶端連接");
Socket s=server.accept();
sockets.add(s);
System.out.println("客戶端已連接,目前在線人數"+sockets.size());
new ServerThread(s).start();
}
}
}
class ServerThread extends Thread{
private Socket s;
public ServerThread(Socket s){
this.s=s;
}
@Override
public void run() {
try {
BufferedReader br=
new BufferedReader(
new InputStreamReader(s.getInputStream()));
//獲得登錄用戶的用戶名,然后給所有客戶端發送歡迎xxx登錄
String username= br.readLine();
for (Socket socket:Server.sockets){
OutputStreamWriter osw=
new OutputStreamWriter(socket.getOutputStream());
osw.write("[系統訊息]"+username+"登錄\n");
osw.flush();
}
//接收聊天資訊,然后轉發給所有的客戶端
while (true){
String content= br.readLine();
for (Socket socket:Server.sockets){
OutputStreamWriter osw=
new OutputStreamWriter(socket.getOutputStream());
osw.write(username+":"+content+"\n");
osw.flush();
}
System.out.println("日志:"+username+":"+content);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.java;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
/**
* 與服務器進行互動
* 接收服務器發送的訊息
* 給服務器發送訊息
*/
public class Client extends JFrame {
public static void main(String[] args) {
new Login();
}
private Socket s;
private String username;
JPanel jp1,jp2;
JTextArea jta;//多行文本,用來顯示內容
JTextField jtf;//單行文本,用來輸入內容
JScrollPane jsp;//滾動條
public Client(Socket s,String username){
this.s=s;
this.username=username;
jp1=new JPanel();
jta=new JTextArea(20,30);//行 列
jsp=new JScrollPane(jta);//使jta具有了滾動條功能
jp1.add(jsp);
//用來放置輸入內容jtf的面板
jp2=new JPanel();
jtf=new JTextField(30);
jp2.add(jtf);
this.setTitle("當前用戶"+username);
this.setLayout(new BorderLayout());
this.add(BorderLayout.CENTER,jp1);
this.add(BorderLayout.SOUTH,jp2);
this.pack();
this.setVisible(true);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
//dispose();
//s.isClosed();
System.exit(0);
}
});
jtf.addActionListener(new MyListener());
//啟動接收服務器訊息的執行緒
new GetThread().start();
}
class MyListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
/*jta.setText(jta.getText()+"\n"+jtf.getText());
jtf.setText("");*/
//向服務器發送訊息
String content=jtf.getText();
try {
OutputStreamWriter osw=
new OutputStreamWriter(s.getOutputStream());
osw.write(content+"\n");
osw.flush();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
class GetThread extends Thread{
public void run(){
try {
BufferedReader br=
new BufferedReader(
new InputStreamReader(s.getInputStream()));
while (true){
String content=br.readLine();
jta.setText(jta.getText()+"\n"+content);
jtf.setText("");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.java;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
/**
* 登錄視窗
* 記錄用戶名
*/
public class Login extends JFrame implements ActionListener {
JLabel label=new JLabel("請輸入用戶名");
JTextField jtf=new JTextField(20);
public Login(){
this.setLayout(new FlowLayout());
this.add(label);
this.add(jtf);
this.pack();
this.setVisible(true);
jtf.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
Socket s= null;
try {
s = new Socket("127.0.0.1",2000);
} catch (IOException ioException) {
ioException.printStackTrace();
}
try {
OutputStreamWriter osw=
new OutputStreamWriter(s.getOutputStream());
osw.write(jtf.getText()+"\n");
osw.flush();
} catch (IOException ioException) {
ioException.printStackTrace();
}
new Client(s,jtf.getText());
this.dispose();
}
}
uj5u.com熱心網友回復:
報啥錯啊,客戶端斷開,服務端應該沒問題啊uj5u.com熱心網友回復:
因為你讀寫前沒判斷socket的狀態,socket斷了再去讀寫肯定會報錯啊。還有服務端的讀操作有問題,你這是所有客戶端佇列化發訊息了。如果其中一個沒發訊息,后邊的都不會處理,也就是別的客戶端發了東西,沒反應。
uj5u.com熱心網友回復:
服務端報連接重置uj5u.com熱心網友回復:
用什么方法判斷socket狀態呢uj5u.com熱心網友回復:
連接重置轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/225100.html
標籤:Java SE
