日期:2014-05-20  浏览次数:20898 次

java关闭socket接收线程的问题
Java code
package com.njust.socket;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ServerUDPReceiver {
    // 侦听的UDP端口号
    private int port = -1;    
    public static DatagramSocket socket_for_receive_udp = null;
    private byte[] buffer = new byte[8192];
    private DatagramPacket incomming = null;
    private boolean isFlag = true;
    //private Thread serverThread = null;
    
    public ServerUDPReceiver(int i_port) {
        port = i_port;
    }

    public void startUDPReceiver() {
        try {
            socket_for_receive_udp = new DatagramSocket(this.port);
            Thread serverThread = new Thread(new Runnable() {
                public void run() {
                    try {
                        while (isFlag) {
                            try {
                                // 构造一个接收数据包
                                incomming = new DatagramPacket(buffer,buffer.length);
                                // 接收数据报
                                System.out.println("before receive!");
                                socket_for_receive_udp.receive(incomming);

                                int length, offset;
                                byte[] realData = null;
                                length = incomming.getLength();
                                offset = incomming.getOffset();
                                realData = new byte[length];
                                System.arraycopy(buffer, offset, realData,offset, length);
                                System.out.println("reveive data:" + new String(realData));

                            } catch (Exception ee) {
                                ee.printStackTrace();
                                System.out.println("In while!!!");
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        socket_for_receive_udp.close();
                    }
                }
            });
            serverThread.start();
        } catch (Exception e) {
            e.printStackTrace();
            if (e.toString().indexOf(
                    "java.net.BindException: Address already in use: JVM_Bind") != -1) {
                System.out.println("Server 已经启动,不能重复启动!");
            }
        }
    }
    
    public void stopUDPReceiver(){
        if(socket_for_receive_udp!=null){
            //serverThread.stop();
            this.isFlag = false;
            System.out.println("before socket close!");
            socket_for_receive_udp.close();
        }
    }
}


问题是这样的:首先启动startUDPReceiver,然后关闭stopUDPReceiver,提示错误如下:
java.net.SocketException: socket closed
at java.net.PlainDatagramSocketImpl.receive0(Native Method)
at java.net.PlainDatagramSocketImpl.receive(Unknown Source)
at java.net.DatagramSocket.receive(Unknown Source)
at com.njust.socket.ServerUDPReceiver$1.run(ServerUDPReceiver.java:29)
at java.lang.Thread.run(Unknown Source)

------解决方案--------------------
估计是它的事,
public void stopUDPReceiver(){
if(socket_for_receive_udp!=null){
//serverThread.stop();
this.isFlag = false;
System.out.println("before socket close!");
socket_for_receive_udp.close();
}
}

这个地方调用close但是你线程里面还在执行 连接被提前关闭,应该把这个close写到 while (isFlag) {...}后面,stopUDPReceiver()里置下this.isFlag = false;就行了。