struct int problem

support for the powerpc toolchain
Post Reply
acidice333
Posts: 5
Joined: Thu Sep 02, 2010 2:28 am

struct int problem

Post by acidice333 » Thu Sep 02, 2010 2:47 am

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);

User avatar
vuurrobin
Posts: 219
Joined: Fri Jul 11, 2008 8:49 pm
Location: The Netherlands
Contact:

Re: struct int problem

Post by vuurrobin » Thu Sep 02, 2010 7:39 am

make sure you're writing and reading the variable in the same endian.

WinterMute
Site Admin
Posts: 1859
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: struct int problem

Post by WinterMute » Thu Sep 02, 2010 10:05 am

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));
Help keep devkitPro toolchains free, Donate today

Personal Blog

acidice333
Posts: 5
Joined: Thu Sep 02, 2010 2:28 am

Re: struct int problem

Post by acidice333 » Fri Sep 03, 2010 8:21 am

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.

WinterMute
Site Admin
Posts: 1859
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: struct int problem

Post by WinterMute » Fri Sep 03, 2010 8:56 pm

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.
Help keep devkitPro toolchains free, Donate today

Personal Blog

Post Reply

Who is online

Users browsing this forum: No registered users and 25 guests