-2

hi i transferred an image from server socket to client socket between two android devices. i got the image but it is 0 bytes in size.here is my code

sender:

  // LISTEN FOR INCOMING CLIENTS
                    Socket client = serverSocket.accept();
                    File myFile = new File("/sdcard/DCIM/d.png");
                    byte [] mybytearray = new byte[(int)myFile.length()];
                    FileInputStream fis = new FileInputStream(myFile);
                    BufferedInputStream bis = new BufferedInputStream(fis);
                    bis.read(mybytearray,0,mybytearray.length);
                    OutputStream os = client.getOutputStream();
                    serverStatus.setText("sending...");
                    os.write(mybytearray,0,mybytearray.length);
                    os.flush();
                    client.close();
                    handler.post(new Runnable() {

                        @Override
                        public void run() {
                            serverStatus.setText("Connected.");

                        }
                    });

receiver:

 public void run() {
        try {
            InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
            Log.d("ClientActivity", "C: Connecting...");
            Socket socket = new Socket(serverAddr, ServerActivity.SERVERPORT);
            connected = true;
            byte[] mybytearray = new byte[filesize];
            InputStream is = socket.getInputStream();
            FileOutputStream fos = new FileOutputStream("/sdcard/j.png");
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            bytesRead = is.read(mybytearray,0,mybytearray.length);
            current = bytesRead;
            do{
                bytesRead = is.read(mybytearray,current,(mybytearray.length-current));
                if(bytesRead > 0){
                    current +=bytesRead;
                }
            }while(bytesRead > 0);
            bos.write(mybytearray,0,current);
            bos.flush();
            long end = System.currentTimeMillis();
            System.out.println(end-start);
            bos.close();
            socket.close();
            while (connected) {
                try {
                    Log.d("ClientActivity", "C: Sending command.");
                    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket
                                .getOutputStream())), true);
                        // WHERE YOU ISSUE THE COMMANDS
                        out.println("Hey Server!");
                        Log.d("ClientActivity", "C: Sent.");
                } catch (Exception e) {
                    Log.e("ClientActivity", "S: Error", e);
                }

is anything wrong in this code? thanks in advance..

4

1 回答 1

1

这段代码有什么问题吗?

你打赌。

byte [] mybytearray = new byte[(int)myFile.length()];

在这里,您假设 (1) 文件的长度适合 anint,和 (2) 文件内容适合内存。

bis.read(mybytearray,0,mybytearray.length);

在这里,您假设读取填充了缓冲区。

byte[] mybytearray = new byte[filesize];

在这里,您再次假设 (3) 文件内容将适合内存,并且 (4) 这filesize确实是文件的正确长度。您还没有展示接收者如何可能知道这一点。

bytesRead = is.read(mybytearray, 0, mybytearray.length);
current = bytesRead;
do {
    bytesRead = is.read(mybytearray, current, (mybytearray.length - current));
    if (bytesRead > 0) {
        current += bytesRead;
    }
} while (bytesRead > 0);
bos.write(mybytearray, 0, current);

在这里,您 (5) 没有检查第一次读取的结果,并且 (6) 毫无意义地将整个输入读取到缓冲区中。你不需要这样做。在 Java 中复制流的规范方法如下:

while ((count = in.read(buffer)) < 0){
    out.write(buffer, 0, count);
}

它在两端工作,即用于发送和接收,并且任何缓冲区大小都大于零。使用它来发送和接收文件。

于 2013-10-27T01:04:23.817 回答