Re: sending structure client server
- From: s0suk3@xxxxxxxxx
- Date: Sat, 11 Oct 2008 23:40:40 -0700 (PDT)
On Oct 11, 4:47 pm, Alex Fraser <me@xxxxxxxxxxx> wrote:
Ian Collins wrote:
s0suk3@xxxxxxxxx wrote:
On Oct 11, 11:19 am, swetha <laptop545@xxxxxxxxx> wrote:
Hi ,
I want to send a structure (user defined) for example :
struct bank{
int empid;
char name[50];
float amount;
} per;
I want to send this from a client to server on unix.Can any1 tell meThis is the basic algorithm for a client app, using stream sockets
how to send this(from client) and read the data at the other end(at
server).tnx in advance...
(prototyped):
The code you posted will only work on a homogeneous network. If either
the client or sever have a different representation of the struct, all
bets are off.
And the solution is to turn the data into a stream of bytes (at the
sender) and back again (at the receiver), according to some protocol.
This is similar to defining a file format.
Not only that, but:
<quote>
if (recv(sockfd, &b, sizeof(struct bank), 0) != sizeof(struct
bank))
// an error occurred or we didn't receive enough data
exit(EXIT_FAILURE);
</quote>
Is logically wrong for a stream socket. A call to recv() returning less
than the requested number of bytes should not be considered a failure.
On a blocking socket, recv() will return when /any/ bytes are available,
or if/when an error is detected, or if interrupted by a signal. A
"short" recv() may happen at any time. As an example, it is virtually
guaranteed for a TCP socket if the requested size is greater than the
MSS and the link between the endpoints is slow (eg dial-up modem).
This possibility can be handled by putting the call in a loop, adjusting
the pointer and requested size in the recv() call according to any data
previously received.
Of course; that's what's always done with stream sockets. Like I said,
that was a "basic" algorithm for this kind of stuff; it wasn't
intended as a complete program.
One real solution to the whole problem would be what Andrew Gabriel
suggested. A better solution, IMO, would be to create a protocol
designed specifically for the needs of your app, or to use an existing
one, such as HTTP, perhaps by extending it. You could then transmit
the data as either text (in the headers of the message, for example),
or as binary (in the body of the message). Of course, sending it as
binary would lead us back to the same problem: struct representation.
Suppose we were to extend HTTP in order to send responses containing a
struct serialized using what Andrew Gabriel suggested. We could add an
"application/xdr" media type. The response could look like this:
HTTP/1.1 200 OK
Content-Type: application/xdr
Content-Length: 50
<binary data corresponding to the serialized struct>
(It should also contain the Date and Server headers, of course :-)
Sebastian
.
- Follow-Ups:
- Re: sending structure client server
- From: Rainer Weikusat
- Re: sending structure client server
- References:
- sending structure client server
- From: swetha
- Re: sending structure client server
- From: s0suk3
- Re: sending structure client server
- From: Ian Collins
- Re: sending structure client server
- From: Alex Fraser
- sending structure client server
- Prev by Date: Re: sending structure client server
- Next by Date: Re: sending structure client server
- Previous by thread: Re: sending structure client server
- Next by thread: Re: sending structure client server
- Index(es):
Relevant Pages
|