日期:2014-05-17  浏览次数:20764 次

ServerSocket关闭异常
我在一个线程里面启动了一个serversocket,代码如下
Java code
package com.boryou.module;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JOptionPane;

/**
 * 窗口服务器,用于在窗口启动时,启动后台服务,接受窗口中,地图组件的本地窗口调用请求
 * 
 * 
 * */
public class FrameServer implements Runnable{


    private ServerSocket ss = null;
    private Socket sock = null;
    private BufferedReader in = null;
    private boolean flg = true;
    
    public FrameServer() {
        try {
            ss = new ServerSocket(80);
            flg = true;
        } catch (IOException e) {
            System.out.println("服务套接字启动失败!");
            e.printStackTrace();
        }
        
    }
    public void run() {
        while (flg) {
            try {
                sock = ss.accept();
                in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                String line = in.readLine();
//                System.out.println("you input is :" + line);
//                MainFrame mf1 = new MainFrame();
                
                //从读取的参数,调用响应的窗口
                JOptionPane.showMessageDialog(null, "", "服务器套接字", JOptionPane.ERROR_MESSAGE);
                
                in.close();
                sock.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }
    
    public void  stopServer() {
        flg = false;
        if(ss.isBound() && !ss.isClosed())
        {
            try {
                ss.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

在外部调用,启动这个线程,然后在调用stopServer方法的时候,总是失败,异常如下:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.boryou.module.MainFrame.processEvent(MainFrame.java:50)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

搞不懂怎么回事,不知道哪里出问题了.


------解决方案-----