Home > Back-end >  Do you have any demo TServerSocket and TClientSocket transport large files?
Do you have any demo TServerSocket and TClientSocket transport large files?

Time:10-26

Dear predecessors have TServerSocket and TClientSocket demo of transferring large files?
The c + + builder, whether can you send a reference?

email:[email protected]

CodePudding user response:

According to the example of char under BCB6 change easily,

CodePudding user response:

Which is char, I want to know about the file transfer is, how to read, write, or everybody said after practice that one way is better

CodePudding user response:

I refer to some examples to write the character of a transfer, but if pass. Dat this kind of file?
//client
Void __fastcall TClientFrm: : Button2Click (TObject * Sender)
{
Try
{
If (Edit1 - & gt; Text=="") return;
ClientSocket1 - & gt; The Socket - & gt; SendText (Edit1 - & gt; The Text);
Memo1 - & gt; Lines - & gt; Add (ClientSocket1 - & gt; The Socket - & gt; LocalAddress + ": :" + Edit1 - & gt; The Text);
Edit1 - & gt; Text="";
}
The catch (... )
{
Memo1 - & gt; Lines - & gt; Add (" send failed!" );
ClientSocket1 - & gt; Close ();
For the - & gt; Enabled=true;
Button2 - & gt; Enabled=false;
Button3 - & gt; Enabled=false;
}
}

//server
Void __fastcall MyServer: : DataProcess (void)//all data processing
{
Char GetStr;
String RevStr;
Char buf [1024 + 1];

Try {
While (ClientSocket - & gt; Connected==true)
{
Memset (buf, 0, sizeof (buf));
If (ReceiveStr (buf, 1024) & gt; 0)
{
RevStr=String (buf);
Message=RemoteHostIP + ": :" + RevStr;
The Synchronize ((TThreadMethod) & amp; AddMessage);
TransmitStr=RemoteHostIP + ": :" + RevStr;
}
RevStr="";
//the following data forwarding
If (TransmitStr!="")
{
BMSG=TransmitStr;
The Synchronize ((TThreadMethod) & amp; BC);
TransmitStr="";
}
}
}
The catch (... )
{
return;
}
}
//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Int __fastcall MyServer: : ReceiveStr (char * _Buf, int length)//receiving data
{
If (length<=0) return 0;
Int aLen=0;
Int pos=0;
Char aBuf [255].

Int smalllen=0;

If (length<=smalllen)
Smalllen=length;
The else
Smalllen=smalllen;

PStream TWinSocketStream *;
DWORD beginTick;

BeginTick=GetTickCount ();
while(! ClientSocket - & gt; Connected & amp; & The GetTickCount () - beginTick<200);
Try {
PStream=new TWinSocketStream (ClientSocket, 1000);
Try {
If (pStream - & gt; WaitForData (TimeOutMSec)) {
While (pStream - & gt; WaitForData (100))
{
Application - & gt; ProcessMessages ();
If ((aLen=pStream - & gt; Read (aBuf smalllen))==0)
{
ClientSocket - & gt; Close ();
break;
}
The else {
ABuf (aLen)=0;
Memcpy (_Buf + pos, aBuf, aLen);
Pos +=aLen.
If (pos>=length)
break;
If (length - posSmalllen=length - pos.
}
}
}
The else ClientSocket - & gt; Close ();
}
__finally {
The delete pStream;
}
}
The catch (Exception & amp; E) {
Application - & gt; HandleException (this);
}
Return pos.
}

CodePudding user response:

File is divided into a number of major sources of the client size and forward through the server to its target client. Know completion of the forward.

CodePudding user response:

TServerSocket and TClientSocket not very good with two controls, suggest SocketAPI
  • Related