從我的表單關閉我的TCP服務器時出現此錯誤。當停止我的TCP服務器時拋出SocketException java
java.net.SocketException: Socket closed
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:404)
at java.net.ServerSocket.implAccept(ServerSocket.java:545)
at java.net.ServerSocket.accept(ServerSocket.java:513)
at com.hightekjonathan.HomeServer.Server.Start(Server.java:36)
at com.hightekjonathan.HomeServer.Form$2.run(Form.java:81)
at java.lang.Thread.run(Thread.java:745)
繼承人的Server.java:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.Enumeration;
public class Server {
private static ServerSocket serverSocket = null;
private static Socket socket = null;
private static DataInputStream dataInputStream = null;
private static DataOutputStream dataOutputStream = null;
private static int port = 19586;
private static boolean running = false;
public void Start() {
try {
System.out.println("Starting Server...");
serverSocket = new ServerSocket(port);
System.out.println("Server Started");
System.out.println("IP Address: " + getIpAddress());
System.out.println("Listening: " + serverSocket.getLocalPort());
running = true;
} catch (IOException e) {
e.printStackTrace();
}
try {
System.out.println("Attempting to connect to clients...");
socket = serverSocket.accept();
dataInputStream = new DataInputStream(socket.getInputStream());
dataOutputStream = new DataOutputStream(socket.getOutputStream());
System.out.println("ip: " + socket.getInetAddress());
System.out.println("message: " + dataInputStream.readUTF());
dataOutputStream.writeUTF("connected");
} catch (Exception e) {
e.printStackTrace();
}
}
public void Stop() {
if (running) {
if (socket != null) {
try {
socket.close();
socket = null;
} catch (Exception e) {
e.printStackTrace();
}
}
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (dataOutputStream != null) {
try {
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
running = false;
}
}
private String getIpAddress() {
String ip = "";
try {
Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();
while (enumNetworkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();
Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();
while (enumInetAddress.hasMoreElements()) {
InetAddress inetAddress = enumInetAddress.nextElement();
if (inetAddress.isSiteLocalAddress()) {
ip += inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
ip += "Something Wrong! " + e.toString() + "\n";
}
return ip;
}
public boolean isRunning() {
return running;
}
}
其使用此功能並開始一個新的線程從我的GUI形式:
button.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
buttonActionPerformed(evt);
}
);
然後buttonActionPerformed()
是這樣的:
private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
if (!server.isRunning()) {
Runnable r = new Runnable() {
public void run() {
server.Start();
}
};
new Thread(r).start();
button.setText("Stop Server");
serverStatus.setText("Server: Started");
} else {
server.Stop();
button.setText("Start Server");
serverStatus.setText("Server: Stopped");
}
}
e當沒有客戶端連接時,我停止服務器時纔會出現rror。但是,當一個客戶端連接,然後斷開連接,我沒有得到那個錯誤。它正確地關閉它,我只是想確保錯誤不會是一個嚴重的問題,或者如果有一個簡單的方法來修復它。
是的 - 只是轉儲異常。 – 2014-12-13 14:02:51
所以它應該是安全的忽略? – hightekjonathan 2014-12-13 16:33:42
@pomkine所以把布爾設置爲這個'private static volatile boolean running = false;'?或者在檢索時將其設置爲volatile? – hightekjonathan 2014-12-13 16:35:37