English 中文(简体)
通过TCP的袖子书向Jas的 client服务公司提供卫星图像
原标题:Sending image from C# Server to Java client through TCP socket

我的服务器中有一个形象,我想通过袖珍向我的 Java客户发送。 c I号转换成单体阵列,然后试图从中转。 但是,C#的一组星阵没有签名,因此,我试图通过星阵签名,但不能通过<条码>发送。 在java客户方面,我需要将所收到的外围阵列变成一个图像物体。 下面是我用<代码>Image 图像 = 阅读器.read(0, param)获取的例外情况。 请帮助我这样做。

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");
}
}

}
最佳回答

我认为,这一错误是因为你正在把在贾瓦服务器中收到的tes改成一个强有力的表述。 这可能产生错误,因为单单体数据是双倍数据,而如果某些双倍数据不能转换成星体的特性,则会发生某种换算,在你使用<代码>getBytes()功能时产生错误。

如果你用<条码>读到输入流中的斜体,则我认为你应当直截了当。

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


Edit, 添加了一部工作法典。

Java签署“问题”是一个非问题。 在双轨制中,如写到档案中,也写上同样的字眼,因为它们仍然是相同的。

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签