0

So I know many of you will advise against this but I want to send floats via TCP/IP to a TCP Server running on a SoC (Zynq7020, with the server on the Arm A9). I want to get this working as a simple proof of concept before moving onto using UDP which is probably more suited to sending raw data. I am having trouble converting the float into a char array on the client side and back again on the server. Although I am not sure this is the best approach.

I am using the WinSocket API, and am sending the data as so (although I am not sure this is correct or efficient!):

char * sendbuf;
sendbuf = reinterpret_cast<char *>(f);
iResult = send(ConnectSocket, sendbuf, (int) strlen(sendbuf), 0);

Basically is this correct, and without using UDP, what is the best way to floats via TCP. I guess decoding will essentially be reverse engineering the method used to send the float as char*.

---Below is a complete working example of sending floats via WinSock to a Zynq ARM cpu LwIP--

Host Windows 7 (htonf maybe used, but I am on x86 so there are no endian issues):

int FPGA_Accelerator::sendFloat(float* f) {
    char *sendbuf;
    int iResult;
    char arr[4];

    *(float*)arr= *f;

    printf("Sending "); printf(arr); printf("\n");
    iResult = send(connection, arr, (int) sizeof(arr), 0);
    if (iResult == SOCKET_ERROR) {
    printf("send failed: %d\n", WSAGetLastError());
    closesocket(connection);
    WSACleanup();
    return -1;
    }

    printf("Bytes Sent: %ld\n", iResult);

    return 1;
};

Zynq code. Where in the recv_callback I call the function parse_input(p->payload)

void parse_input(char* input){
    xil_printf("Input : ");
    xil_printf(input);
    xil_printf("\n\r");

u32 d = *(u32*)input;
union v{
    float f;
    u32 u;
};
union v val;
val.u = d;

printf("Value u32 = %i \n\r",val.u);
printf("Value float = %f \n\r",val.f);

}
3
  • 1
    «UDP which is probably more suited to sending raw data.» This is not true. UDP/TCP has nothing to do about rawness of data. In both cases, if you need to send the same float you'll have to use the same encoding :) Apr 29, 2014 at 21:49
  • 1
    I would read a little about ASN.1, it might help if your data gets more complex
    – SJuan76
    Apr 29, 2014 at 21:50
  • C ist not C++. They're not the same, still. Mar 29, 2021 at 17:23

3 Answers 3

1

It doesn't matter what you send is float or string. You send data as array of bytes and receive them on the other side and interpret them as you wish. So in your case, you should send a float which is 4 bytes long. You should send 4 bytes as array of bytes and on the other side cast it to a float and use it.

iResult = send(ConnectSocket, (char*)&f, (int) sizeof(float), 0);

in receiver part:

char *receivebuf;
// read data from socket
float *receivedFloat = (float*)receivebuf;

Using UDP is easier, because you don't have to check connection and success of sent data. You just send it without connecting to the server.

0

This is not a question of TCP vs UDP.

First, to send a single float value, you don't reinterpret it as an address, but take the address of the float

iResult = send(ConnectSocket, &f, (int) sizeof(f), 0);

But this works only, if you have the same architecture on both ends of the communication.

To make it independent of architecture issues, you must use some External Data Representation for the transmission from one system to another. Alternatively, you can also use a text based protocol and convert the float value to a string.

4
  • 1
    htonf(f) should be adequate for OP's case. I don't see much benefit in converting f to a string
    – Red Alert
    Apr 29, 2014 at 22:04
  • 1
    Ths is wrong. strlen(f) makes no sense if f is a float or a pointer to a float. Apr 29, 2014 at 23:28
  • @RedAlert, as you said I need to use htonf, but I am running Windows 7 and it is not in the WinSock API, (I cannot call the function, and on the WinSock API page it states that Windows 8 minimum is needed for htonf) are there any alternatives?
    – Sam Palmer
    May 1, 2014 at 11:35
  • @SamPalmer You don't have to use htonf specifically. htonl() will convert any 32 bit input for you, so you can use that instead. Note that on the receiving end, you have to use ntohl() before you can consider it valid.
    – Red Alert
    May 1, 2014 at 17:54
-1

This is incorrect. As its name suggests, strlen is for strings. You need to pass the actual size of the buffer to send.

1
  • the example (well the use of the send method, not the cast) I gave is correct and is a direct copy from the MSDN WinSocket example.
    – Sam Palmer
    Apr 30, 2014 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.