Click to See Complete Forum and Search --> : error sending image as char array


thinkDiscrete
November 5th, 2009, 05:05 AM
i am trying to send image c++ server to java client. by using opencv lib a convert to rgb char array. until now everything is ok. but when i try to send char array, after a certain number code cannot send.ie: it sends only first 10000 char. can it be about encoding problem ? imageData method may return signed char, does send method takes char as unsigned ?

other side is java. i read data with readline() method in java. i stops the connection after '\n' and '\r' comes, but i know i dont send this chars inside string.

my partial code is below

img = cvLoadImage( name, -1 );

for(k=0;k<270000;k++){

buffer2[k]=img->imageData[k]; // reads rgb values of image
}
buffer2[k] = '\n';

retval = send(msgsock,buffer2,270001,0);

hoxsiew
November 5th, 2009, 10:21 AM
Why the "\n" on the end? Seems out-of-place.

With a buffer that big, you're probably seeing the effects of "chunking" by the stack. You should read the returns of both send() on the server side and recv() on the client side to ensure that all data is sent/received and if not, you need to keep send()ing or recv()ing until it is done. This is typically done in a loop with some kind of timeout.


int SendAll(SOCKET s,void *ph,int len)
{
fd_set w,e;
struct timeval tval;
int i,retval,done=0,total=0;
unsigned timeout=2; //2 second timeout

while(!done){

FD_ZERO(&w);FD_ZERO(&e);
FD_SET(s,&w);FD_SET(s,&e);
tval.tv_sec=timeout;
tval.tv_usec=0;
retval=select(s+1,NULL,&w,&e,&tval);
if(retval==0){
//TIMEOUT
return 0;
}
if(retval==-1){
//ERROR
return -1;
}
if(FD_ISSET(s,&e)){
//ERROR
return -1;
}
if(FD_ISSET(s,&w)){
i=send(s,((char *)ph)+total,len-total,0);
if(i==-1){
//ERROR
return -1;
}
if(i==0){
return 0;
}
total+=i;
if(total==len){
done=1;
}
}

}//done
return total;
}


Receiving is pretty much the same.

thinkDiscrete
November 5th, 2009, 02:44 PM
Size of buffer can be problematic but when i sketch 300.300 image in paint and try to send it send. But in photos tanen from real life or some screen shots it cannot send. What is the difference between this two image. Two of them are 24 bit.

MikeAThon
November 5th, 2009, 04:04 PM
i am trying to send image c++ server to java client. ...
other side is java. i read data with readline() method in java. i stops the connection after '\n' and '\r' comes, but i know i dont send this chars inside string.
Most likely, the image contains data that is coincidentally the same as \r or \n. In hex, \r is 0x0D (carriage return) and \n is 0x0A (line feed). Your image data probably contains these hex values purely as coincidence. And since the java client is using the readline() method, it naturally misinterprets this data as actual CR or LF characters.

I don't know enough about java, but you proabaly should use some other method for bulk reading of raw bytes.

Mike

thinkDiscrete
November 6th, 2009, 06:58 AM
i checked the sending chars whether they are '\n' or '\r'. and if they are one of both, replaced them with any other constant char say 'a'. but still sending crashes. while sending char are starting or ending bits attached to char ? or they are sent in order without any starting bits ? if so one part of hex value of char and other part of hex value of char may constitute new line char ?

MikeAThon
November 6th, 2009, 03:38 PM
The sending side "crashes"? You didn't mention this before, please elaborate.

I don't know what you mean by "one part of the hex value of a char". But whatever you might mean, you need to discard the concept of characters when thinking about TCP network transmissions. TCP does not care what the data represents. It does not care that you might want to think about the data as a character or as data in an image. As far as TCP is concerned, the data is just a stream of bytes, nothing more. Any meaning given to the bytes must be given by the sender and the recipient, because to TCP, the data is nothing more than pure hex bytes, one after the other.

Try reading this article: "Network Transfer Of Files Using MFC's CSocket Class" at http://www.codeproject.com/KB/IP/SocketFileTransfer.aspx . The article discusses use of MFC classes, but it also includes a discussion of important concepts when sending files over a network.

In my experience, the two most common errors are:
(1) failure to check the return values of the functions that send or receive data, to ensure that all data was actually sent or received, coupled with a failure to call these functions repeatedly until all data was sent or received as expected; and
(2) use of text functions (like strlen() or readline()) when dealing with pure data, for the reason that text functions typically terminate when they encounter a NULL byte. A NULL byte is a perfectly acceptable value for pure data, and use of text functions will prematurely terminate processing of the data.

Mike