Q:

Sending multiple object via sockets

I have the following pieces of code and don't fully understand the way they behave.
Client
public class Client {

    public static void main(String[] args) throws InterruptedException {
        try {
            final Socket socket = new Socket("localhost", 3011);
            final OutputStream socketStream = socket.getOutputStream();
            for (int i = 0; i < 10; i++) {
                final byte[] message = new byte[1 * 1000 * 1000];
                socketStream.write(message);
                socketStream.flush();
                System.out.println("sent message");
            }
            socketStream.close();
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

Server:
public class Server {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        final int portNumber = 3011;
        try {
            ServerSocket serverSocket = new ServerSocket(portNumber);
            System.out.println("server running on " + portNumber);
            while (true) {
                final Socket clientSocket = serverSocket.accept();
                final InputStream inputStream = clientSocket.getInputStream();
                final byte[] in = new byte[1024];
                long start = System.currentTimeMillis();
                int totalBytesRead = 0;
                int bytesRead;
                while((bytesRead = inputStream.read(in)) >= 0) {
                    totalBytesRead += bytesRead;
                }
                long duration = System.currentTimeMillis() - start;
                System.out.println("got " + totalBytesRead + " bytes from socket");
                System.out.println("took " + duration + "ms");
                final double transferRatePerSecond = totalBytesRead / (duration / 1000f);
                System.out.println("average transfer was " + transferRatePerSecond + " bytes/second");
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

Now what I'm hoping to see is 10 individual messages received on the server side.  What I actually get is one message that seems to be the total of all 10 messages sent by the client.  For example:
got socket Socket[addr=/5.67.133.157,port=53432,localport=3011]
got 10000000 bytes from socket
took 69332ms
average transfer was 144233.546875 bytes/second

A:

The way you are using sockets, there isn't a built-in break signal between each message you send... It all comes in on the same InputStream and gets read byte by byte. If you want to be able to differentiate between your writes, you either need to use a DataInput/OutputStream or use a transfer/object protocol (like XML or JSON) that marks this in the message.
By the way, if you've got interest in Sockets beyond this test, you might want to look at some of the utilities I wrote way back for handling Sockets / InputStreams. Specifically, check out ServerSocketEx and DataFetcher

