简体   繁体   English

通过JAVA中的TCP套接字通过网络传输图像不起作用?

[英]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. 我是Java新手。 Basically, I am a C, C++, linux programmer. 基本上,我是C,C ++,Linux程序员。 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); 首先,您的imageSend.print(imagetoShare); sends over the String representation of an InputStream , which explains the small content of the file. 发送一个InputStream的String表示形式,它解释了文件的小内容。 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). 您将需要创建一个从imagetoShare读取的循环(尽管您可能想更好地命名它,但它不是图像,而是流),然后将数据写入到输出流中(搜索典型的读写循环)。

Secondly, you're using PrintStream which is used to write character data to an OutputStream . 其次,您使用的是PrintStream ,它用于将字符数据写入OutputStream You want to use a BufferedOutputStream for that. 您要为此使用BufferedOutputStream

There's a variety of errors in your code due to wrong use of the various java.io -classes. 由于对各种java.io -class的错误使用,代码中会出现各种错误。

1.) You are using PrintStream 's print(Object o) method. 1.)您正在使用PrintStreamprint(Object o)方法。 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. 2)在您的客户端中,您使用的是ReaderWriter类。 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. 总结:使用普通的BufferedInputStreamBufferedOutputStream进行输入和输出。 You'll have to wrap it all up in some loops because you'll only be reading a bunch of bytes at a time. 您必须将其全部包装成一些循环,因为您一次只能读取一堆字节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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