简体   繁体   中英

Transfer of image over network via TCP Socket in JAVA isn't working?

I have the following code. It seems that I am streaming the content accurately. However, the image is not being captured correctly. I am new to Java. Basically, I am a C, C++, linux programmer. I am wondering that the problem is reading the buffer line by line. Am I missing something here?

Here is the socket server code -

import java.io.*;
import java.net.*;


public class ImageSocketServer {

    public static void main(String args[]) throws IOException
    {

        ImageSocketServer imageServer = new ImageSocketServer();
        imageServer.run();

    }

    private void run() throws IOException {
        // TODO Auto-generated method stub

        ServerSocket serverSock = new ServerSocket(1025);
        Socket sock = serverSock.accept();

        InputStream imagetoShare = new BufferedInputStream(new FileInputStream("/export/home/joshis1/Lizard.png"));

        PrintStream imageSend = new PrintStream( sock.getOutputStream());
        imageSend.print(imagetoShare);

    }

}

Here is the socket client code -

import java.io.*;
import java.net.*;

public class ImageSocketClient {

    public static void main(String args[]) throws IOException
    {

        ImageSocketClient imageClient = new ImageSocketClient();
        ImageSocketClient.run();

    }

    private static void run() throws UnknownHostException, IOException
    {
        // TODO Auto-generated method stub

        BufferedWriter bufWriter = null;  
        bufWriter = new BufferedWriter(new FileWriter(  
                "/export/home/joshis1/file1.png"));  
        Socket sock = new Socket("localhost", 1025);
        InputStreamReader IR = new InputStreamReader(sock.getInputStream());
        BufferedReader BR = new BufferedReader(IR);

        String data;  
        while ((data = BR.readLine()) != null)
        {  
            System.out.println("Shreyas got the data");
            bufWriter.write(data);  
        }  

        bufWriter.close();  
    }


}

I see that the source image is of size -

$ ls -l Lizard.png 
-rw-rw-r-- 1 joshis1 joshis1 19071522 May 29 15:46 Lizard.png

and the destination image is wrongly copied -

$ ls -l file1.png 
-rw-rw-r-- 1 joshis1 joshis1 34 May 29 17:38 file1.png

First of all your imageSend.print(imagetoShare); sends over the String representation of an InputStream , which explains the small content of the file. You'll want to create a loop that reads from imagetoShare (although you might want to name it better, it's not an image, it's a stream) and writes the data to the outputstream (search around for the quintessential read-write loop).

Secondly, you're using PrintStream which is used to write character data to an OutputStream . You want to use a BufferedOutputStream for that.

There's a variety of errors in your code due to wrong use of the various java.io -classes.

1.) You are using PrintStream 's print(Object o) method. This is not copying the stream's content but is just writing a textual representation of the object.

2.) In your client you are using Reader and Writer classes. These are used to handle character-data while your images are raw binary data. You'll get into lots of trouble considering encoding, non-printable characters etc. this way.

To wrap it up: Use plain BufferedInputStream s and BufferedOutputStream s to do your input and output. You'll have to wrap it all up in some loops because you'll only be reading a bunch of bytes at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM