当前位置: 代码迷 >> J2SE >> 求好手相助,点拨一段代码的执行故障原因
  详细解决方案

求好手相助,点拨一段代码的执行故障原因

热度:140   发布时间:2016-04-23 19:59:08.0
求高手相助,点拨一段代码的执行故障原因。
高手,您好:
        小弟搞了一段程序,源代码如下:
       
SocketClient1st so = new SocketClient1st(DRecv.N_PORT_SERVER, 10008);
try {
so.writeStr(str0, "DRecv");
} catch (NullPointerException e) {
new NoteAdministratorServerInfo();
}
Socket sog = sso1.nextSocket();
try {
MyInfo = sso1.read(sog);
//System.out.println("MyInfo            " + MyInfo);
} catch (Exception e) {
// TODO Auto-generated catch block
}
 
        (顶层源代码)
       由于第3行向通信节点发出了报文。
       小弟在上述代码的第7行,做好了接受准备。可是,出现的结果如下:
       
        上图中的第620行代码是小弟贴出的代码中的第7行。
        小弟的上述所贴代码的第7行的sso1,是本类型的一个实例:
        
package d_port_package;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class SocketServer {
private ServerSocket ss = null;
private Map<Socket, BufferedReader> rm = new HashMap<Socket, BufferedReader>();
private Map<Socket, PrintWriter> wm = new HashMap<Socket, PrintWriter>();
String MyKey = "CJCO5888CJCO";
BufferedReader br = null;
PrintWriter pw = null;

public SocketServer(int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {

}
}

public Socket nextSocket() {
Socket s = null;
try {
s = ss.accept();
} catch (IOException e) {

}

return s;
}

public String read(Socket s) throws IOException {
if (null == (br = rm.get(s))) {
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
rm.put(s, br);
}
String str = br.readLine();
// str = Systemcrypt.Recovery(str,"CJCO");
return str;
}

public void write(Socket s, String content) throws IOException {
if (null == (pw = wm.get(s))) {
pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
wm.put(s, pw);
}
// content = Systemcrypt.HloveyRC4(content, "CJCO");
pw.println(content);
pw.flush();
}

public void getMyResourceBack() {
if (pw != null) {
pw.close();
}
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block

}
}
if (rm != null) {
rm.clear();
}
if (wm != null) {
wm.clear();
}
if (ss != null) {
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block

}
}
}
}

        小弟希望得到大哥的指导。
        希望能够让小弟,美梦成真!
------解决思路----------------------
目测是这个方法出错了:
public?Socket?nextSocket()?{
????????Socket?s?=?null;
????????try?{
????????????s?=?ss.accept();
????????}?catch?(IOException?e)?{
?????????????
????????}
?
????????return?s;
????}
你debug看看ss是不是为null或者关闭了。
  相关解决方案