服务器无法读取Java缓冲区

服务器无法读取Java缓冲区,java,tcp,server,Java,Tcp,Server,我的多连接服务器TCP有问题。它接受到客户端的连接,但当它必须读取InputBuffer中的数据(字节数组[])时,它就卡住了 代码如下: 服务器: public class thread_acc extends Thread{ private final ServerSocket sock_acc; public thread_acc() throws IOException { this.sock_acc = new ServerSocket(10000); } @Overri

我的多连接服务器TCP有问题。它接受到客户端的连接,但当它必须读取
InputBuffer
中的数据(字节数组[])时,它就卡住了

代码如下:

服务器:

public class thread_acc extends Thread{

private final ServerSocket sock_acc;  
public thread_acc() throws IOException {
    this.sock_acc = new ServerSocket(10000);
}
@Override
public void run(){
    for(;;){
        try {
            Socket sock_client = sock_acc.accept();
            System.out.println("connection accepts");
            new Thread(new thread_proc(sock_client)).start();
        } catch (IOException ex) {               Logger.getLogger(thread_acc.class.getName()).log(Level.SEVERE, null, ex);
        }      
    }
  }
}

public class thread_proc implements Runnable{
private Socket sock_client;
public thread_proc(Socket sock){
    this.sock_client = sock;
}
@Override
public void run(){
    try {
        procRequest();
    } catch (IOException ex) {
        Logger.getLogger(thread_proc.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(thread_proc.class.getName()).log(Level.SEVERE, null, ex);
    }
} 
void procRequest() throws IOException, ClassNotFoundException{
byte[] pack1 = socket.readSocket(sock_client);
System.out.println("read pack 1");
byte[] pack2 = socket.readSocket(sock_client);
System.out.println("read pack 2");
.....
}
public class thread_key implements Runnable {
private static InetAddress IpServer = InetAddress.getByName("127.0.0.1");
private static int PortServer = 10000;
private Socket sock_send;
private int id;
private String namefile;

public thread_key(int id, String namefile) throws IOException {
    this.sock_send = new Socket(IpServer, PortServer);
    this.id = id;
    this.namefile = namefile;
}
@Override
public void run(){
    Request();
}

private String[] Request(int iddoc) throws IOException, ClassNotFoundException{
    ...generations Keys RSA and payloads....

    byte[] pack1 = RSA.encryptPub(payload1, pub);
    byte[] pack2 = RSA.encryptPub(payload2, pub);
    socket.writeSocket(sock_send, pack1);
    socket.writeSocket(sock_send, pack2);
    System.out.println("write ok");

    }
}
public class socket {

public static void writeSocket(Socket sock, byte[] pacchetto) throws IOException{
    ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
    out.writeObject(pacchetto);
    out.flush();
}

public static byte[] readSocket(Socket sock) throws IOException, ClassNotFoundException{
    ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
    byte[] pacchetto = (byte[]) in.readObject();
    return pacchetto;
  }
}
客户端:

public class thread_acc extends Thread{

private final ServerSocket sock_acc;  
public thread_acc() throws IOException {
    this.sock_acc = new ServerSocket(10000);
}
@Override
public void run(){
    for(;;){
        try {
            Socket sock_client = sock_acc.accept();
            System.out.println("connection accepts");
            new Thread(new thread_proc(sock_client)).start();
        } catch (IOException ex) {               Logger.getLogger(thread_acc.class.getName()).log(Level.SEVERE, null, ex);
        }      
    }
  }
}

public class thread_proc implements Runnable{
private Socket sock_client;
public thread_proc(Socket sock){
    this.sock_client = sock;
}
@Override
public void run(){
    try {
        procRequest();
    } catch (IOException ex) {
        Logger.getLogger(thread_proc.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(thread_proc.class.getName()).log(Level.SEVERE, null, ex);
    }
} 
void procRequest() throws IOException, ClassNotFoundException{
byte[] pack1 = socket.readSocket(sock_client);
System.out.println("read pack 1");
byte[] pack2 = socket.readSocket(sock_client);
System.out.println("read pack 2");
.....
}
public class thread_key implements Runnable {
private static InetAddress IpServer = InetAddress.getByName("127.0.0.1");
private static int PortServer = 10000;
private Socket sock_send;
private int id;
private String namefile;

public thread_key(int id, String namefile) throws IOException {
    this.sock_send = new Socket(IpServer, PortServer);
    this.id = id;
    this.namefile = namefile;
}
@Override
public void run(){
    Request();
}

private String[] Request(int iddoc) throws IOException, ClassNotFoundException{
    ...generations Keys RSA and payloads....

    byte[] pack1 = RSA.encryptPub(payload1, pub);
    byte[] pack2 = RSA.encryptPub(payload2, pub);
    socket.writeSocket(sock_send, pack1);
    socket.writeSocket(sock_send, pack2);
    System.out.println("write ok");

    }
}
public class socket {

public static void writeSocket(Socket sock, byte[] pacchetto) throws IOException{
    ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
    out.writeObject(pacchetto);
    out.flush();
}

public static byte[] readSocket(Socket sock) throws IOException, ClassNotFoundException{
    ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
    byte[] pacchetto = (byte[]) in.readObject();
    return pacchetto;
  }
}
读写:

public class thread_acc extends Thread{

private final ServerSocket sock_acc;  
public thread_acc() throws IOException {
    this.sock_acc = new ServerSocket(10000);
}
@Override
public void run(){
    for(;;){
        try {
            Socket sock_client = sock_acc.accept();
            System.out.println("connection accepts");
            new Thread(new thread_proc(sock_client)).start();
        } catch (IOException ex) {               Logger.getLogger(thread_acc.class.getName()).log(Level.SEVERE, null, ex);
        }      
    }
  }
}

public class thread_proc implements Runnable{
private Socket sock_client;
public thread_proc(Socket sock){
    this.sock_client = sock;
}
@Override
public void run(){
    try {
        procRequest();
    } catch (IOException ex) {
        Logger.getLogger(thread_proc.class.getName()).log(Level.SEVERE, null, ex);
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(thread_proc.class.getName()).log(Level.SEVERE, null, ex);
    }
} 
void procRequest() throws IOException, ClassNotFoundException{
byte[] pack1 = socket.readSocket(sock_client);
System.out.println("read pack 1");
byte[] pack2 = socket.readSocket(sock_client);
System.out.println("read pack 2");
.....
}
public class thread_key implements Runnable {
private static InetAddress IpServer = InetAddress.getByName("127.0.0.1");
private static int PortServer = 10000;
private Socket sock_send;
private int id;
private String namefile;

public thread_key(int id, String namefile) throws IOException {
    this.sock_send = new Socket(IpServer, PortServer);
    this.id = id;
    this.namefile = namefile;
}
@Override
public void run(){
    Request();
}

private String[] Request(int iddoc) throws IOException, ClassNotFoundException{
    ...generations Keys RSA and payloads....

    byte[] pack1 = RSA.encryptPub(payload1, pub);
    byte[] pack2 = RSA.encryptPub(payload2, pub);
    socket.writeSocket(sock_send, pack1);
    socket.writeSocket(sock_send, pack2);
    System.out.println("write ok");

    }
}
public class socket {

public static void writeSocket(Socket sock, byte[] pacchetto) throws IOException{
    ObjectOutputStream out = new ObjectOutputStream(sock.getOutputStream());
    out.writeObject(pacchetto);
    out.flush();
}

public static byte[] readSocket(Socket sock) throws IOException, ClassNotFoundException{
    ObjectInputStream in = new ObjectInputStream(sock.getInputStream());
    byte[] pacchetto = (byte[]) in.readObject();
    return pacchetto;
  }
}

不要每次发送或接收内容时都创建新的对象流。在插座的两端使用相同的流。对象流具有在构造时读取或写入的头,因此如果接收方的构造与发送方的构造不匹配,则会出现错误。

如果需要代码帮助,请使其可读。遵循格式和命名约定-我使用。类的命名应该像
这个例子
-变量应该像
这个例子
。这个解决方案是我第一次采用,但它产生了同样的问题。我在客户端通过睡眠解决,因为当客户端写入缓冲区服务器时,创建带有缓冲区的线程,网络代码中的休眠实际上是浪费时间。您需要在
ObjectInputStream之前创建
ObjectOutputStream