CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • Installing SQL Server 2008
  • Writing UDFs for Firebird Embedded SQL Server
  • [Updated] Shutdown Manager
  • Building Windows Azure Cloud Service Applications with Azure Storage and the Azure SDK

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Network Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Network Programming Chat about all aspects of network programming, including raw sockets, Winsock API, BSD sockets, MFC-based CSocket, CAsyncSocket, and other network-related topics.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old November 5th, 2009, 05:05 AM
    thinkDiscrete thinkDiscrete is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 3
    thinkDiscrete is an unknown quantity at this point (<10)
    Angry error sending image as char array

    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);
    Reply With Quote
      #2    
    Old November 5th, 2009, 10:21 AM
    hoxsiew hoxsiew is offline
    Senior Member
     
    Join Date: Feb 2005
    Posts: 1,189
    hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)hoxsiew  is a jewel in the rough (300+)
    Re: error sending image as char array

    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.

    Code:
    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.
    Reply With Quote
      #3    
    Old November 5th, 2009, 02:44 PM
    thinkDiscrete thinkDiscrete is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 3
    thinkDiscrete is an unknown quantity at this point (<10)
    Re: error sending image as char array

    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.
    Reply With Quote
      #4    
    Old November 5th, 2009, 04:04 PM
    MikeAThon MikeAThon is offline
    Elite Member
    Power Poster
     
    Join Date: Nov 2002
    Location: California
    Posts: 4,123
    MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)
    Re: error sending image as char array

    Quote:
    Originally Posted by thinkDiscrete View Post
    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
    Reply With Quote
      #5    
    Old November 6th, 2009, 06:58 AM
    thinkDiscrete thinkDiscrete is offline
    Junior Member
     
    Join Date: Nov 2009
    Posts: 3
    thinkDiscrete is an unknown quantity at this point (<10)
    Re: error sending image as char array

    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 ?
    Reply With Quote
      #6    
    Old November 6th, 2009, 03:38 PM
    MikeAThon MikeAThon is offline
    Elite Member
    Power Poster
     
    Join Date: Nov 2002
    Location: California
    Posts: 4,123
    MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)MikeAThon has a brilliant future (2000+)
    Re: error sending image as char array

    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/Soc...eTransfer.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

    Last edited by MikeAThon; November 6th, 2009 at 03:40 PM.
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Network Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 01:01 AM.



    Acceptable Use Policy

    internet.comMediabistrojusttechjobs.comGraphics.com

    WebMediaBrands Corporate Info


    Advertise | Newsletters | Feedback | Submit News

    Legal Notices | Licensing | Permissions | Privacy Policy


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
    Copyright WebMediaBrands Inc. 2002-2009