What's the idiomatic way to do async socket programming in Delphi?

后端 未结 10 960
迷失自我
迷失自我 2021-01-04 19:18

What is the normal way people writing network code in Delphi use Windows-style overlapped asynchronous socket I/O?

Here\'s my prior research into this question:

10条回答
  •  暖寄归人
    2021-01-04 19:42

    With the ScktComp classes, you need to use a ThreadBlocking server rather than an a NonBlocking server type. Use the OnGetThread event to hand off the ClientSocket param to a new thread of your devising. Once you've instantiated an inherited instance of TServerClientThread you'll create a instance of TWinSocketStream (inside the thread) which you can use to read and write to the socket. This method gets you away from trying to process data in the event handler. These threads could exist for just the short period need to read or write, or hang on for the duration for the purpose of being reused.

    The subject of writing a socket server is fairly vast. There are many techniques and practices you could choose to implement. The method of reading and writing to the same socket with in the TServerClientThread is straight forward and fine for simple applications. If you need a model for high availability and high concurrency then you need to look into patterns like the Proactor pattern.

    Good luck!

提交回复
热议问题