The technique of using Socketstream to read and write data in Delphi

Source: Internet
Author: User

Procedure Tserverthread.clientexecute;
Var
Pstream:twinsocketstream;
Buffer:pointer;
readtext,sendtext:string;
I:integer;
Const
{The buffer size used when reading client tokens because they are some strings, so the definition is 1024byte enough}
readlen=1024;
Begin

{Create a connection flow object to communicate with the client}
Pstream:=twinsocketstream.create (clientsocket,60000);
Try
{Clientsocket is an object built into the Tserverclientthread class, which is a socket connected to the client}
while (not Terminated) and clientsocket.connected do
Begin
Try
{Allocate read data buffer}
Buffer:=allocmem (Readlen);
If Pstream.waitfordata (6000) Then
Begin
Pstream.read (Buffer^,readlen); The read client sends data, which is read from the cache. Readtext:=pchar (Buffer);
Freemem (buffer);
Client requests to verify that there are files that can be updated
If ReadText = key_clt[1] Then
Begin
Synchronize (Listitemadd);//The method that synchronizes information in multiple threads needs to be called.
Sendtext:=key_srv[1]+stringstostring (filesnamesepstr,filesname,true);
{Special note sendtext should be added after index 1, specifying the Write method from SendText first character
Start reading, otherwise default starting from 0}
Pstream.write (Sendtext[1],length (SendText) +1);//write data to the client
End

Except

Errorraise:=true;
Terminate;

End

End
Finally
Pstream. Free;
Clientsocket.close;
End
End

Since the socket chooses to use ctblock, it cannot be relied on to read and write data in Onread,onwrite when it receives and sends data, because the event cannot be triggered at all under Ctblock.
The solution is to continuously monitor the thread

while (not Terminated) and clientsocket.connected do
Begin

End

Time to read data: if Pstream.waitfordata (6000) Then
Begin
End

Transferred from: http://gykthh.blog.163.com/blog/static/6453464200852710710790/

--------------------------------------------------------------------------------------------------------------- --------------


{Tfileserverthread} Tfileserverthread = Class (Tserverclientthread)
Public
Procedure Clientexecute; Override
End {Tfileserverthread}procedure tfileserverthread.clientexecute;
Var
DATA:ARRAY[0..1023] of Char;
rectext:string;
Socketstream:twinsocketstream;
Begin while not Terminated and clientsocket.connected do
Try
Socketstream: = Twinsocketstream.create (Clientsocket, 30000);
Try
Fillchar (data, SizeOf (data), 0);
If socketstream.read (data, SizeOf (data)) = 0 Then
Begin
If we didn ' t get any data after xx seconds then close the connection
Clientsocket.sendtext (' Timeout on Server ' + #13 # #);
Wait a little time to allow sending of text before disconnect
Sleep (1);
Clientsocket.close;
Terminate;
End;
Rectext: = Data;
If Length (Rectext) > 2 Then
Delete (Rectext, Pos (#13 # #, Rectext), 2); Delete #13 # #
If clientsocket.connected Then
Begin
Clientsocket.sendtext (Rectext);
SendMessage (Form1.Listbox1.Handle, lb_addstring, 0, Integer (PChar (Rectext)));
PostMessage (Form1.handle, cm_inccount, 0, 0);
End;
Finally
Socketstream.free;
End;
Except
HandleException;
End;
End Procedure Tform1.serversocketgetthread (Sender:tobject;
Clientsocket:tserverclientwinsocket;
var socketthread:tserverclientthread);
Begin
Socketthread: = Tfileserverthread.create (False, Clientsocket);
End

--------------------------------------------------------------------------------------------------------------- --------------

Delphi's help has Writing client threads and Writing server threads code examples.
Procedure Tmyclientthread.execute;
Var
Thestream:twinsocketstream;
buffer:string;
Begin
{Create a twinsocketstream for reading and writing}
Thestream: = Twinsocketstream.create (Clientsocket1.socket, 60000);
Try
{Fetch and process commands until the connection or thread is terminated}
while (not Terminated) and (clientsocket1.active) does
Begin
Try
Getnextrequest (buffer); {Getnextrequest must be a Thread-safe method}

{Write the request to the server}
Thestream.write (buffer, Length (buffer) + 1);
{Continue the communication (e.g. read a response from the server)}
...
Except
If not (Exceptobject was eabort) then
Synchronize (handlethreadexception); {You must write Handlethreadexception}
End
End
Finally
Thestream.free;
End
End

Procedure Tmyserverthread.clientexecute;
Var
Stream:twinsocketstream;
Buffer:array[0:9] of Char;
Begin
{Make sure connection is active}
while (not Terminated) and clientsocket.connected do
Begin
Try
Stream: = Twinsocketstream.create (Clientsocket, 60000);
Try
Fillchar (Buffer, 10, 0); {Initialize the buffer}
{Give the client seconds to start writing}
If Stream.waitfordata (60000) Then

Begin
If Stream.read (Buffer, ten) = 0 Then {if can ' t Read in seconds}
Clientsocket.close; {Close the connection}
{Now process the request}
...
End
Else
Clientsocket.close; {If client doesn ' t start, close}
Finally
Stream.free;
End
Except
HandleException;
End
End
End

The technique of using Socketstream to read and write data in Delphi

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.