Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从套接字inputstream读取时,第二个客户端被阻止_Java_Multithreading_Sockets - Fatal编程技术网

Java 从套接字inputstream读取时,第二个客户端被阻止

Java 从套接字inputstream读取时,第二个客户端被阻止,java,multithreading,sockets,Java,Multithreading,Sockets,我正在向客户端发送一个nonce,但是,第一个客户端工作正常。当第二个客户端正在运行时,在服务器发送加密的nonce后,它将被阻止 ReadByte方法: private static void readByte(byte[] byteArray, InputStream byteIn, Socket socket) throws Exception{ int offset = 0; int numRead = 0; while (offset < byteArray.l

我正在向客户端发送一个nonce,但是,第一个客户端工作正常。当第二个客户端正在运行时,在服务器发送加密的nonce后,它将被阻止

ReadByte方法:

private static void readByte(byte[] byteArray, InputStream byteIn, Socket socket) throws Exception{
   int offset = 0;
   int numRead = 0;
   while (offset < byteArray.length && (numRead = byteIn.read(byteArray, offset, byteArray.length - offset)) >= 0){
        offset += numRead;
    }
    if (offset < byteArray.length) {
        System.out.println("File reception incomplete!");
    }
}
客户:

public class Client2 {
private static final String server = "localhost";
private static final int port = 1111;
private static final String filePath = "medianFile.txt";
private static final String fileName = "medianFile.txt";
private static final String CAcert = "CA.crt";

public static void main(String[] args) {
    try{
        Socket socket = new Socket(server,port);
        OutputStream byteOut = socket.getOutputStream();
        InputStream byteIn = socket.getInputStream();

        PrintWriter stringOut = new PrintWriter(socket.getOutputStream(),true);
        BufferedReader stringIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        stringOut.println("Please prove ur identity!");
        stringOut.flush();
        System.out.println("Sent to server: Please prove ur identity!");

        String r1 = stringIn.readLine();
        System.out.println(r1);

//          send nonce
        byte[] nonce = generateNonce();
        if(r1.contains("I am server")){
            stringOut.println(Integer.toString(nonce.length));
            byteOut.write(nonce);
            byteOut.flush();
            System.out.println("Sent to server: nonce sent!");
        }

//          get encrypted nonce from server
        String encryptedNonceLen = stringIn.readLine();
        byte[] encryptedNonceBytes = new byte[Integer.parseInt(encryptedNonceLen)];
        readByte(encryptedNonceBytes,byteIn,socket);
    }
}

您的服务器是多线程的吗?它是否在循环中接受()?如果您的方法在
byteIn.read()
上阻塞,这是因为它在读取累计的
byteArray.length
字节总数之前耗尽了可用字节,但对等套接字未关闭。这可能有多种原因。如果有更多字节从对等方到达,它将解除阻塞;它显然没有恢复消除了阻塞的几个可能原因。@EJP,我使用executorservice为服务器端处理不同的线程,是的,
accept()
处于while循环中。您在
read()
中阻塞,这意味着服务器已停止发送,或者可能从未启动。需要更多信息,甚至可能是服务器代码。不,“对等方”是连接另一端的人。
public class Client2 {
private static final String server = "localhost";
private static final int port = 1111;
private static final String filePath = "medianFile.txt";
private static final String fileName = "medianFile.txt";
private static final String CAcert = "CA.crt";

public static void main(String[] args) {
    try{
        Socket socket = new Socket(server,port);
        OutputStream byteOut = socket.getOutputStream();
        InputStream byteIn = socket.getInputStream();

        PrintWriter stringOut = new PrintWriter(socket.getOutputStream(),true);
        BufferedReader stringIn = new BufferedReader(new InputStreamReader(socket.getInputStream()));

        stringOut.println("Please prove ur identity!");
        stringOut.flush();
        System.out.println("Sent to server: Please prove ur identity!");

        String r1 = stringIn.readLine();
        System.out.println(r1);

//          send nonce
        byte[] nonce = generateNonce();
        if(r1.contains("I am server")){
            stringOut.println(Integer.toString(nonce.length));
            byteOut.write(nonce);
            byteOut.flush();
            System.out.println("Sent to server: nonce sent!");
        }

//          get encrypted nonce from server
        String encryptedNonceLen = stringIn.readLine();
        byte[] encryptedNonceBytes = new byte[Integer.parseInt(encryptedNonceLen)];
        readByte(encryptedNonceBytes,byteIn,socket);
    }
}