Java: Byte-Array per for-Schleife schicken?

Guten Tag,

ich sende dem Server mit folgendem Code eine Reihe von Arrays. Als Erstes die Anzahl der Arrays und danach die Byte-Arrays:

Socket client = new Socket("localhost", 1612);
OutputStream outStream = client.getOutputStream();
ObjectOutputStream ous = new ObjectOutputStream(outStream);
ous.flush();

PrintWriter outP = new PrintWriter(outStream);
outP.println(out.Final_Filebyte.length); // Sende die Anzahl der Elemente
outP.flush();

for (int i = 0; out.Final_Filebyte.length > i; i++) {
  byte[] send = out.Final_Filebyte[i];
  ous.writeObject(send); // sende die Arrays
  ous.flush();
}

und empfange die Daten auf dem Server dann mit folgenden Code:

ObjectInputStream ois = new ObjectInputStream(inStream);
BufferedReader inS = new BufferedReader(new InputStreamReader(inStream));
String input = inS.readLine();
inS.close();
int leng = Integer.parseInt(input); // erhalte die Anzahl und schreibe sie in ein int

byte[][] bytes = new byte[leng][]; // erstelle das Array wo die Daten hineingeschrieben werden

for (int i = 0; leng > i; i++) {
  outO = ois.readObject();
  byte[] getByte = (byte[]) outO;
  bytes[i] = getByte; // empfange die Daten und speichere sie
}

Dabei bekomme ich folgenden Fehler:

java.net.SocketException: socket closed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.socketRead(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.io.ObjectInputStream$PeekInputStream.peek(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.peek(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at sendTest.inWirter.run(inWirter.java:52)
    at java.lang.Thread.run(Unknown Source)

Wie kann ich das Problem lösen?

Schon einmal Danke im Voraus. 😊

programmieren, Java
2 Antworten
Java: Connection reset by peer: socket write error?

Ich habe bei folgenden (das ist um einen zip Datei zu verschicken) den Error:

java.net.SocketException: Connection reset by peer: socket write error
	at java.net.SocketOutputStream.socketWrite0(Native Method)
	at java.net.SocketOutputStream.socketWrite(Unknown Source)
	at java.net.SocketOutputStream.write(Unknown Source)
	at java.nio.file.Files.copy(Unknown Source)
	at java.nio.file.Files.copy(Unknown Source)
	at x46.Server.Server_in.run(Server_in.java:59)
	at java.lang.Thread.run(Unknown Source)

Mein Client (senden der Datei):

		try {
    Socket client = new Socket("localhost", 16102);

    // strems
    // out
    OutputStream outStream = client.getOutputStream();
    PrintWriter out = new PrintWriter(outStream);

    // in

    InputStream inStream = client.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(inStream));


    // -------------------------------------------------------
    
			File file = new File("send.zip");
			// init array with file length
			byte[] bytesArray = new byte[(int) file.length()];
			  FileInputStream fis = new FileInputStream(file);
			  fis.read(bytesArray); //read file into bytes[]
			  fis.close();
		

			  outStream.write(bytesArray);
			  outStream.flush();

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


Server(empfängt die Datei):

		try {
			ExecutorService executor = Executors.newFixedThreadPool(2);
        // server starten
        ServerSocket server01 = new ServerSocket(16102);

        System.out.println("Warte auf clients auf dem Port " + server01.getLocalPort());
        // --------------------------------------------------------------
        while (true) {

            // connection aktiviren
            Socket connection = server01.accept();
            System.out.println("Client verbingung unter: " + connection.getRemoteSocketAddress());

            // -------------------------------------------------------------

            // strems
            // out
            OutputStream outStream = connection.getOutputStream();
            PrintWriter out = new PrintWriter(outStream);

            ObjectOutputStream oos = new ObjectOutputStream(outStream);
            // in

            InputStream inStream = connection.getInputStream();
            BufferedReader in = new BufferedReader(new InputStreamReader(inStream));

            // ------------------------------------------------------------

            // Path path = Paths.get("test.zip");
            File f = new File("test.zip");
            Files.copy(f.toPath(), outStream);
        }
    } catch (IOException e) {
        e.printStackTrace();

    }


Also woher kommt der Error?

Server, Computer, Java, java programmieren
2 Antworten