高手,您好:
    我在写一个Socket通信的程序,之中,需要用到:“两个通信节点进行:"双方为了完成一笔通信业务,进行每个端点只拿出一个类型,进行两个类型之间的多次数据交互,完成这个业务”,的代码逻辑实现.
    现在,为了完成上述功能的代码实现,我进行了下面的代码实验:
    (S_port源代码:)
    package test;import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;public class S_port {
String str = "";
String IP = "";
int port = 0;
InetAddress IPAddress = null;
SocketClient soobj = null;
InetAddress Obj = null;
ServerSocket s =null;
Socket so = null;
S_port(){
SocketServer ss = new SocketServer(8888);
for(int i = 0;i<8;i++){
so = ss.nextSocket();
IPAddress = so.getInetAddress();
IP = GetMyUserIP.getMyIP(IPAddress);
try {
str = ss.read(so);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("S端测试目标:1.S端主线程8888端口接收C端的第1次通信数据2.主线程中的上述数据传入子线程进行打印3.S端8811端口接收到的C端写出的第二次通信报文");
System.out.println("C端发送的第"+1+"次通信数据"+str);
SystemThread cjco = new SystemThread(so,IP,str);  
cjco.start();
}
}

public static void main(String[] args){
new S_port();
}
}    (上述代码第18行的SocketServer类的源代码如下:)
    package test;
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"; public SocketServer(int port) {
try {
ss = new ServerSocket(port);
} catch (IOException e) {
e.printStackTrace();
}
} public Socket nextSocket() {
Socket s = null;
try {
s = ss.accept();
} catch (IOException e) {
e.printStackTrace();
}

return s;
} public String read(Socket s) throws IOException {
BufferedReader br = null;
if (null == (br = rm.get(s))) {
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
rm.put(s, br);
}
return br.readLine();
} public void write(Socket s, String content) throws IOException {
PrintWriter pw = null;
if (null == (pw = wm.get(s))) {
pw = new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
wm.put(s, pw);
}
pw.println(content);
pw.flush();
}

public void getMyResourceBack(){
if(rm!=null){
rm.clear();
}if(wm!=null){
wm.clear();
}if(ss!=null){
try {
ss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}    (S_port类的第22行的GetUserIP类的源代码如下:)
    package test;import java.net.InetAddress;
import java.net.UnknownHostException;public class GetMyUserIP {
public static String getMyIP(InetAddress ip){
try {
ip = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//得到本机的IP
String strip =ip.toString();
String[] strArr = strip.split("/");
return strArr[1];
}
}    (S_port类的第31行的代码的SystemThread类的源代码如下:)
    package test;import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;public class SystemThread extends Thread{
int LocalPort = 0;
SocketClient obj = null;
String ObjIP = null;
SocketServer so = null;
Socket Clientso = null;
DataInputStream dips = null;
InputStream inputstream = null;
String str0 = null;

public SystemThread(Socket s,String IP,String str){
Clientso = s;
so = new SocketServer(8811);
ObjIP = IP;
str0 = str;
System.out.println("==================="+ObjIP);
obj = new SocketClient(ObjIP,5588);
obj.writeStr("S端子线程中的数据写回");
}

public void run(){
String str = null;
try {
str = so.read(Clientso);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
obj.writeStr("S端子线程中的数据再次向C端在run方法中写回:本数据证明“C端的测试类的对象,可以用”");
System.out.println("主线程在子线程中的字符串的数据值:"+str0+"S端接收到的C端发送的第一次通信的数据值传入S端的子线程进行打印");
System.out.println("主线程中数据在子线程中接发打印结果:"+str+"+【本数据证明:C端第二次向S端的子线程中定义为端口号:8811的ServerSocket写出的数据,为成功的!!】");
obj.getMyResourceBack();
}
}    (上述SystemThread类的第10行代码的SocketClient类的源代码如下:)
    package test;import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;public class SocketClient { public Socket getS() {
return s;
} public void setS(Socket s) {
this.s = s;
} private Socket s;
private InputStream in;
private OutputStream out;
private BufferedInputStream inByte;
private OutputStream outByte;
private BufferedReader inStr;
private PrintWriter outStr;

private long size = 0; public SocketClient(String ip, int port) {
try {
s = new Socket(ip, port); in = s.getInputStream();
out = s.getOutputStream(); inByte = new BufferedInputStream(in);
outByte = out; inStr = new BufferedReader(new InputStreamReader(in));
outStr = new PrintWriter(new OutputStreamWriter(out));
} catch (Exception e) {
e.printStackTrace();
}
} public String readStr() throws IOException {
synchronized (this.in) {
return this.inStr.readLine();
}
} public void writeStr(String content) {
synchronized (this.out) {
outStr.println(content);
outStr.flush();
}
} public File readToFile(File file) throws IOException {
synchronized (this.in) {
FileOutputStream fos = new FileOutputStream(file);
byte[] temp = new byte[1024 * 8];
int count = 0;
while (-1 != (count = this.inByte.read(temp))) {
fos.write(temp, 0, count);
fos.flush();
}
fos.close();
return file;
}
} public void writeFile(File file) {
synchronized (this.out) {
size = file.length();
this.noticeFileSize(size); FileInputStream fis;
try {
fis = new FileInputStream(file);
byte[] temp = new byte[1024 * 8];
int count = 0;
while (-1 != (count = fis.read(temp))) {
this.outByte.write(temp, 0, count);
this.outByte.flush();
}
fis.close();
} catch (Exception e) {
e.printStackTrace();
}// long progress = 0;
// while (size != (progress = getServerReceiveSize())) {
// System.out.println("progress : " + (progress / (size / 100)) + "%");
// }
}
} private void noticeFileSize(long l) {
String str = l + "";
int j = 19 - str.length();
for (int i = 0; i < j; i++) {
str = "0" + str;
}
this.writeByByte(str);
} protected void writeByByte(String content) {
synchronized (this.out) {
try {
this.out.write(content.getBytes());
this.out.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
} private long getServerReceiveSize() {
synchronized (in) {
byte[] b = new byte[19];
try {
this.in.read(b);
} catch (IOException e) {
e.printStackTrace();
} return Long.parseLong(new String(b));
}
}

public String getProgress() {
long l = this.getServerReceiveSize() / (size / 100);
if(100 == l) {
return null;
}
return l + " %";
}

public void getMyResourceBack(){
if(s!=null){
try {
s.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(in!=null){
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(out!=null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(inStr!=null){
try {
inStr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}if(outStr!=null){
outStr.close();
}
}}    (C_port_Test类的源代码如下:)
    package test;import java.io.OutputStream;
import java.net.Socket;import s.C_port;
import s.SendDatagetAnswer;
import s.SocketClient;public class C_port_Test {
String str = "";
String str0 = "";
Socket s = null;
SocketClient so = null;
OutputStream outputstream = null; public C_port_Test(){
SendDatagetAnswer cs = new SendDatagetAnswer();
SocketClient so = new SocketClient("127.0.0.1",8888);
String str = "褚江!!必胜!!";
String str0 = cs.sendSDatagetSAnswer("C端第一次写出数据"+str,so.getS());
cs.getMyCSResourceBack();
SocketClient so1 = new SocketClient("127.0.0.1",8811);
System.out.println("C端测试打印数据:1.C端第一次写给S端的数据.2.S端第一次通过C端的测试类的对象接收到的写回给C端的数据.3.C端第二次写给S端的数据.");
System.out.println("1.C端第一次向S端写出的数据:"+str);
System.out.println("2.S端第一次写回数据:"+str0);
so1.writeStr("C端写出S端发回的数据:"+str0+"本数据证明:C端写回S端的数据8811端口,S端的本8811端口的子线程接收连接,可用");
System.out.println("3.C端写出S端发回的数据:"+str0+"本数据证明:C端写回S端的数据8811端口,S端的本8811端口的子线程接收连接,可用");
so1.getMyResourceBack();
}

public static void main(String[] args){
new C_port();
}

}

解决方案 »

  1.   

        (上文中的第18行的C_port_Test类的代码如下:)
        package s;import java.io.IOException;
    import java.net.Socket;public class SendDatagetAnswer {
    SocketClient sendDportData = new SocketClient("127.0.0.1",9999);
    SocketClient sendSportData = new SocketClient("127.0.0.1",8888);
    SocketServer getDataFromSport = new SocketServer(5588);
    SocketServer getDataFromDport = new SocketServer(5599);
    String getData = "";

    public String sendSDatagetSAnswer(String sendData,Socket s){
    sendSportData.writeStr(sendData);
    try {
    getData = getDataFromSport.read(s);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    sendSportData.getMyResourceBack();
    return getData;
    }

    public String sendDDatagetDAnswer(String sendData,Socket s){
    sendSportData.writeStr(sendData);
    try {
    getData = getDataFromDport.read(s);
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    return getData;
    }

    public void getMyCSResourceBack(){
    if(sendSportData!=null){
    sendSportData.getMyResourceBack();
    }if(getDataFromSport!=null){
    getDataFromSport.getMyResourceBack();
    }
    }

    public void getMyCDResourceBack(){
    if(sendDportData!=null){
    sendDportData.getMyResourceBack();
    }if(getDataFromDport!=null){
    getDataFromDport.getMyResourceBack();
    }
    }
    }    我的代码逻辑是“S_port类”首先运S_port类”,连接“S_port”类进行通信.
        运行后,打印出来的结果为:
        (S_port类console窗口的内容为:)
        
        点击本图中的“第10行报错”:“SocketClient.java:38”行的结果如下:
        
        (点击“S_port类console窗口”中的第11行报错的“SendDatagetAnswer.java:7”的内容为:)
        
        (C_port_Test类的console窗口中的内容如下:)
        
        求高手点拨:
        我的代码,为什么在“C_port_Test”端没有输出,并且,在“S_port”端报出了“空指针”的错误..
        希望高手能够帮助我:
        上面的“与预期不符”的程序运行打印输出,是什么原因.
        谢谢高手!!