0
我有一個服務器,等待傳入的客戶端消息並使用UDP。當我試圖關閉UDP連接,我得到一個IOException ..如何在不拋出異常的情況下關閉UDP服務器?
public void run(){
String user_message = null;
try {
connection = startServer();
System.out.println("Server started");
while ((true) && (serverStarted)){
try {
user_message = receiveMessage();
check_query(user_message , ",");
} catch (IOException ex) {
System.out.println("Error1 "+ex.getMessage());
setError("Error establishing connection ") ;
Txt_Log.setText(Txt_Log.getText() + "\n Error establishing connection0") ;
break;
}
}
System.out.println("Server is stopped.....");
}
catch (SocketException ex) {
System.out.println("Error2 "+ex.getMessage());
setError("Error establishing connection ") ;
Txt_Log.setText(Txt_Log.getText() + "\n Error establishing connection1") ;
}
catch(NullPointerException ex){
System.out.println("Error3 "+ex.getMessage());
ex.printStackTrace();
setError("Error Null Pointer ") ;
Txt_Log.setText(Txt_Log.getText() + "\n Error :"+ex.getMessage()) ;
}
}
/*
* Method starts server datagram to bind to port
*/
public DatagramSocket startServer() throws SocketException{
return new DatagramSocket(10000);
}
/*
* Method stops server so that server is not able to receive client requests
*/
public void closeServer(){
if (connection != null){
//connection.disconnect();
connection.close();
//connection = null;
}
}
/*
* Method sends message via datagram to client
*/
private void sendMessage(String message) throws IOException{
byte[] message_byte = message.getBytes();
packet_send = new DatagramPacket(message_byte , message_byte.length , packet_receive.getAddress() , packet_receive.getPort());
connection.send(packet_send);
}
/*
* Method receives messages from clients
*/
private String receiveMessage() throws IOException,NullPointerException{
if (connection!=null){
byte[] buf = new byte[256];
packet_receive = new DatagramPacket(buf , buf.length) ;
connection.receive(packet_receive) ;
return getString(packet_receive);
}
throw new NullPointerException("Connection cannot be null");
}
/*
* Method retrieves data from packet and converts to string
*/
private String getString(DatagramPacket packet){
String string = new String (packet.getData() , 0 , packet.getLength()).trim();
return string;
}
private void drop_DBconnection(){
if (DB_connection!=null){
try{
DB_connection.close();
//DB_connection = null;
}
catch(SQLException e){
Txt_Log.setText("Server connection Error...");
setError("Cannot close database connection");
}
}
}
public void closeServer(){
if (connection != null){
//connection.disconnect();
connection.close();
//connection = null;
}
}
私人無效btn_stopActionPerformed(EVT java.awt.event.ActionEvent中){
if (serverStarted){
Txt_Log.setText(Txt_Log.getText()+"\nServer has been stopped ....") ;
serverStarted = false ;
drop_DBconnection() ;
closeServer();
btn_start.setEnabled(true) ;
btn_stop.setEnabled(false);
}
}
我的輸出和堆棧跟蹤
Server started
Server is started..... Error1 socket
closed Server is stopped.....
java.net.SocketException: socket closed
at java.net.PlainDatagramSocketImpl.receive0(NativeMethod)
at java.net.PlainDatagramSocketImpl.receive(PlainDatagramSocketImpl.java:136)
at java.net.DatagramSocket.receive(DatagramSocket.java:712)
at server.Server.receiveMessage(Server.java:608)
at server.Server.run(Server.java:528)
at java.lang.Thread.run(Thread.java:619)
如何關閉服務器以避免引發異常?
我想你在這裏缺少一些代碼位,比如serverStarted,以及任何調用closeServer()。連接的作用域在run()之外,所以任何數量的函數都可以調用它並對其進行操作。 – 2011-02-09 19:08:48