Transfer binary files using SDLNet_TCP_Send()

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

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

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 <@Eduardo_Fernandes> 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

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
// chars 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 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 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

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
// chars 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 <@Eduardo_Fernandes> 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 <@Eduardo_Fernandes> 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

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

Your English is quite good, actually.

You’ll want to make sure that SDLNet_TCP_Send actually writes all the
bytes (and that the process that’s reading them actually reads them
all). While you should also check for failures from file i/o, writing to
a file usually works, but a network connection could drop at any time,
and failures usually need to be managed on two computers instead of one.

You’ll also need some way to signal the remote side that there aren’t
more bytes coming, so when it has the whole file, it’s not waiting for
more data to arrive.

–ryan.