Huge File read and send via socket
This is a multi-part message in MIME format.
------=_NextPart_000_000B_01C85DB7.F4C5EF30
Content-Type: text/plain;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi all,
I am new to c++ and socket programming, I have a situvation that my =
function have to a binary read ( whole file as 1 chunk ) and keep the =
buffer, and send the buffrer via socket by chunk by chunk as user =
specified. I have written a code sample, but I am not getting the =
correct out out.
can some one pls look in to the code and fix it ?
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 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;
}
//////////////////////////////
regards
Ganesh
------=_NextPart_000_000B_01C85DB7.F4C5EF30
Content-Type: text/html;
charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; =
charset=iso-8859-1">
<META content="MSHTML 6.00.2900.3132" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=Arial size=2>Hi all,</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>I am new to c++ and socket programming, =
I have a
situvation that my function have to a binary read ( whole file as 1 =
chunk ) and
keep the buffer, and send the buffrer via socket by chunk by chunk =
as user
specified. I have written a code sample, but I am not getting the =
correct out
out.</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>can some one pls look in to the code =
and fix it
?</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>FILE *fp23 = fopen( =
"c:\\hpfile.prn", "rb"
);</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>fseek (fp23 , 0 , =
SEEK_END);<BR>int
m_jobSize = ftell (fp23);<BR>rewind (fp23);</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>char* m_buffer = new
char[m_jobSize+1];</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2>int m_result = fread( =
m_buffer, 1,
m_jobSize, fp23 );</FONT></DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New" size=2></FONT> </DIV>
<DIV><FONT face="Courier New"
size=2>///////////////////////////////////////////////</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New" size=2> int BytesIndex = 0; =
</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New" size=2> while( bufsize >
0)<BR> {</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New" size=2> // Send some
bytes<BR> if ( bufsize < (unsigned long)iSendStatus
)<BR> {<BR> iSendStatus = send(*sock,
&m_buffer[BytesIndex], bufsize, 0); // Socket is of
type<BR> <BR> //<BR> /=
/ Call
Back Function<BR> //<BR> set +=
iSendStatus;<BR> ptr(jobid,set);<BR> </=
FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New" size=2> if ( =
iSendStatus !=
bufsize )<BR> {<BR> cpError =
SocketErrorHandler();<BR> return
cpError;<BR> }</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"
size=2> }<BR> else<BR> {<BR> &nbs=
p; iSendStatus
= send(*sock, &m_buffer[BytesIndex], chunkSize, 0); // Socket is =
of
type</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"
size=2> //<BR> // Call Back
Function<BR> //<BR> set +=
iSendStatus;<BR> ptr(jobid,set);</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV><FONT size=2>
<DIV><BR><FONT face="Courier New"> if ( iSendStatus =
!=
chunkSize )<BR> {<BR> cpError =
=
SocketErrorHandler();<BR> return
cpError;<BR> }<BR> <BR> }</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><FONT face="Courier New"> // Update buffer and
counter<BR> if ( bufsize < (unsigned long)iSendStatus
)<BR> {<BR> bufsize -=
bufsize;<BR> BytesIndex +=
bufsize;<BR> }<BR> else<BR> {<BR> &=
nbsp; bufsize
-= iSendStatus; <BR> BytesIndex +=iSendStatus;
<BR> }</FONT></DIV>
<DIV><FONT face="Courier New"></FONT> </DIV>
<DIV><BR><FONT
face="Courier =
New">//////////////////////////////<BR></FONT></FONT></DIV>
<DIV><FONT face=Arial size=2>regards</FONT></DIV>
<DIV><FONT face=Arial size=2>Ganesh</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV></BODY></HTML>
------=_NextPart_000_000B_01C85DB7.F4C5EF30--