Problem bei Übertragung von Binärdateien

Status
Nicht offen für weitere Antworten.

jeroen

Mitglied
hi,

ich hänge schon seit einiger Zeit an folgendem Problem:

und zwar will ich eine Binär datei vom einem Server auf einem Android Client übertragen.

Hier ist zur erst mal mein Code:
Server:
Code:
import java.io.*;
import java.net.*;



public class ServerTest 
{
	public static void main(String[] args) throws CanotConnectToServer, IOException
	{
		
		
		if ( args.length != 2)
		{
			System.err.println("usage: ClientTest <port> <file>");
			System.exit(-1);
		}
		
		ServerSocket server = new ServerSocket( new Integer(args[0]) );
		
		while( true )
		{
			Socket socket = server.accept();		
			
			System.out.println("Eingehende Verbindung von " + socket.getInetAddress());
			
			OutputStream out = socket.getOutputStream();
			InputStream fileIn = new FileInputStream( args[1] );
			 
			byte[] buffer = new byte[1024];
			while (fileIn.available() > 0) {
			    out.write(buffer, 0, fileIn.read(buffer));
			}
			 
			fileIn.close();
			socket.close();
		}		
		
	}
}

Hier der Code der die Datenempfängt (nur ausschnitt):
Code:
InetSocketAddress ServerAddress = new InetSocketAddress( "192.168.1.31", 2001 );
Socket s = new Socket();

try 
{
s.connect( ServerAddress , 1000);
	
if ( s.isConnected() )
{				
	InputStream in = s.getInputStream();
				
	byte[] buffer = new byte[ 2500 * 3 + 1 ];
	int bytesRead = 1;
				
	bytesRead = in.read(buffer);			
			
	for(i=0; i<buffer.length; i++)
	{
		Log.e(TAG, i+": " + new Byte(buffer[i]).intValue());
	}
				
	if ( bytesRead != buffer.length )
	{
	Log.e(TAG, "canot read");
	}

Die Datei die ich senden will:
Code:
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff fa ff ff ed ff ff f1 ff ff ff ff ff ff fd ff ff fc ff ff fd ff ff ff ff f8 ff ff f0 fc fc f6 ff ff ff ff ff ff fa fb ff fc fd ff fc fd f6 fb fa f6 fc fb fd ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff

Der empfange Binäre:
Code:
E/UC4:    (  581): 0: -1

E/UC4:    (  581): 1: -1

E/UC4:    (  581): 2: -1

E/UC4:    (  581): 3: -1

E/UC4:    (  581): 4: -1

E/UC4:    (  581): 5: -1

E/UC4:    (  581): 6: -1

E/UC4:    (  581): 7: -1

E/UC4:    (  581): 8: -1

E/UC4:    (  581): 9: -1

E/UC4:    (  581): 10: -1

E/UC4:    (  581): 11: -1

E/UC4:    (  581): 12: -1

E/UC4:    (  581): 13: -1

E/UC4:    (  581): 14: -1

E/UC4:    (  581): 15: -1

E/UC4:    (  581): 16: -1

E/UC4:    (  581): 17: -1

E/UC4:    (  581): 18: -1

E/UC4:    (  581): 19: -1

E/UC4:    (  581): 20: -1

E/UC4:    (  581): 21: -1

E/UC4:    (  581): 22: -1

E/UC4:    (  581): 23: -1

E/UC4:    (  581): 24: -1

E/UC4:    (  581): 25: -1

E/UC4:    (  581): 26: -1

E/UC4:    (  581): 27: -1

E/UC4:    (  581): 28: -1

E/UC4:    (  581): 29: -1

E/UC4:    (  581): 30: -1

E/UC4:    (  581): 31: -1

E/UC4:    (  581): 32: -1

E/UC4:    (  581): 33: -1

E/UC4:    (  581): 34: -1

E/UC4:    (  581): 35: -1

E/UC4:    (  581): 36: -1

E/UC4:    (  581): 37: -1

E/UC4:    (  581): 38: -1

E/UC4:    (  581): 39: -1

E/UC4:    (  581): 40: -1

E/UC4:    (  581): 41: -1

E/UC4:    (  581): 42: -1

E/UC4:    (  581): 43: -1

E/UC4:    (  581): 44: -1

E/UC4:    (  581): 45: -8

E/UC4:    (  581): 46: -1

E/UC4:    (  581): 47: -1

E/UC4:    (  581): 48: -19

E/UC4:    (  581): 49: -1

E/UC4:    (  581): 50: -1

E/UC4:    (  581): 51: -17

E/UC4:    (  581): 52: -1

E/UC4:    (  581): 53: -1

E/UC4:    (  581): 54: -1

E/UC4:    (  581): 55: -1

E/UC4:    (  581): 56: -1

E/UC4:    (  581): 57: -1

E/UC4:    (  581): 58: -3

E/UC4:    (  581): 59: -1

E/UC4:    (  581): 60: -1

E/UC4:    (  581): 61: -4

E/UC4:    (  581): 62: -1

E/UC4:    (  581): 63: -1

E/UC4:    (  581): 64: -3

E/UC4:    (  581): 65: -1

E/UC4:    (  581): 66: -1

E/UC4:    (  581): 67: -1

E/UC4:    (  581): 68: -1

E/UC4:    (  581): 69: -8

E/UC4:    (  581): 70: -1

E/UC4:    (  581): 71: -1

E/UC4:    (  581): 72: -19

E/UC4:    (  581): 73: -6

E/UC4:    (  581): 74: -8

E/UC4:    (  581): 75: -12

E/UC4:    (  581): 76: -1

E/UC4:    (  581): 77: -3

E/UC4:    (  581): 78: -1

E/UC4:    (  581): 79: -2

E/UC4:    (  581): 80: -2

E/UC4:    (  581): 81: -1

E/UC4:    (  581): 82: -5

E/UC4:    (  581): 83: -4

E/UC4:    (  581): 84: -1

E/UC4:    (  581): 85: -6

E/UC4:    (  581): 86: -4

E/UC4:    (  581): 87: -1

E/UC4:    (  581): 88: -4

E/UC4:    (  581): 89: -3

E/UC4:    (  581): 90: -7

E/UC4:    (  581): 91: -2

E/UC4:    (  581): 92: -3

E/UC4:    (  581): 93: -9

E/UC4:    (  581): 94: -3

E/UC4:    (  581): 95: -4

E/UC4:    (  581): 96: -3

E/UC4:    (  581): 97: -1

E/UC4:    (  581): 98: -1

E/UC4:    (  581): 99: -1

Das komische das ein Teil der Übertragung stimmt, aber teile auch nicht.
Könnte bitte jemand von euch über den Code mal drüber gucken.

jeroen
 
hi,

ich hab mal den client code als normales Java Program formuliert.
der effekt ist der gleiche, aber leichter zu testen.

Code:
import java.io.*;
import java.net.*;

public class ClientTest 
{
	static SocketAddress ServerAddress; 

	public static void main(String[] args) throws CanotConnectToServer, IOException
	{
		
		InetSocketAddress ServerAddress = new InetSocketAddress( "192.168.1.31", 2001 );
		Socket s = new Socket();
		int i,j;

		try 
		{
			s.connect( ServerAddress , 1000);
	
			if ( s.isConnected() )
			{				
				InputStream in = s.getInputStream();
			
				
				byte[] buffer = new byte[ 7501 ];
				int bytesRead = 1;
				
				bytesRead = in.read(buffer);			
			
				for(i=0; i<buffer.length; i++)
				{
					System.out.println(i+": " + new Byte(buffer[i]).intValue());
				}
				
				if ( bytesRead != buffer.length )
				{
					System.out.println("canot read");
				}
				
				s.close();
			}
		}
		catch (Exception e) 
		{
			System.err.println(e);
		}

	}
	
	
}


jeroen
 
:rtfm: InputStream.read(byte[]):
API-Doc hat gesagt.:
Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected.
:rtfm: InputStream.available():
API-Doc hat gesagt.:
Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream. The next invocation might be the same thread or another thread. A single read or skip of this many bytes will not block, but may read or skip fewer bytes.

Note that while some implementations of InputStream will return the total number of bytes in the stream, many will not. It is never correct to use the return value of this method to allocate a buffer intended to hold all data in this stream.
Richtig liest man also vom Strom mit: [HIGHLIGHT="Java"]byte[] buffer = new byte[7501];
int bytesRead;
int totalBytesRead = 0;
while ((bytesRead = in.read(buffer)) != -1) {
for (int i = 0; i < bytesRead; i++) {
System.out.printf("%5d: Byte (Hex) %2x", totalBytesRead + i, buffer);
}
totalBytesRead += bytesRead;
}[/HIGHLIGHT]
Variablen deklariert man in Java dort wo man sie braucht; nicht irgendwo vorher.

Ebenius
 
hi,

ich hatte das problem schon vorher gelöst in dem ich datei komplett in den buffer eingelese habe. und dann mit einem schlag auf den socket geschrieben habe.

jetzt hab ich aber ein anders problem:
mach mal wird das bild das ich übertrag nur zur hälft übertragen,
mach mal 3/4 oder 1/4. das die daten nicht komplet verfügbar sind kann ich feststellen, aber warum ist das so ?
bitte sagen wenn ich ihr den code braucht.

jeroen
 
ich hatte das problem schon vorher gelöst in dem ich datei komplett in den buffer eingelese habe. und dann mit einem schlag auf den socket geschrieben habe.
Das löst das Problem nicht wirklich, oder ich habe Dich falsch verstanden. available() gibt nun mal nur die Anzahl der Bytes die bereits in den Puffer des Stroms gelesen wurden und deshalb ohne zu Blockieren vom Strom gelesen werden können. Mach's so, wie ich oben geschrieben habe!

jetzt hab ich aber ein anders problem:
mach mal wird das bild das ich übertrag nur zur hälft übertragen,
mach mal 3/4 oder 1/4. das die daten nicht komplet verfügbar sind kann ich feststellen, aber warum ist das so ?
bitte sagen wenn ich ihr den code braucht.
Sicher, dass es ein anderes Problem ist und nicht das selbe? Code wäre hilfreich, ja.

PS: Ich kaufe ein 'n'.

Ebenius
 
hi,

ich hab deine code mal getest und jetzt werden mehr daten gesendet als die datei groß ist.

server:
[HIGHLIGHT="Java"]OutputStream out; // ist der socket
int bytesRead;
byte[] buffer = new byte[100];

InputStream fileIn = new FileInputStream( "H:\\" + filename );

while ((bytesRead = fileIn.read(buffer)) != -1)
{
for (int i = 0; i < bytesRead; i++)
{
out.write(buffer);
}
}

fileIn.close();[/HIGHLIGHT]

client:
[HIGHLIGHT="Java"]InetSocketAddress ServerAddress = new InetSocketAddress( "192.168.1.31", 2001 );
Socket s = new Socket();
int i,j;

try
{
s.connect( ServerAddress , 1000);

if ( s.isConnected() )
{
InputStream in = s.getInputStream();

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

out.write( "GET bla" );
out.newLine();
out.flush();


byte[] buffer = new byte[100];

int bytesRead;
int totalBytesRead = 0;

while ((bytesRead = in.read(buffer)) != -1)
{
for (i = 0; i < bytesRead; i++)
{
System.out.printf("%5d: Byte (Hex) %2x\n", totalBytesRead + i, buffer);
}

totalBytesRead += bytesRead;
}

if ( bytesRead != buffer.length )
{
System.out.println("canot read");
}

s.close();
}
}
catch (Exception e)
{
System.err.println(e);
}[/HIGHLIGHT]

jeroen
 
Zuletzt bearbeitet von einem Moderator:
*hust*

In Deinem letzten Beitrag im ersten Code-Beispiel: Wenn Du nur eine Anzahl von bytesRead Bytes liest, solltest Du auch nicht mehr verschicken, oder? Zeile 11 ändern in [HIGHLIGHT="Java"]out.write(buffer, 0, bytesRead);[/HIGHLIGHT]
Stimmt's oder hab ich Recht?

Ebenius
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben