2

I need to get incoming data from a socket into a ByteBuffer and I do not know how to do it. I am new to this field and therefore not sure of the best way to start. I found the following but that is not what I want as it gets the data in line but I need to have all of my data in bytebuffer for other purposes.

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);
}
user207421
  • 305,947
  • 44
  • 307
  • 483
user1372020
  • 91
  • 2
  • 2
  • 12
  • You should add which language you are using. To me it looks like Java but well I dont know. – clentfort Sep 18 '12 at 14:19
  • The code you posted uses Reader interface, which handles human readable strings rather than bytes. Is that what you want? – pmoleri Sep 18 '12 at 15:48
  • I want it to handle the incoming string as a byte since there are characters there that can't be cast in string. – user1372020 Sep 18 '12 at 16:18

3 Answers3

4

This code will read all the bytes and store them in a ByteBuffer, you may have to adjust the bufferSize to store all the data you need.

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();
}
pmoleri
  • 4,238
  • 1
  • 15
  • 25
2

int count = SocketChannel.read(ByteBuffer). Not sure why you added the 'socketchannel' tag if you weren't using SocketChannels, but this is how to do it.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1
        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();
        }
user1372020
  • 91
  • 2
  • 2
  • 12
  • 2
    Careful, there's no guarantee that the read method will read all the data at once, it can return with a portion of the data to be received. – pmoleri Sep 18 '12 at 19:29
  • (-1 if I were allowed) You should not be calling allocate() inside the while loop – Alastair Nov 15 '16 at 21:12