[转] Delphi Socket Architecture
1 - Socket Programming in DelphiSockets were first introduced under Unix. Windows later, much later, followed. Since Windows has no easy multitasking library, a consortium defined a WindowsSocket specification which could use the Windows messages for socket scheduling. Third parties offered libraries (Trumpet was one of those). Later Windowsincluded its own WinSock version. And later Windows offered threading, which allows blocking sockets without message notifications. Therefore it is a small wonder that Delphi had to change its VCL components several times to allow networking. To make a long story short:
This paper will present the client and server socket component organization, as well as a small file exchange application. 2 - The ScktComp unit2.1 - Required ClassesCommunication between a Server and a Client needs:
To review what is really needed, have a look at the Socket Programming article. In any case, a component library will naturally include:
However there are several aspects:
2.2 - The ScktComp UnitThe tClientSocket and tServerSocket have been place in a single 90 K unit, which contains 12 classes. Splitting those into 6 units helped us better understand the relationships between those classes. Basically there are two layers:
2.3 - The WinSock encapsulation
To add threading capabilities, the following CLASSES (17 K) have been added:
As usual, threading add its share of complexity: there is a thread cache, one has to wait for thread termination etc. And to read and write socket as streams, the following tStream descendent is included:
In addition, when we use threads, there are some access protection to take care of:
2.4 - The User classesThe user is mainly concerned with
For this purpose, we have the following CLASSes:
2.5 - The UML class diagramBase on the previous analysis, we can draw the following diagram:
Note that
2.6 - ScktComp usageThe ScktComp is used in the following VCL units:
There is a Chat demo application in the DEMO directory of Delphi. This is a very elegant sample, since all the people taking part in a chat use the same application. The user either start with "Listen" or "Connect" and the tForm1 contains an IsServer Boolean which tells whether the application should use the tServerSocket or thetClientSocket. And all is contained in 7 K. I would be reluctant though to use this application as a Socket programming first example, since most Socket application are disymmetric by nature: there is a Server application handling server tasks, and a separate Client application used by all the clients dedicated to client business. The server receives an HTML page requests and uses CGI, ISAPI, ASP etc to build the page and send it over to the Client. On the other side, the Client receives the page and renders it, analyzing Style Sheets parameters and monkeying around with Visual Basic Script, Java Script or ActiveX components. Quite different jobs. The tClientSocket and tServerSocket are also published on the Palette to let us build Socket application which:
We will now build a simple file transfer application as an example. 3 - Socket File Transfer3.1 - The GoalThe Client will send a file name, and the Server will send the file back. To avoid typing errors, the Client selects the file names in a tFileListBox (so in effect he has already access to the files, but let's pretend that he cannot grab them directly). The Server loads the file and uses Send to transfer the file. 3.2 - The ClientThe Client application uses a tClientSocket. We have the following possibilities:
The tClientSocket.OnRead will be notified of data reception. The Client first reads the file size (4 bytes) and then reads the packets in a loop, until the reception buffer is empty. If the file was not received, the loop is exited, and the next OnRead will fetch the next packets. 3.3 - The ServerThe "listen" and "disconnect" buttons allow to start or stop the Server. The requested file will be read using a tFileStream and sent over to the client with tServerSocket.SendStream or tServerSocket.SendBuff. Since several Clientstransfer might overlap, we have to use a separate tFileStream for each incoming Client. We chose to save those tFileStreams as pointers in the tListBox.Objects. The key we chose to identify each Client is the ServerClientSocket handle. Here is the tServerSocket.OnAccept handler:
And when the fd_read notification arrives, we use the following tServerSocket.OnRead handler:
This procedure is unnecessarily complex, because we wanted to use packet transfers with delay in order to display several Clients in action. If we use a zero delay, only SendStream is called. Notice however that Delphi frees the tFileStream when the transfer is over. We personally prefer to let the unit which calls Create also call Free, but this is not the case here. 3.4 - Transfer exampleHere is a snapshot of 2 clients downloading files from the server: In this example
In this case, since the server uses an infinite loop until all the file is sent, we will observe that:
If we wanted to evenly spread the sending between clients, we should
4 - Download the SourcesYou can download the Client and Server projects (not very useful, but anyway...):
Those .ZIP files contain:
Those .ZIP
To use the .ZIP:
To remove the .ZIP simply delete the folder. As usual:
5 - The authorFelix John COLIBRI works at the Pascal Institute. Starting with Pascal in 1979, he then became involved with Object Oriented Programming, Delphi, Sql, Tcp/Ip, Html, UML. Currently, he is mainly active in the area of custom software development, Delphi Consulting and Delph training, and is a frequent speaker at Borland Developer Conferences. His web site features tutorials, technical papers about programming with full downloadable source code, and the description and calendar of forthcoming Delphi, Interbase, Asp.Net, Ado.Net and OOP / UML training sessions. |
|
[转] Delphi Socket Architecture的更多相关文章
- Delphi Socket Demo
Delphi Socket Demo 转自 http://www.anqn.com/dev/delphi/2010-01-07/a09122531-1.shtml 自己对中间有点修改,下面是代码 ...
- Delphi Socket通信及多线程编程总结
http://cxhblog.blog.sohu.com/41930676.html 一.Socket通信: Delphi在ScktComp单元中对WinSock进行了封装,该单元提供了TAbstra ...
- 初涉Delphi Socket编程
不是第一次接触socket编程了,但以前都是看别人的依葫芦画瓢,也不知道具体的原理. 新的项目,有了新的开始,同时也需要有新的认识. Delphi 中带有两套TCP Socket组件: Indy So ...
- Delphi Socket 阻塞线程下为什么不触发OnRead和OnWrite事件
//**********************************************************************************//说明: 阻塞线程下为什么不触 ...
- Delphi socket() 函数的应用
socket()系统调用,带有三个参数: 1.参数domain指明通信域,如PF_UNIX(unix域),PF_INET(IPv4), PF_INET6(IPv6)等 2.type指明通信类型,最常用 ...
- delphi socket 编程 使用多线程
http://blog.csdn.net/lailai186/article/details/8788710?utm_source=tuicool TClientSocket和TServerSocke ...
- Delphi Socket的最好项目——FastMsg IM(还有一些IM控件),RTC,RO,Sparkle等等,FileZilla Client/Server,wireshark,NSClient
https://www.nsclient.org/nsclient/ 好好学习,天天向上
- DELPHI下的SOCK编程
DELPHI下的SOCK编程(转自http://www.cnblogs.com/devcjq/articles/2325600.html) 本文是写给公司新来的程序员的,算是一点培训的教材.本文不会 ...
- Android InputStream转Bitmap
android socket服务端 接收Delphi socket客户端发来的图片,保存到bitmap中,代码如下: public static Bitmap readInputStreamToBit ...
随机推荐
- html5--移动端视频video的android兼容,去除播放控件、全屏等
html5 中的video 在手机浏览器中的总结所有页面播放时, 如果选择全屏播放, 播放画面将浮动到屏幕的最上层 IOS 手机 自动播放 播放界面浮动文字 播放时是否自动全屏 能否嵌入在页面中播 ...
- Java精选笔记_集合概述(Collection接口、Collections工具类、Arrays工具类)
集合概述 集合有时又称为容器,简单地说,它是一个对象,能将具有相同性质的多个元素汇聚成一个整体.集合被用于存储.获取.操纵和传输聚合的数据. 使用集合的技巧 看到Array就是数组结构,有角标,查询速 ...
- NGUI在5.3打包失败问题
一.NGUI版本 NGUI是很好用的Unity UI插件. 当前使用版本NGUI Next-Gen UI v3.9.7 (Feb 10, 2016)和NGUI Next-Gen UI 3.9.0两个版 ...
- laravel框架容器管理的一些要点
本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章很长,但是内容应该很有用,希望有需要的朋友能看到.php经验有限,不到位的地方,欢迎帮忙指正. ...
- Runtime应用(二)使用对象关联为分类增加属性(每个对象的属性互不干扰)
一.对象的关联方法有 1. void objc_setAssociatedObject(id object, const void *key, id value,objc_AssociationPol ...
- 【转载】为ASP.NET MVC及WebApi添加路由优先级
路由方面的: 转载地址:http://www.jb51.net/article/73417.htm Author:lijiao 这是一个对Asp.Net Mvc的一个很小的功能拓展,小项目可能不太需要 ...
- 如何偷懒地用 PHP 搭建一个班级网站
版权声明:本文由李宜东原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/116 来源:腾云阁 https://www.qclo ...
- 学习坤哥的replaceTpl方法
学习坤哥的方法之后自己写的replaceTpl function replaceTpl(tpl, data){///////////////没有传入可让用户自己定义的方式进行替换,不够灵活 ...
- hihocoder [Offer收割]编程练习赛14 投掷硬币
题目2 : 投掷硬币 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi有一枚神奇的硬币.已知第i次投掷这枚硬币时,正面向上的概率是Pi. 现在小Hi想知道如果总共投 ...
- windows本地环境如何用wamp配置多域名绑定访问
https://jingyan.baidu.com/article/acf728fd5fcdadf8e510a3e5.html