Page 1 of 1

struct int problem

Posted: Thu Sep 02, 2010 2:47 am
by acidice333
Hi there.

So I'm trying to create an application for Wii that communicates with my PC.
The problem I've got is `char` works, but when I use `int` it doesn't work properly. The `int` either displays a number that is totally off, or it just renders the screen to nothing. I made a PC client, and it works fine its just the Wii isn't working properly????

I've got something like for this for both Wii (client) and PC (server) its just a snipplet:

Code: Select all

  struct info{
     int x;
     char a;
   };
PC side

Code: Select all

void main() {
  info rdata;
  rdata.x = 17; 
  rdata.a = 'A';

        int len = sizeof(rdata);
        n = send(newsockfd, &rdata, len, 0);
}  

Wii side:

Code: Select all

struct info recv;
int recvlength;
net_read(connection, &recvlength, 4);
GRRLIB_Printf(255, 115, tex_BMfont5, GRRLIB_LIME, 1, "recvlen: %d",recvlength);

net_recv(connection,&recv,recvlength,0);
GRRLIB_Printf(255, 125, tex_BMfont5, GRRLIB_LIME, 1, "x: %d", recv.x);
GRRLIB_Printf(255, 155, tex_BMfont5, GRRLIB_LIME, 1, "a: %c", recv.a);

Re: struct int problem

Posted: Thu Sep 02, 2010 7:39 am
by vuurrobin
make sure you're writing and reading the variable in the same endian.

Re: struct int problem

Posted: Thu Sep 02, 2010 10:05 am
by WinterMute
Your PC is little endian, the Wii is big endian so you need to make sure that any values you send are either converted or make your data transfer endian agnostic. Conveniently network traffic is designated big endian so there are standard functions provided to convert between host and network byte ordering. See http://linux.die.net/man/3/htons for a list of these functions.

PC Side

Code: Select all

    void main() {
      info rdata;
      rdata.x = htonl(17);
      rdata.a = 'A';

            int len = sizeof(rdata);
            n = send(newsockfd, &rdata, len, 0);
    } 
Wii Side

Code: Select all

GRRLIB_Printf(255, 125, tex_BMfont5, GRRLIB_LIME, 1, "x: %d", ntohl(recv.x));

Re: struct int problem

Posted: Fri Sep 03, 2010 8:21 am
by acidice333
Thank you very much! I've been struggling with this for days and I finally got it working :)

What I found out is if I htonl() on the PC server side, I don't need ntohl() on the Wii client side but I figure it'd be best to to ntohl() it just incase.

Re: struct int problem

Posted: Fri Sep 03, 2010 8:56 pm
by WinterMute
Sure, network byte order is big endian the same as the Wii which makes the ntohl & ntohs functions no ops. You're better leaving them in though, if you ever port your code to a little endian system you'll end up pulling your hair out all over again if you don't.