Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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 如何将数据从TCP套接字获取到ByteBuffer中_Java_Sockets_Tcp_Bytebuffer_Socketchannel - Fatal编程技术网

Java 如何将数据从TCP套接字获取到ByteBuffer中

Java 如何将数据从TCP套接字获取到ByteBuffer中,java,sockets,tcp,bytebuffer,socketchannel,Java,Sockets,Tcp,Bytebuffer,Socketchannel,我需要将来自套接字的传入数据获取到ByteBuffer中,但我不知道如何操作。我是这个领域的新手,因此不确定最好的开始方式。我发现了以下内容,但这不是我想要的,因为它可以获取数据,但我需要将所有数据保存在bytebuffer中,以用于其他目的 ServerSocket welcomeSocket = new ServerSocket(Integer.parseInt(ibmPort)); while (true) { Socket connectionSocket = welcomeSo

我需要将来自套接字的传入数据获取到ByteBuffer中,但我不知道如何操作。我是这个领域的新手,因此不确定最好的开始方式。我发现了以下内容,但这不是我想要的,因为它可以获取数据,但我需要将所有数据保存在bytebuffer中,以用于其他目的

ServerSocket welcomeSocket = new ServerSocket(Integer.parseInt(ibmPort));
while (true) {
    Socket connectionSocket = welcomeSocket.accept();                   
    BufferedReader inFromClient =  new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
    DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
    clientSentence = inFromClient.readLine();
    System.out.println("Received: " + clientSentence);
    setRequestDataFromCT(clientSentence);
    capitalizedSentence = clientSentence.toUpperCase() + '\n';
    outToClient.writeBytes(capitalizedSentence);
}

此代码将读取所有字节并将其存储在ByteBuffer中,您可能需要调整bufferSize以存储所需的所有数据

int bufferSize = 8192;
ServerSocket welcomeSocket = new ServerSocket(Integer.parseInt(ibmPort));
while (true) {
    Socket connectionSocket = welcomeSocket.accept();
    ByteBuffer bf = ByteBuffer.allocate(bufferSize);
    BufferedInputStream inFromClient = new BufferedInputStream(connectionSocket.getInputStream());
    while (true) {
        int b = inFromClient.read();
        if (b == -1) {
            break;
        }
        bf.put( (byte) b);
    }
    connectionSocket.close();
}

int count=SocketChannel.read(ByteBuffer)。
不确定如果您没有使用
SocketChannels,为什么要添加“SocketChannel”标记,但这是如何做到的。

您应该添加您正在使用的语言。对我来说,它看起来像Java,但我不知道。你发布的代码使用了Reader接口,它处理人类可读的字符串而不是字节。这就是你想要的吗?我希望它将传入的字符串作为一个字节处理,因为有一些字符不能在字符串中转换。非常感谢。我也在一分钟前找到了类似的方法。我也在这里发布了它,因为它可能很有用。小心,不能保证read方法会一次读取所有数据,它可以返回要接收的部分数据。(-1,如果允许的话)您不应该在while循环中调用allocate()
        ServerSocket welcomeSocket = new ServerSocket(Integer.parseInt(ibmPort));
        while (true) {
            Socket connectionSocket = welcomeSocket.accept();
            InputStream stream = connectionSocket.getInputStream();
            byte[] data = new byte[1024];
            int count = stream.read(data);
            ByteBuffer bb = ByteBuffer.allocate(data.length);
            bb.put(data);
            bb.flip();
        }