krydea
asked on
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?fil esize
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,rec vfile);
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_s tr,(size_t )(found_st r - 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_s tr,(size_t )(found_st r - big_str));
words[count][(found_str - big_str)]=0;
big_str= found_str + 1;
count++;
found_str = strchr(big_str, '?');
}
strcpy(words[count],big_st r);
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(Sende r: TObject;
Socket: TCustomWinSocket);
procedure ClientSocket2Connect(Sende r: 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(S ender: TObject; Socket: TCustomWinSocket);
var s: String;
fs: TFileStream;
buf: array[0..3000] of Byte;
nbytes: Integer;
begin
s := ClientSocket2.Socket.Recei veText;
if (s = 'ok') and (state = wait_for_ok) then
begin
fs := TFileStream.Create(FILENAM E, fmOpenRead);
while fs.Position < fs.Size do
begin
nbytes := fs.Read(buf, Min(3000, fs.Size - fs.Position));
ClientSocket2.Socket.SendB uf(buf, nbytes);
end;
state := idle;
Memo1.lines.add('done'+Fil ename);
fs.Free;
end;
memo1.lines.add(S);
end;
procedure TForm1.Button1Click(Sender : TObject);
begin
if OpenDialog1.Execute then begin
FILENAME:= OpenDialog1.FileName;
ClientSocket2.Socket.SendT ext('uploa d=' + edit1.text + '?' + IntToStr(GetFileSize(FILEN AME)));
memo1.lines.add('upload=' + edit1.text + '?' + IntToStr(GetFileSize(FILEN AME)));
state := wait_for_ok;
end
end;
procedure TForm1.Button2Click(Sender : TObject);
begin
ClientSocket1.Active:=true ;
end;
procedure TForm1.ClientSocket1Connec t(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.lines.add('connect: 2418');
ClientSocket2.Active:=true ;
end;
procedure TForm1.ClientSocket2Connec t(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.lines.add('connect: 600');
end;
procedure TForm1.ClientSocket1Read(S ender: TObject;
Socket: TCustomWinSocket);
var s: String;
begin
s := ClientSocket2.Socket.Recei veText;
memo1.lines.add(S);
end;
end.
and this was it...
maybe can help to fix problem ..
plzzz help.
Carlos Smith
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?fil
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)
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_s
words[count][(found_str - big_str)]=0;
big_str= found_str + 1;
count++;
while (found_str = strchr(big_str, '?'))
{
strncpy(words[count],big_s
words[count][(found_str - big_str)]=0;
big_str= found_str + 1;
count++;
found_str = strchr(big_str, '?');
}
strcpy(words[count],big_st
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(Sende
Socket: TCustomWinSocket);
procedure ClientSocket2Connect(Sende
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(S
var s: String;
fs: TFileStream;
buf: array[0..3000] of Byte;
nbytes: Integer;
begin
s := ClientSocket2.Socket.Recei
if (s = 'ok') and (state = wait_for_ok) then
begin
fs := TFileStream.Create(FILENAM
while fs.Position < fs.Size do
begin
nbytes := fs.Read(buf, Min(3000, fs.Size - fs.Position));
ClientSocket2.Socket.SendB
end;
state := idle;
Memo1.lines.add('done'+Fil
fs.Free;
end;
memo1.lines.add(S);
end;
procedure TForm1.Button1Click(Sender
begin
if OpenDialog1.Execute then begin
FILENAME:= OpenDialog1.FileName;
ClientSocket2.Socket.SendT
memo1.lines.add('upload=' + edit1.text + '?' + IntToStr(GetFileSize(FILEN
state := wait_for_ok;
end
end;
procedure TForm1.Button2Click(Sender
begin
ClientSocket1.Active:=true
end;
procedure TForm1.ClientSocket1Connec
Socket: TCustomWinSocket);
begin
Memo1.lines.add('connect: 2418');
ClientSocket2.Active:=true
end;
procedure TForm1.ClientSocket2Connec
Socket: TCustomWinSocket);
begin
Memo1.lines.add('connect: 600');
end;
procedure TForm1.ClientSocket1Read(S
Socket: TCustomWinSocket);
var s: String;
begin
s := ClientSocket2.Socket.Recei
memo1.lines.add(S);
end;
end.
and this was it...
maybe can help to fix problem ..
plzzz help.
Carlos Smith
Would you mind to explain what the actual problem _is_?
ASKER
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?
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?
ASKER
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?
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?
When You oppenning a file try this:
recvfile = fopen(localfile,"a+b");
recvfile = fopen(localfile,"a+b");
ASKER
MadYugoslav: umm that does not work :(..
Which compiler You use to compile allsource taht you post.
ASKER
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.
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.
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;
}
}
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)
recvsize+=ret;
}
}
ASKER
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?
while( recvsize < size )
{
ret=recv(sock, szBuff, 4096, 0);
if( ret != 0&& ret != SOCKET_ERROR)
{
fwrite(szBuff,sizeof(char)
recvsize+=ret;
}
}
btW: how can i see the if the socket is clossed?
ASKER
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(c har),1024, rfile);
idx = 0;
while(nleft > 0)
{
ret = send(sock, &chunk[idx], nleft, 0);
nleft -= ret;
idx += ret;
}
}
fclose(rfile);
return 0;
}
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(c
idx = 0;
while(nleft > 0)
{
ret = send(sock, &chunk[idx], nleft, 0);
nleft -= ret;
idx += ret;
}
}
fclose(rfile);
return 0;
}
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.
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.
ASKER
i give it up can we try to make the sendfile in c/c++?
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.
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.
ASKER
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?
and the rest i don't know..
maybe we can try to make all the stuff in c/c++ and no more delphi!?!
k?
ASKER
or do i have to use:
SOCK_DGRAM
SOCK_DGRAM
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.
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.
ASKER
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
do you what that i give you the point's now? i trust you;)
and how long will you think it will take?
thanx
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.
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.
ASKER
my e_mail = krydea@hotmail.com
ASKER
btw: thanx for helping and will you mail you compiled project?
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++.
ASKER
it will be a serevr and a client?
and i use lcc32 only c lib's i think..
and i use lcc32 only c lib's i think..
Is it ok that it is in C++ compiled with Visual C as console application without MFC ?
ASKER
so only windows.h and stdio.h c libs?
Is it ok that it is in C++ compiled with Visual C as console application without MFC ?
And library ws2_32.lib
ASKER
yes.(winsock2.h and the ws2_32.lib)
Krydea please check mail on hotmail.
ASKER
i send it something back...
ASKER
i got your code but it does not compile some arror with CSocket class..
do you know what?
do you know what?
ASKER
i mailed you so check your mail..
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanx will we still have contact over mail?
i will send a email to you..
btw: thanx
krydea
i will send a email to you..
btw: thanx
krydea
ASKER
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
are you helping... plz respone......
Carlos Smith
ASKER
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..
soc.obj : error LNK2001: unresolved external symbol "int __cdecl _kbhit(void)" (?_kbhit@@YAHXZ)
plz help..