Porting an application from little-endian architecture to big-endian

I have a TCP server developed on x86 architecture using C under Linux using berkley socker API. The server is working fine without any problems. But now for some reason I have to start the server on MIPS architecture with architecture big-endian.

The server and clients communicate through a set of predefined protocols. I will give an example of how the server sends a simple message to clients:

struct echo_req req;

  req.header.version = OFP_VERSION;
  req.header.type = OFPT_ECHO_REQUEST;
  req.header.length = htons (sizeof req);
  req.header.xid = htonl(y);
  req.data = htonl (456);

char data[sizeof (req)];
data[0] = req.header.version;
data[1] = req.header.type;
memcpy (data + 2, &req.header.length, 2);
memcpy (data + 4, &req.header.xid, 4);
memcpy (data + 8, &req.data, 4);

  if ((send (sock_fd, &data, sizeof (data), 0) == -1))
    {
      printf ("Error in sending echo request message\n");
      exit (-1);
    }
printf("Echo Request sent!\n");

As you can see, I use htonland htonsfor any type of length byte to convert it into the network byte order. After compiling the package, I serialize and pack the data into an array charand finally send it to netowrk.

, Big-endian, . , memcpy , , , memcpy , Big-endian. , , , , , , , :). , , . .

+5
2

, memcpy .

, , hton (l | s) , . , - , , .

, .

+3

, ntoh/ntos ?

, ; , , , .

0

All Articles