import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ByteServer {
    public static void main(String[] args) {
        try {
            ServerSocket ss = new ServerSocket(2345);
            System.out.println("Opened Serversocket and waiting");

            Socket s = ss.accept();//establishes connection

            DataInputStream dIn = new DataInputStream(s.getInputStream());

            int length = dIn.readInt();         // read the length of the incoming message
            if (length > 0) {
                byte[] message = new byte[length];
                dIn.readFully(message, 0, message.length); // read the bytes of the message

                System.out.print("Received an array of " + length + " bytes: ");
                for (int i = 0; i < length; i++)
                    System.out.print(Integer.toHexString(message[i] & 0xFF) + " ");
                System.out.println();
            }
            s.close();
            ss.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

