big problem plz help

hi all i have a big problem..
i'm trying to make a upload from a delphi client to a c server(server made in winsock)
it's like
client>upload=filename?filesize
server>ok
client>file

socket is made with AF_INET, SOCK_STREAM, IPPROTO_IP on port 600 here is the recv file function:

int recvfile(char localfile[256], char *StrSize, SOCKET sock)
{
FILE *recvfile;
long recvsize;
int ret,
      nLeft;
char   szBuff[4096];
long size;

  size = atol(StrSize);
  recvsize=0;
  recvfile = fopen(localfile,"wb");

  SendString("ok",sock);

  while ( size-1 > recvsize )
       {
            ret = recv(sock, szBuff, 4096, 0);
            if (ret > 0)
            {
              fwrite(szBuff,sizeof(char),ret+1,recvfile);
              recvsize = recvsize + ret+1;
                  }
         }
      SendString("Done",sock);
  fclose(recvfile);
return 0;
}
and the SendString function:
int SendString(char szBuff[4096],SOCKET sock)
{
      if (szBuff!=NULL)
      {
            int nLeft,
                    idx,
                    ret;
            nLeft = strlen(szBuff);
          idx = 0;

           while(nLeft > 0)
          {
              ret = send(sock, &szBuff[idx], nLeft, 0);
              nLeft -= ret;
              idx += ret;
           }
      }
return 0;
}

the string is kut with:
int getcommand(char *big_str,char words[7][4096])
{
   char *found_str;
   int count=0;

   if(found_str = strchr(big_str, '='))
   {
       strncpy(words[count],big_str,(size_t)(found_str - big_str));
       words[count][(found_str - big_str)]=0;
       big_str= found_str + 1;
       count++;

       while (found_str = strchr(big_str, '?'))
       {
           strncpy(words[count],big_str,(size_t)(found_str - big_str));
           words[count][(found_str - big_str)]=0;
           big_str= found_str + 1;
           count++;
           found_str = strchr(big_str, '?');
       }
       strcpy(words[count],big_str);
       count++;
   }

return count;
}
that's how i get filesize and stuff.
and here the client now:

unit upload;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,Math,
  StdCtrls, ScktComp;

type
  TForm1 = class(TForm)
    ClientSocket1: TClientSocket;
    ClientSocket2: TClientSocket;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    Button2: TButton;
    Memo1: TMemo;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure ClientSocket2Read(Sender: TObject; Socket: TCustomWinSocket);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure ClientSocket1Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocket2Connect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

    type TState = (idle, wait_for_ok);
var
  Form1: TForm1;
  state: TState;
  Filename : String;
implementation

{$R *.DFM}

function GetFileSize(filename: String): Integer;
var f: file of Byte;
begin
 AssignFile(f, filename);
 Reset(f);
 Result := FileSize(f);
 CloseFile(f);
end;

// OnRead event code
procedure TForm1.ClientSocket2Read(Sender: TObject; Socket: TCustomWinSocket);
var s: String;
   fs: TFileStream;
   buf: array[0..3000] of Byte;
   nbytes: Integer;
begin
 s := ClientSocket2.Socket.ReceiveText;
 if (s = 'ok') and (state = wait_for_ok) then
 begin
   fs := TFileStream.Create(FILENAME, fmOpenRead);
   while fs.Position < fs.Size do
   begin
     nbytes := fs.Read(buf, Min(3000, fs.Size - fs.Position));
     ClientSocket2.Socket.SendBuf(buf, nbytes);
   end;
   state := idle;
   Memo1.lines.add('done'+Filename);
   fs.Free;
 end;
 memo1.lines.add(S);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then begin
      FILENAME:= OpenDialog1.FileName;
      ClientSocket2.Socket.SendText('upload=' + edit1.text + '?' + IntToStr(GetFileSize(FILENAME)));
      memo1.lines.add('upload=' + edit1.text + '?' + IntToStr(GetFileSize(FILENAME)));
      state := wait_for_ok;
  end
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
   ClientSocket1.Active:=true;
end;

procedure TForm1.ClientSocket1Connect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  Memo1.lines.add('connect: 2418');
  ClientSocket2.Active:=true;
end;

procedure TForm1.ClientSocket2Connect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  Memo1.lines.add('connect: 600');
end;

procedure TForm1.ClientSocket1Read(Sender: TObject;
  Socket: TCustomWinSocket);
  var s: String;
begin
    s := ClientSocket2.Socket.ReceiveText;
    memo1.lines.add(S);
end;

end.

and this was it...
maybe can help to fix problem ..
plzzz help.

Carlos Smith
krydeaAsked:
Who is Participating?
 
MadYugoslavConnect With a Mentor Commented:
I have no mail. Send it on previous mail address.
tmaruna@krt.neobee.net
0
 
snoeglerCommented:
Would you mind to explain what the actual problem _is_?
0
 
krydeaAuthor Commented:
yes sorry,
if the file is smaller the the buffer it goes file but if the client have to send 2 or6 or 7 the a buffer the file will be to small and not close so there have to be some were a mistake..
maybe you know what it is?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
krydeaAuthor Commented:
yes sorry,
if the file is smaller then the buffer it goes fine but if the client have to send 7 or 10 or 20 time a buffer the file will be to small and not close so there have to be some were a mistake..
maybe you know what it is?
0
 
MadYugoslavCommented:
When You oppenning a file try this:
 recvfile = fopen(localfile,"a+b");
0
 
krydeaAuthor Commented:
MadYugoslav: umm that does not work :(..
0
 
MadYugoslavCommented:
Which compiler You use to compile allsource taht you post.
0
 
krydeaAuthor Commented:
i can compile it but the problem is still there i use lcc to compile the c/c++ and borland delphi to compile the delphi.
0
 
MadYugoslavCommented:
That part of code (in C++) which receiving data seems alright. I post comment to avoid multiple calls of that function with same filename. If You make sure that function recvfile() is called only once for one file, and that there are no return from there before finishing file that part is ok. I don't write in Delphi at all and not familiar with.
0
 
MadYugoslavCommented:
Try to replace your while with this one.
If it isn't ok describe why You use ret+1.

while( recvsize < size )
{
   ret=recv(sock, szBuff, 4096, 0);
   if( ret == 0 )
   {
      ; // socket closed
      ; // probably want return from here
   }
   else if( ret == SOCKET_ERROR )
   {
      ; // socket error
      ; // probably want return from here
   }
   else // recv OK
   {
      fwrite(szBuff,sizeof(char), ret, recvfile);
      recvsize+=ret;
   }
}
0
 
krydeaAuthor Commented:
so
while( recvsize < size )
{
  ret=recv(sock, szBuff, 4096, 0);
  if( ret != 0&& ret != SOCKET_ERROR)
  {
     fwrite(szBuff,sizeof(char), ret, recvfile);
     recvsize+=ret;
 
  }
}


btW: how can i see the if the socket is clossed?
0
 
krydeaAuthor Commented:
it will not get enouth byt's..
i give upl i think.
can you maybe make a send file? a small client? plz i made something now.
and i don't know if it work's but plz help..

int sendfile(char localfile[256],SOCKET sock)
{
FILE *rfile=fopen(localfile,"rb");
unsigned char* chunk = (unsigned char*) malloc(1);
char *furst=0,*furstsend=0;
int idx = 0,
    ret;
double nleft;
long size;
FILE *f;

f = fopen(localfile, "rb");
fseek(f, 0, SEEK_END);
size = ftell(f);
fclose(f);

             strcpy(furstsend,"upload=");
             sprintf(furst,"%ld ",size);
             strcpy(furstsend,localfile);
             strcpy(furstsend,"?");
             strcpy(furstsend,furst);

        nleft = strlen(furstsend);

     while(nleft > 0 )/*mak sure i send all the bit's*/
              {
                  ret = send(sock, &furstsend[idx], nleft, 0);
                  nleft -= ret;
                  idx += ret;
               }

   idx = 0;

   while (!feof(rfile))
   {

              nleft=fread(chunk,sizeof(char),1024,rfile);

             idx = 0;

             while(nleft > 0)
           {
               ret = send(sock, &chunk[idx], nleft, 0);
               nleft -= ret;
               idx += ret;
           }

   }
fclose(rfile);
return 0;
}
0
 
MadYugoslavCommented:
Function recv return values are:
if recv return value is 0 socket is closed,
if recv return value is SOCKET_ERROR there are some error in receiving,
if recv return value is greater than 0 data are received correct and You need to continue receiving.
0
 
krydeaAuthor Commented:
i give it up can we try to make the sendfile in c/c++?
0
 
MadYugoslavCommented:
Problem is still here.
If You want to use that kind of aproach You need to use blocking sockets (do You ?).
That kind of aproach meen that You don't use events.
0
 
krydeaAuthor Commented:
i'm makeing the sockets with AF_INET, SOCK_STREAM, IPPROTO_IP .
and the rest i don't know..
maybe we can try to make all the stuff in c/c++ and no more delphi!?!
k?  
0
 
krydeaAuthor Commented:
or do i have to use:
SOCK_DGRAM
0
 
MadYugoslavCommented:
This is ok.
Try to rewrite code for send in c/c++. Less or more it is same with recvfile with use if send instead if receive.
I can try that. But this isn't small thing and in the mean time someone else can take points and make my time and code useless.
0
 
krydeaAuthor Commented:
ok
do you what that i give you the point's now? i trust you;)
and how long will you think it will take?
thanx
0
 
MadYugoslavCommented:
Let say this.
I'll try to make this in two days (Friday afternoon is dead time). If I'll make that I'll post to You code (post Your mail) or I'll post comment with no answer. Is it OK.
But You will wait until that with points.
0
 
krydeaAuthor Commented:
my e_mail = krydea@hotmail.com
0
 
krydeaAuthor Commented:
btw: thanx for helping and will you mail you compiled project?
0
 
MadYugoslavCommented:
I'll mail to you exe file. If it is ok with You and you give me points I'll send to You source in C++.
0
 
krydeaAuthor Commented:
it will be a serevr and a client?
and i use lcc32 only c lib's i think..
0
 
MadYugoslavCommented:
Is it ok that it is in C++ compiled with Visual C as console application without MFC ?
0
 
krydeaAuthor Commented:
so only windows.h and stdio.h c libs?
0
 
MadYugoslavCommented:
Is it ok that it is in C++ compiled with Visual C as console application without MFC ?
0
 
MadYugoslavCommented:
And library ws2_32.lib
0
 
krydeaAuthor Commented:
yes.(winsock2.h and the ws2_32.lib)
0
 
MadYugoslavCommented:
Krydea please check mail on hotmail.
0
 
krydeaAuthor Commented:
i send it something back...
0
 
krydeaAuthor Commented:
i got your code but it does not compile some arror with CSocket class..
do you know what?
0
 
krydeaAuthor Commented:
i mailed you so check your mail..
0
 
krydeaAuthor Commented:
thanx will we still have contact over mail?
i will send a email to you..
btw: thanx
krydea
0
 
krydeaAuthor Commented:
hey i got MVC6++ :) so you cn hold it in C++ if you like.... cn you plz translate it to win9x sockets......

are you helping... plz respone......


Carlos Smith
0
 
krydeaAuthor Commented:
i i got the last link errors away but how do i fix this one?

soc.obj : error LNK2001: unresolved external symbol "int __cdecl _kbhit(void)" (?_kbhit@@YAHXZ)

plz help..
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.