Java Socket编程问题:客户端/服务器无法接受/读取输出/输入

时间:2014-03-16 09:58:09

标签: java sockets

nbp.java

public class nbp {
public static void main(String[] args) throws Exception {
    Socket clientSocket = new Socket("localhost",6789);
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
    BufferedInputStream in = new BufferedInputStream(clientSocket.getInputStream());

    out.write("A");
    out.write("\r\n");
    out.flush();
    System.out.println("NBP SENDS GW A");
    char c;
    StringBuilder sb = new StringBuilder();
    while(in.available()>0){
        c = (char) in.read();
        sb.append(c);
    }
    System.out.println("NBP RECEIVES FROM GW " + sb.toString());

}
}

gw.Java

public class gw {
public static void main(String[] args) throws Exception {
    ServerSocket listener = new ServerSocket(6789);
    Socket nbpSocket = listener.accept();
    Socket dspSocket = new Socket("localhost",6791);
    BufferedWriter nbpOut = new BufferedWriter(new OutputStreamWriter(nbpSocket.getOutputStream()));
    BufferedInputStream nbpIn = new BufferedInputStream(nbpSocket.getInputStream());
    BufferedWriter dspOut = new BufferedWriter(new OutputStreamWriter(dspSocket.getOutputStream()));
    BufferedInputStream dspIn = new BufferedInputStream(dspSocket.getInputStream());        

    char c;
    StringBuilder sb = new StringBuilder();
    while(nbpIn.available()>0){
        c = (char) nbpIn.read();
        sb.append(c);
    }
    System.out.println("GW RECEIVES FROM NBP " + sb.toString());

    dspOut.write("B");
    dspOut.write("\r\n");
    dspOut.flush();
    System.out.println("GW SENDS DSP B");

    sb = new StringBuilder();
    while(dspIn.available()>0){
        c = (char) dspIn.read();
        sb.append(c);
    }
    System.out.println("GW RECEIVES FROM DSP " + sb.toString());

    nbpOut.write(sb.toString());
    nbpOut.write("\r\n");
    nbpOut.flush();
    System.out.println("GW SENDS DSP " + sb.toString());
}
}

dsp.Java

public class dsp {
public static void main(String[] args) throws Exception {
    ServerSocket listener = new ServerSocket(6791);
    Socket clientSocket = listener.accept();
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
    BufferedInputStream in = new BufferedInputStream(clientSocket.getInputStream());

    char c;
    StringBuilder sb = new StringBuilder();
    while(in.available()>0){
        c = (char) in.read();
        sb.append(c);
    }
    System.out.println("DSP RECEIVES FROM GW " + sb.toString());

    out.write("C");
    out.write("\r\n");
    out.flush();
            System.out.println("DSP SENDS GW C");

}
}

这是上面的代码我试图了解Java中的Socket编程。所以,我想从NBP向GW发送消息。然后我希望GW向DSP发送消息并接受DSP的回复。然后,我想将GW收到的消息转发给NBP。

目前这并没有像我计划的那样发生。如果有任何时间和帮助,我将不胜感激。感谢

.P.S这是每个控制台的输出

nbp.console

NBP SENDS GW A
NBP RECEIVES FROM GW 

gw.console

GW RECEIVES FROM NBP A
GW SENDS DSP B
GW RECEIVES FROM DSP 
GW SENDS DSP 

dsp.console

DSP RECEIVES FROM GW 
DSP SENDS GW C

你可以看到..网关发送DSP'B'但DSP不能接收'B'。它也不能尝试向GW发送'C'但GW不接受'C'.. GW也不能向NBP发送'C'..任何想法为什么会这样?抱歉混淆。

1 个答案:

答案 0 :(得分:-1)

我想出了错误..应该有一个in.read();在while循环之前阻塞线程直到它收到第一个字符。否则它会在另一个线程准备好发送数据并跳过while循环之前命中in.available。