How to store bytes in an array that grows automatically if the byte size changes in java?

129 views Asked by At

I'm trying to create a very simple client-server program. My aim is to send a message to the server after connecting succesfully but I need to be able to store the message using byteArrayOutputStream and also byteArrayInputStream later just in case something goes wrong along the way. This is my code so far:

public class TCPClient
{

    public byte[] askServer(String hostname, int port, byte[] toServerBytes) throws IOException
    {
            Socket clientSocket = new Socket(hostname, port);
            
            String Message;
            StringBuilder sb = new StringBuilder();

            try {

                clientSocket.getOutputStream().write(toServerBytes(StandardCharsets.UTF_8));
                ByteArrayOutputStream fromClient = new ByteArrayOutputStream();
                byte[] b = fromClient.toByteArray();

            }
0

There are 0 answers