简体   繁体   中英

Sending image from C# Server to Java client through TCP socket

I have an image in my server which I want to send to my Java client through sockets. In c# I converted to byte array and then tried to send over the socket. But, a byte array in C# is unsigned so I tried to send signed byte array sbyte[] but it cannot be send by clientSocket.Send() method. On the java client side, I need to convert the byte array received to an Image object. Here is the exception trace that I get, at Image image = reader.read(0, param) . Please help me with this.

Exception in thread "main" javax.imageio.IIOException: Bogus marker length
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readImageHeader(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readNativeHeader(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.checkTablesOnly(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.gotoImage(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readHeader(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.readInternal(Unknown Source)
at com.sun.imageio.plugins.jpeg.JPEGImageReader.read(Unknown Source)
at ServerByteStreamWithoutOIS.main(ServerByteStreamWithoutOIS.java:54)

Here is my C# server code:

class Program
{
static void Main(String[] args)
{
Socket sListen = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

// 2. Fill IP
IPAddress IP = IPAddress.Parse("147.174.117.187");
IPEndPoint IPE = new IPEndPoint(IP, 20229);

// 3. binding
sListen.Bind(IPE);

// 4. Monitor
Console.WriteLine("Service is listening ...");
sListen.Listen(2);

// 5. loop to accept client connection requests
while (true)
{
Socket clientSocket;
try
{
clientSocket = sListen.Accept();
}
catch
{
throw;
}


// send the file
byte[] buffer = ReadImageFile("1.jpg");

clientSocket.Send(buffer, buffer.Length, SocketFlags.None);
clientSocket.Close();
Console.WriteLine("Send success!");
}
}


private static byte[] ReadImageFile(String img)
{
FileInfo fileInfo = new FileInfo(img);
byte[] buf = new byte[fileInfo.Length];
FileStream fs = new FileStream(img, FileMode.Open, FileAccess.Read);
fs.Read(buf, 0, buf.Length);
fs.Close();
//fileInfo.Delete ();
GC.ReRegisterForFinalize(fileInfo);
GC.ReRegisterForFinalize(fs);
return buf;
}
}
}

Here is my Java client:

public class ServerByteStreamWithoutOIS {
public static void main(String[] args) throws IOException, ClassNotFoundException{
int port = 20229;
Socket sock = null; 
InetAddress addr = null;
addr = InetAddress.getByName("147.174.117.187");    
sock = new Socket(addr, port);
System.out.println("created socket!");
int count = 0;
while(true){
String line = "";
String realLine = "";
BufferedReader bReader = new BufferedReader(new InputStreamReader(sock.getInputStream()));
byte[] buffer = null;
while((line=bReader.readLine() )!=null){
realLine = realLine + line;
System.out.println(line.getBytes());
}

buffer = realLine.getBytes();

//buffer = (byte[])ois.readObject();

ByteArrayInputStream bis = new ByteArrayInputStream(buffer);
Iterator<?> readers = ImageIO.getImageReadersByFormatName("jpg");

ImageReader reader = (ImageReader) readers.next();
Object source = bis; // File or InputStream, it seems file is OK

ImageInputStream iis = ImageIO.createImageInputStream(source);
//Returns an ImageInputStream that will take its input from the given Object

reader.setInput(iis, true);
ImageReadParam param = reader.getDefaultReadParam();

Image image = reader.read(0, param);
//got an image file

BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
//bufferedImage is the RenderedImage to be written
Graphics2D g2 = bufferedImage.createGraphics();
g2.drawImage(image, null, null);
File imageFile = new File("image.bmp");
ImageIO.write(bufferedImage, "bmp", imageFile); 

System.out.println(imageFile.getPath());

//Thread.sleep(100);
System.out.println("Done saving");
}
}

}

I believe the error is because you are converting the bytes received in the Java server to a string representation. This can result in an error because a jpg is binary data, and when some binary data can not be converted to a character in a string, some conversion occurs which will result in an error when you use the getBytes() function.

If you instead read the bytes from the inputstream using the read(byte[],int,int]) function, I think you should be alright.

http://download.oracle.com/javase/1.5.0/docs/api/java/io/InputStream.html#read(byte[], int, int)


Edit, added a working code example

The "problem" of Java having signed bytes is a non issue. In binary they are the same bits, so when written to a file, the same bits are written since they are still the same order.

I wrote an example of a C# client and a Java server that I got working. I'm sure you can find it of use.

Server – Java

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ImageServer {
    public static void main(String[] args) {
        try {
            ServerSocket server = new ServerSocket(8000);
            Socket accept = server.accept();
            InputStream inputStream = accept.getInputStream();
            BufferedInputStream stream = new BufferedInputStream(inputStream);
            int length = readInt(inputStream);
            byte[] buf = new byte[length];
            for (int read = 0; read < length; ) {
                read += stream.read(buf, read, buf.length - read);
            }
            stream.close();

            FileOutputStream fos = new FileOutputStream("image.png");
            fos.write(buf, 0, buf.length);
            fos.flush();
            fos.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static int readInt(InputStream inputStream) throws IOException {
        byte[] buf = new byte[4];
        for (int read = 0; read < 4; ) {
            read += inputStream.read(buf, 0, 4);
        }
        return toInt(buf);
    }

    public static int toInt(byte[] b) {
        return (b[0] << 24)
                + ((b[1] & 0xFF) << 16)
                + ((b[2] & 0xFF) << 8)
                + (b[3] & 0xFF);
    }
}

Client – C#

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

namespace Test.SendImageClient {
    public class Program {
        public static void Main(string[] args) {
            if (args.Length == 0) {
                Console.WriteLine("usage: client imagefile");
                return;
            }
            FileStream stream = File.OpenRead(args[0]);

            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.Connect("localhost", 8000);
            int length = IPAddress.HostToNetworkOrder((int)stream.Length);
            socket.Send(BitConverter.GetBytes(length), SocketFlags.None);

            byte[] buffer = new byte[1024];
            int read;
            while ((read = stream.Read(buffer, 0, buffer.Length)) > 0) {
                socket.Send(buffer, 0, read, SocketFlags.None);
            }
            socket.Close();
        }
    }
}

it seems like the bytes being received are not lining up w/the jpeg specification (can't be deserialized properly). are you sure the file on the server exists and that the c# byte[] is getting filled properly? maybe try to write the file on the server before sending it through the socket to ensure that you are actually reading a valid jpeg on the server.

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