简体   繁体   中英

Send a XDR struct over a socket with C in Linux

I'm writing a server that send an XDR struct over a socket. Using Wireshark I see that my client correctly send an XDR message to the server BUT I don't see any data go from the server to the client. But the code seems to be correct as it is the same used for the client. I have see that the problem is xdr_Response. Is there any mistake on it? Thanks to all

XDR xdrs_w;
Response y;

stream_socket_w = fdopen(s, "w");
xdrstdio_create(&xdrs_w, stream_socket_w, XDR_ENCODE);

y.error = 0; 
y.result = 5.7;

xdr_Response(&xdrs_w, &y);
fflush(stream_socket_w);

with:

struct Response {
    bool_t error;
    float result;
};
typedef struct Response Response;

I'm not very expert of XDR, but I found a way that worked to receive data from XDR with a socket connection (on TCP). First you have to do recv to receive the data from your client, then call xdrmem_create() , that need the XDR structure that you'll use to the reading, a buffer (a string), the return value of recv(), and you have to use XDR_DECODE because you're writing from XDR that's codified.

You have to write something like this:

l = recv(socket, buffer, BUFFERDIM, 0);
xdrmem_create(&xdrs_w, buff, l, XDR_DECODE );
if(!xdr_Response(&xdrs_w, &y) {
fprintf(stdout, "Error XDR\n");
}
fprintf(stdout, "Received: %f", y.result);

and y should be filled in. Note that buffer is different from buff . I prefer to do this instead of use fd_open, you've only to create the xdr and call xdr_Response .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM