Solved

big problem plz help

Posted on 2001-06-12
36
250 Views
Last Modified: 2007-12-19
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
0
Comment
Question by:krydea
  • 21
  • 14
36 Comments
 
LVL 6

Expert Comment

by:snoegler
ID: 6183777
Would you mind to explain what the actual problem _is_?
0
 

Author Comment

by:krydea
ID: 6184594
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
 

Author Comment

by:krydea
ID: 6184600
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
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6184938
When You oppenning a file try this:
 recvfile = fopen(localfile,"a+b");
0
 

Author Comment

by:krydea
ID: 6185023
MadYugoslav: umm that does not work :(..
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6185076
Which compiler You use to compile allsource taht you post.
0
 

Author Comment

by:krydea
ID: 6185815
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
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6185894
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
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6185934
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
 

Author Comment

by:krydea
ID: 6188174
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
 

Author Comment

by:krydea
ID: 6188341
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
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6189685
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
 

Author Comment

by:krydea
ID: 6189920
i give it up can we try to make the sendfile in c/c++?
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6189987
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
 

Author Comment

by:krydea
ID: 6190732
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
 

Author Comment

by:krydea
ID: 6190747
or do i have to use:
SOCK_DGRAM
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6190829
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
 

Author Comment

by:krydea
ID: 6190878
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6190898
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
 

Author Comment

by:krydea
ID: 6190924
my e_mail = krydea@hotmail.com
0
 

Author Comment

by:krydea
ID: 6190929
btw: thanx for helping and will you mail you compiled project?
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6190957
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
 

Author Comment

by:krydea
ID: 6191126
it will be a serevr and a client?
and i use lcc32 only c lib's i think..
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6191152
Is it ok that it is in C++ compiled with Visual C as console application without MFC ?
0
 

Author Comment

by:krydea
ID: 6191179
so only windows.h and stdio.h c libs?
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6191185
Is it ok that it is in C++ compiled with Visual C as console application without MFC ?
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6191193
And library ws2_32.lib
0
 

Author Comment

by:krydea
ID: 6191304
yes.(winsock2.h and the ws2_32.lib)
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6194203
Krydea please check mail on hotmail.
0
 

Author Comment

by:krydea
ID: 6194299
i send it something back...
0
 

Author Comment

by:krydea
ID: 6211077
i got your code but it does not compile some arror with CSocket class..
do you know what?
0
 

Author Comment

by:krydea
ID: 6211104
i mailed you so check your mail..
0
 
LVL 2

Accepted Solution

by:
MadYugoslav earned 300 total points
ID: 6213149
I have no mail. Send it on previous mail address.
tmaruna@krt.neobee.net
0
 

Author Comment

by:krydea
ID: 6219381
thanx will we still have contact over mail?
i will send a email to you..
btw: thanx
krydea
0
 

Author Comment

by:krydea
ID: 6232590
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
 

Author Comment

by:krydea
ID: 6235206
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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now