Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on a project that contains a C# client and a Java(SE) server, using socket (tcp) for communication. When the client send string or integer information, it work just fine, the server can perfectly recieve the exact information. However, I'm now in trouble sending images from the client to the server or in the reversive way. I came up with a probable way by transferring the image to a byte array and send the byte array through socket and after the server recieves it, the server transfers the array to an image again. It sounds doable but after I implemented and debugged this, part of the byte array was "damaged". Note that a C#-byte's range is [0,255] while a Java-byte's range is [-128,127]. So when a C#-byte that is greater than 128 is transmitted to the Java side, it is "damaged".
Please help me out, it'll be apprieciated if you provide a way to transmit images between C# and Java.
Posted

iirc C# byte is unsigned - whereas C# sbyte IS signed ergo [-127,127] so that would be equivalent to java's byte .. so saying, suggesting that you can use that pairing to send image data over the network is fraught with danger

Without know much about imaging encoding/decoding, I find it 'odd' that you would expect negative bytes

If it were me, I'd be looking at putting an abstraction over the c# and java sides, such that they can transmit the data and each end can handle the issues of encoding/decoding (as opposed to 'raw tcp sockets') - there are messaging frameworks out there that would greatly assist you
 
Share this answer
 
Comments
Wu Jiecheng 23-Sep-15 2:59am    
Thanks for answering my question, I found my way out. You can check my solution below:)
You could try to encode your byte array as Base64. That way all values should be between 0 - 127. Then decode it on the Java side back to a byte array.

Convert.ToBase64String Method (Byte[])[^]

Java Class Base64[^]

This approach will give you a bit of an overhead.
 
Share this answer
 
v2
Comments
Wu Jiecheng 23-Sep-15 2:59am    
Thanks for answering my question, I found my way out. You can check my solution below:)
I think I just found the solution. The general idea was the same with what I had said in the question.
About 2 hours earlier, I curiously transferred the very same image to a byte array in C# as well as Java. When I looked into the byte data I found that the [0,127] part of byte data was not "damaged". However, if an element in the C# array is greater than 127, e.g. C# byte: 137, 10001001, the corresponding element in the Java array would be its two's complement, e.g. Java byte: -119, 01110111. I think this is because the encoding and decoding way is different between C# and Java, I'm not quite sure. But all this is just about bit operation, so why do I care about this? All I need to do is to send the byte array from one side to another.
Here is the main methods:
C#:
C#
public static byte[] ImageToBytes(string path)
        {
            FileInfo fileInfo = new FileInfo(path);
            byte[] buffer = new byte[fileInfo.Length];
            using (FileStream stream = fileInfo.OpenRead())
            {
                stream.Read(buffer, 0, buffer.Length);
            }
            return buffer;
        }

C#
public static bool sendBytes(byte[] content)
        {
            try
            {
                int length = content.Length;
                sendCode(length);                
                clientSocket.Send(content);
                return true;
            }
            catch
            {
                return false;
            }
        }


Java:
Java
public static void byte2image(byte[] data, String path) {
		if (data.length < 3 || path.equals(""))
			return;
		try {
			FileImageOutputStream imageOutput = new FileImageOutputStream(
					new File(path));
			imageOutput.write(data, 0, data.length);
			imageOutput.close();
			System.out.println("Make Picture success,Please find image in "
					+ path);
		} catch (Exception ex) {
			System.out.println("Exception: " + ex);
			ex.printStackTrace();
		}
	}

Java
public static byte[] readBytes(){
		DataInputStream din=null;
		int length=readCode();
		byte[] b=new byte[length];
		try {
			din = new DataInputStream(socket.getInputStream());
			din.read(b);
			din.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return b;
	}
 
Share this answer
 
Comments
aarif moh shaikh 23-Sep-15 8:40am    
:) 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900