[SDL] Transfer binary files using SDLNet_TCP_Send()

Eduardo Fernandes ceduardodfernandes at gmail.com
Fri Mar 23 10:07:36 PDT 2007


Hi,

I´ve tested this way,

buffer = (char*)malloc(len*sizeof(char));

with malloc, but the system was allocating a wrong space of memory.


And when we try to do this:

fread(buffer,len,1,file);

in a binary file, I don´t know why, but it don´t read all the file, and
that´s because I´m doing this way:

for(i=0; i<len; i++)
{
   fread(&c,sizeof(char),1,file);
   memcpy((buffer + i),&c,sizeof(char);
}

That´s the only way it looks to works...

PS: Forgive me if my english is bad, ...I´m brazillian and don´t use to
speak (or write) in english everyday.

Eduardo Fernandes.



2007/3/23, Brian <brian.ripoff at gmail.com>:
>
> Bear in mind this is not tested:
>
> int len;
> char* buffer;
> FILE* file = fopen("file.dat", "rb");
> if( !file )
> {
>     // do something
> }
>
> fseek(file,0,SEEK_END);
> len = ftell(file);
> rewind(file);
>
> // you would only need sizeof(char) here too, on a 32bit platform
> // char*s are be 4 ties the size of chars
> // but why use calloc, we will be overwriting it all ourselves anyway
> // I *think* that cast is unnecessary in C, but im not sure
> // sizeof(char) is defined as being 1 in c++
> // but Im not so sure about c, so Ill leave it in
> buffer = (char*)malloc(len*sizeof(char));
> if(!buffer)
> {
>    // do something
>   // and fclose(file)
> }
>
> // alternatively you could read it in manageable chunks
> // this really depends on the file you are sending, if its never
> // going to be big this should suffice
> fread(buffer,len,1,file);
>
> fclose(file);
>
> // strlen(buffer)? That's dangerous
> // if the binary file contained a byte that equaled '\0' you wouldn't
> // send the whole file, only up to that point
> SDLNet_TCP_Send(sock,buffer,len);
>
> free(buffer);
>
> On 3/23/07, Eduardo Fernandes <ceduardodfernandes at gmail.com> wrote:
> > Thanks,
> >
> > I´ve already did it, and I was sucessfull, I done it this way:
> >
> > int i;
> > int len;
> > char c;
> > char* buffer;
> > FILE* file = fopen("file.dat", "rb");
> >
> > fseek(file,0,SEEK_END);
> > len = ftell(file);
> > rewind(file);
> >
> > buffer = (char*)calloc(len,sizeof(char*));
> >
> > for(i=0; i<len; i++)
> > {
> >    fread(&c,sizeof(char),1,file);
> >    memcpy((buffer + i),&c,sizeof(char);
> > }
> >
> > fclose(file);
> >
> > SDLNet_TCP_Send(sock,buffer,(int)strlen(buffer));
> >
> > free(buffer);
> >
> > What do you think about it?
> >
> > If somebody knows a better way, please send us.
> >
> > Thanks again.
> >
> > Eduardo Fernandes.
> >
> >
> > 2007/3/23, Olivier Delannoy <olivier.delannoy at gmail.com>:
> > > You load the file by chunk in a  memory buffer and send loaded chunk
> using
> > SDLNet_TCP_Send(). Send work like write on file. Basically it's exactly
> the
> > same as copying a file.
> > >
> > >
> > >
> > > On 3/23/07, Eduardo Fernandes <ceduardodfernandes at gmail.com> wrote:
> > > >
> > > > Hi,
> > > >
> > > > I would like to know how you to transfer binary files using
> > SDLNet_TCP_Send().
> > > >
> > > > I´m a C programmer working at a software house developing a game in
> SDL.
> > > >
> > > > thanks
> > > >
> > > > Eduardo Fernandes
> > > >
> > > > _______________________________________________
> > > > SDL mailing list
> > > > SDL at lists.libsdl.org
> > > > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
> > > >
> > > >
> > >
> > >
> > >
> > > --
> > > Olivier Delannoy
> > > ATER
> > > PRiSM Laboratory
> > > Versailles University, FRANCE
> > > _______________________________________________
> > > SDL mailing list
> > > SDL at lists.libsdl.org
> > > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
> > >
> > >
> >
> >
> > _______________________________________________
> > SDL mailing list
> > SDL at lists.libsdl.org
> > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
> >
> >
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20070323/fbbd68f6/attachment.htm 


More information about the SDL mailing list