Sending an array of astruct over a TCP socket (Using C).

  • Follow


Hi,
   I have been having a problem with sending information over a TCP
socket using an array of structures. I have basically an image that I'd
like to send to a server

typedef struct {

   float r, g, b;
} rgbstruct;

The image is declared as follows:
rgbstruct buff[200][200];

Everytime, I try to send it using the write command it sends OK on the
client side, but the server doesn't receive all the data. Most of the
time it stops receiving the last row.
I have tried sending it one row at a time, and have tried sending it
all at once, and nothing worked. Someone mentioned that I have to use
select in order to do this, but nothing seems to still be working. If
someone has an idea on how to fix this problem, that would be greatly
appreciate it.

The send command: write(sd, buff, sizeof(rgbstruct) * 200 * 200);

the receive is in a while loop reading a chunk at a time and
incrementing the pointer.

I also tried sending it using: write (sd, buff[i], sizeof(rgbstruct) *
200); in a for loop looping for 200 times, and on the receiver's side,
I am receiving them one row at a time as well.


Thanks

0
Reply abukmail (1) 10/1/2005 3:20:35 AM

In article <1128136835.020826.214540@z14g2000cwz.googlegroups.com>,
 "GoBucks93" <abukmail@gmail.com> wrote:

> Hi,
>    I have been having a problem with sending information over a TCP
> socket using an array of structures. I have basically an image that I'd
> like to send to a server
> 
> typedef struct {
> 
>    float r, g, b;
> } rgbstruct;
> 
> The image is declared as follows:
> rgbstruct buff[200][200];
> 
> Everytime, I try to send it using the write command it sends OK on the
> client side, but the server doesn't receive all the data. Most of the
> time it stops receiving the last row.
> I have tried sending it one row at a time, and have tried sending it
> all at once, and nothing worked. Someone mentioned that I have to use
> select in order to do this, but nothing seems to still be working. If
> someone has an idea on how to fix this problem, that would be greatly
> appreciate it.
> 
> The send command: write(sd, buff, sizeof(rgbstruct) * 200 * 200);
> 
> the receive is in a while loop reading a chunk at a time and
> incrementing the pointer.
> 
> I also tried sending it using: write (sd, buff[i], sizeof(rgbstruct) *
> 200); in a for loop looping for 200 times, and on the receiver's side,
> I am receiving them one row at a time as well.

Both ways should work.

How does the receiving code know when to exit the loop?  If it's reading 
until EOF, do you close the socket after the write()?

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
0
Reply Barry 10/1/2005 5:25:30 AM


Thanks for the reply,
   I am not closing the socket. Actually I am supposed to be sending
back the results to the client program, but it is not even getting
there as it's not getting past the receive. Another thing that is
happening is that the code works perfectly if the client and the server
are running on the  same machine.

The order of sending:
send one structure (big structure)
then send the rgbs (large array of structures)
then send another small 2-D arrays of structures
then send another small 1-D array of structures

All of this is happening one after the other. So, might there be a
synchronization problem? But it's working if both are on the same
machine.

Thanks,

0
Reply GoBucks93 10/1/2005 10:47:23 AM

"GoBucks93" <abukmail@gmail.com> writes:
>    I have been having a problem with sending information over a TCP
> socket using an array of structures. I have basically an image that I'd
> [...]
> Everytime, I try to send it using the write command it sends OK on the
> client side, but the server doesn't receive all the data. Most of the
> time it stops receiving the last row.

TCP is buffered.  You should use send(2) with MSG_DONTWAIT and recv(2)
instead of write(2) and read(2)!

Or set socket options:

       SO_RCVLOWAT and SO_SNDLOWAT
              Specify the minimum number of bytes in  the  buffer  until
              the  socket  layer  will  pass  the  data  to the protocol
              (SO_SNDLOWAT) or  the  user  on  receiving  (SO_RCVLOWAT).
              These  two  values  are  not changeable in Linux and their
              argument size is always fixed to 1  byte.   getsockopt  is
              able  to  read them; setsockopt will always return ENOPRO�
              TOOPT.

       SO_SNDBUF
              Sets or gets the maximum socket send buffer in bytes.  The
              default  value  is  set by the wmem_default sysctl and the
              maximum allowed value is set by the wmem_max sysctl.

       SO_RCVBUF
              Sets or gets the maximum socket receive buffer  in  bytes.
              The  default  value  is set by the rmem_default sysctl and
              the maximum allowed value is set by the rmem_max sysctl.


In other words, RTFM!

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
In deep sleep hear sound,
Cat vomit hairball somewhere.
Will find in morning.
0
Reply Pascal 10/1/2005 1:18:06 PM

3 Replies
759 Views

(page loaded in 0.089 seconds)

Similiar Articles:













7/23/2012 9:56:45 AM


Reply: