By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

Read a Huge file and send via SOCKET

P: 3
Hi All,

I have a fn prototype as follows

void print ( char *Buffer, usigned long bufferSize, int chunkSize );

This fn will accept binary file buffer and the buffer size and the chunk
size, implementation must be open socket and send the buffer in chunks which
is provided by the parameter.

My probelm is if I send a txt file the server receives it correctly, but
when I send a binary data(exe,doc,pdf), the socket send() returns
succussfully sent the no of bytes, but I cannot open the file in server
side.

I think in my code, breaking in chunks makes this probelm. Can u some one
help me fix this probelm.

But all must be in ANSI C,

Regards,
Ganesh

FILE *fp23 = fopen( "c:\\hpfile.prn", "rb" );

fseek (fp23 , 0 , SEEK_END);
int m_jobSize = ftell (fp23);
rewind (fp23);

char* m_buffer = new char[m_jobSize+1];

int m_result = fread( m_buffer, 1, m_jobSize, fp23 );

int chunkSize = 15;

void print ( m_buffer, m_jobSize, chunkSize)

///////////////////////////


void print ( char *m_buffer, usigned long m_jobSize, int chunkSize)
{

//
// Socket open implementation is done here
//

int BytesIndex = 0;

while( bufsize > 0)
{

// Send some bytes
if ( bufsize < (unsigned long)iSendStatus )
{
iSendStatus = send(*sock, &m_buffer[BytesIndex], bufsize, 0); // Socket
is of type

//
// Call Back Function
//
set += iSendStatus;
ptr(jobid,set);


if ( iSendStatus != bufsize )
{
cpError = SocketErrorHandler();
return cpError;
}

}
else
{
iSendStatus = send(*sock, &m_buffer[BytesIndex], chunkSize, 0); // Socket
is of type

//
// Call Back Function
//
set += iSendStatus;
ptr(jobid,set);


if ( iSendStatus != chunkSize )
{
cpError = SocketErrorHandler();
return cpError;
}

}

// Update buffer and counter
if ( bufsize < (unsigned long)iSendStatus )
{
bufsize -= bufsize;
BytesIndex += bufsize;
}
else
{
bufsize -= iSendStatus;
BytesIndex +=iSendStatus;
}
}
Feb 5 '08 #1
Share this Question
Share on Google+
4 Replies


P: 6
how about the one that receives the file? what is that program?
Feb 5 '08 #2

gpraghuram
Expert 100+
P: 1,275
R u opening the file on the receive end also in binary mode?

Raghuram
Feb 5 '08 #3

P: 3
Yes, the server side also reading it in binary format. Actually I am doing a Class for LPR printing, the client will send PCL formated binary file to a Network printer. So the client cannot read chunk by chunk, it have to read the file whole in one shot and send it chunk by chunk.

Ganesh


R u opening the file on the receive end also in binary mode?

Raghuram
Feb 5 '08 #4

P: 1
Hi,
Can you post the whole code... I also stuck with the same problem....

Thanks in advance...

Suresh


Hi All,

I have a fn prototype as follows

void print ( char *Buffer, usigned long bufferSize, int chunkSize );

This fn will accept binary file buffer and the buffer size and the chunk
size, implementation must be open socket and send the buffer in chunks which
is provided by the parameter.

My probelm is if I send a txt file the server receives it correctly, but
when I send a binary data(exe,doc,pdf), the socket send() returns
succussfully sent the no of bytes, but I cannot open the file in server
side.

I think in my code, breaking in chunks makes this probelm. Can u some one
help me fix this probelm.

But all must be in ANSI C,

Regards,
Ganesh

FILE *fp23 = fopen( "c:\\hpfile.prn", "rb" );

fseek (fp23 , 0 , SEEK_END);
int m_jobSize = ftell (fp23);
rewind (fp23);

char* m_buffer = new char[m_jobSize+1];

int m_result = fread( m_buffer, 1, m_jobSize, fp23 );

int chunkSize = 15;

void print ( m_buffer, m_jobSize, chunkSize)

///////////////////////////


void print ( char *m_buffer, usigned long m_jobSize, int chunkSize)
{

//
// Socket open implementation is done here
//

int BytesIndex = 0;

while( bufsize > 0)
{

// Send some bytes
if ( bufsize < (unsigned long)iSendStatus )
{
iSendStatus = send(*sock, &m_buffer[BytesIndex], bufsize, 0); // Socket
is of type

//
// Call Back Function
//
set += iSendStatus;
ptr(jobid,set);


if ( iSendStatus != bufsize )
{
cpError = SocketErrorHandler();
return cpError;
}

}
else
{
iSendStatus = send(*sock, &m_buffer[BytesIndex], chunkSize, 0); // Socket
is of type

//
// Call Back Function
//
set += iSendStatus;
ptr(jobid,set);


if ( iSendStatus != chunkSize )
{
cpError = SocketErrorHandler();
return cpError;
}

}

// Update buffer and counter
if ( bufsize < (unsigned long)iSendStatus )
{
bufsize -= bufsize;
BytesIndex += bufsize;
}
else
{
bufsize -= iSendStatus;
BytesIndex +=iSendStatus;
}
}
Mar 14 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.