Jump to content

Winsock file transfer

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
n1mda

n1mda

    Newbie

  • Members
  • Pip
  • 1 posts
Hi!

I'm working on a simple project - I want to transfer binary files using winsock. But I've reached a major problem.

I've studied this and googled for a couple of hours, but I do NOT understand what the problem is.

int recvDll(SOCKET s)

{

    int file_size;

    char fileLength[50] = "";

    FILE *fp;

    int bytes_recieved, bytes_sent, bytes_written;

    

    int recs = recv(s, fileLength, 32, 0);

    fileLength[recs] = '\0';

    file_size = atoi(fileLength);

    

    fp = fopen("testrecv.dll", "wb");

    

    printf("[+] Size of recieved DLL: %d\n", file_size);


    while(file_size > 0)

    {

                    printf("filesize: %d\n", file_size);

                    char buf[512];

                    if(file_size >= 512)

                    {

                           bytes_recieved = recv(s, buf, 512, 0);

                    } else {

                           printf("x: %d\n", file_size);

                           bytes_recieved = recv(s, buf, file_size, 0);

                           buf[file_size] = '\0';

                           

                    }

                    fwrite(buf, bytes_recieved, 1, fp);

                    file_size -= bytes_recieved;

    }


    fclose(fp);

    return 0;

}

The server that sends the file is written i VB, and splits up the file á 512 byte. But the transfer seems to be discontinued right in the middle of the download.

This is a typical output:
[+] Size of recieved DLL: 122861

filesize: 122861

filesize: 122349

filesize: 121863

filesize: 121351

filesize: 120839

filesize: 120327

filesize: 120324

filesize: 119812

filesize: 119300

filesize: 118864

filesize: 118785

filesize: 118273

filesize: 117761

filesize: 117249

filesize: 117246

filesize: 116734

filesize: 116222

filesize: 115786

filesize: 115707

filesize: 115195

filesize: 114683

filesize: 114171

filesize: 114168

filesize: 113656

filesize: 113144

filesize: 112632

filesize: 112629

filesize: 112117

filesize: 111605

filesize: 111093

filesize: 111090

filesize: 110579

I would appreciate any help, since this has been bugging me for a long time now.

#2
julmuri

julmuri

    Programmer

  • Members
  • PipPipPipPip
  • 139 posts
Heres how i would propably write it.


int recvDll( SOCKET socket )

{

	int		bytes;

	int		index;

	int		length;

	char	buffer[ 0x0200 ];

	FILE*	pFile;


	memset( buffer, 0, 0x0200 );


	bytes = 0;

	bytes = ::recv( socket, buffer, 0x20, 0 );

	if ( 0 == bytes )

	{

		// closed gracefully

		// ...

		return 0;

	} // if

	if ( SOCKET_ERROR == bytes )

	{

		// socket error

		// ...

		return 0;

	} // if


	buffer[ bytes ] = '\0';


	length = 0;

	length = ::atoi( buffer );

	if ( 0 == length )

	{

		// atoi failed to convert the size

		// ...

		return false;

	} // if


	// receiving length this way is kinda sloppy imo

	// if you there isnt always 32bytes reserved for

	// the length string. better way would be to just

	// write 4byte integer in beginning of the packet

	// and memcpy / cast that.


	memset( buffer, 0, 0x0200 );


	pFile = 0;

	pFile = ::fopen( "testrecv.dll", "wb" );

	if ( 0 == pFile )

	{

		// failed to open the file

		// ...

		return 0;

	} // if


	for ( index = 0; index <= length; )

	{

		bytes = 0;

		bytes = ::recv( socket, buffer, 0x0200, 0 );

		if ( 0 == bytes )

		{

			// closed gracefully

			// ...

			::fclose( pFile );

			return 0;

		} // if

		if ( SOCKET_ERROR == bytes )

		{

			// socket error

			// ...

		

			::fclose( pFile );

			return 0;

		} // if


		::fwrite( buffer, 1, bytes, pFile );

		index += bytes;

	} // for


	if ( index > length )

	{

		// deal with trailing bytes

		// ie. fill the extra written

		// data with zeroes.

		// ...

	} // if


	::fclose( pFile );

	return 0;

}


Didnt notice any terrible mistakes with your code, cept receiving the file size seemed kinda sloppy.
Anyways test that code if it works. :p