使用了2个组建:

TServerSocket

TClientSocket

------------------TServerSocket---------------------------

//开启连接

ServerSocket1.Port := 9000;
ServerSocket1.Active := true;

//接收客户端发送的消息。对应事件:OnClientRead

Socket.ReceiveText

//发送消息给客户端。

ServerSocket1.Socket.Connections[0].SendText(Edit1.Text);

----------------------TClientSocket---------------------------------------

//连接服务端

ClientSocket1.Host := '127.0.0.1';
ClientSocket1.Port := 9000;

ClientSocket1.Active := true;

//接受服务端发送的消息 。对应事件:OnRead

str := Socket.ReceiveText

//发送消息给服务端。

ClientSocket1.Socket.SendText(Edit1.Text);

下面为完整代码:

unit Client;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ScktComp, StdCtrls, ExtCtrls, Sockets; type
TFrmClient = class(TForm)
ClientSocket1: TClientSocket;
Memo1: TMemo;
Panel1: TPanel;
Label2: TLabel;
Edit2: TEdit;
Button2: TButton;
Panel2: TPanel;
Label1: TLabel;
Button1: TButton;
Edit1: TEdit;
Label3: TLabel;
Edit3: TEdit; procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ClientSocket1Read(Sender: TObject; Socket: TCustomWinSocket); private
{ Private declarations }
public
{ Public declarations }
end; var
FrmClient: TFrmClient; implementation {$R *.dfm} procedure TFrmClient.Button2Click(Sender: TObject);
begin
if (ClientSocket1.Active = false) then
begin
ClientSocket1.Host := Edit3.Text;
ClientSocket1.Port := strtoint(Edit2.Text); ClientSocket1.Active := true;
Self.Caption := '连接socket服务器成功!';
end;
end; procedure TFrmClient.ClientSocket1Read(Sender: TObject;
Socket: TCustomWinSocket);
begin
// Memo1.Lines.Add('服务器说:' + Socket.ReceiveText);
if (Memo1.Lines.Count > 50) then
Memo1.Lines.Clear; Memo1.Lines.Add('服务器:' + ' Ip=' + Socket.RemoteAddress + ' ' + datetimetostr
(now)); // Socket.RemoteHost +
Memo1.Lines.Add(Socket.ReceiveText);
Memo1.Lines.Add('');
end; procedure TFrmClient.Button1Click(Sender: TObject);
begin
// ClientSocket1.Socket.SendText(Edit1.Text);
with ClientSocket1 do
begin
if Edit1.Text <> '' then
begin
Memo1.Lines.Add('客户端:' + ' ' + datetimetostr(now));
Socket.SendText(Edit1.Text);
Memo1.Lines.Add(Edit1.Text);
Memo1.Lines.Add('');
end;
end;
end; end.
unit server;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ScktComp; type
TFrmMain = class(TForm)
ServerSocket1: TServerSocket;
Memo1: TMemo;
Panel1: TPanel;
Edit2: TEdit;
Label2: TLabel;
Button2: TButton;
Panel2: TPanel;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
CheckBox1: TCheckBox;
procedure ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
procedure ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent;
var ErrorCode: Integer);
procedure ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
procedure Button2Click(Sender: TObject);
procedure Edit2Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
FrmMain: TFrmMain; implementation {$R *.dfm} procedure TFrmMain.Button1Click(Sender: TObject);
begin
with ServerSocket1 do
begin
if Edit1.Text <> '' then
begin
Memo1.Lines.Add('服务器:' + ' ' + datetimetostr(now));
Socket.Connections[0].SendText(Edit1.Text);
// Socket.Connections[0].RemoteAddress;//IP
Memo1.Lines.Add(Edit1.Text);
Memo1.Lines.Add('');
end;
end;
end; procedure TFrmMain.Button2Click(Sender: TObject);
begin
if Edit2.Text <> '' then
begin
try
ServerSocket1.Port := strtoint(Edit2.Text);
ServerSocket1.Active := true;
Self.Caption := 'Socket服务器待命中,端口:' + Edit2.Text;
Button2.Enabled := False;
except
Self.Caption := 'Socket服务器没有准备好';
end;
end;
end; procedure TFrmMain.Edit2Change(Sender: TObject);
begin
Button2.Enabled := true;
end; procedure TFrmMain.FormCreate(Sender: TObject);
begin
Self.Caption := 'Socket服务器';
end; procedure TFrmMain.ServerSocket1ClientConnect(Sender: TObject;
Socket: TCustomWinSocket);
begin
Memo1.Lines.Add('-----客户端:' + Socket.RemoteAddress + ' 发消息来连接了--');
end; procedure TFrmMain.ServerSocket1ClientError(Sender: TObject;
Socket: TCustomWinSocket; ErrorEvent: TErrorEvent; var ErrorCode: Integer);
begin
ErrorCode := 0;
end; procedure TFrmMain.ServerSocket1ClientRead(Sender: TObject;
Socket: TCustomWinSocket);
var
stemp: string;
begin
try
if (Memo1.Lines.Count > 50) then
Memo1.Lines.Clear; Memo1.Lines.Add('客户端:' + ' Ip=' + Socket.RemoteAddress + ' ' +
datetimetostr(now)); // Socket.RemoteHost +
Memo1.Lines.Add(Socket.ReceiveText);
Memo1.Lines.Add(''); if (CheckBox1.Checked = true) then
begin
Button1.Click;
// Socket.SendText(Edit1.Text);
end; except
Memo1.Lines.Add(datetimetostr(now) + ' :程序异常');
end;
end; end.

delphi之socket通讯的更多相关文章

  1. Delphi 跨平台 Socket 通讯库

    Delphi 跨平台 Socket 通讯库 免费开源的Delphi 跨平台 Socket 通讯库. 源码URL:https://github.com/winddriver/Delphi-Cross-S ...

  2. Delphi跨平台Socket通讯库

    盒子中的souledge大侠发布了新的Socket库,以下为原文: 我之前写过一个iocp的框架,放到googlecode上了. 由于当时的delphi版本尚无法跨平台,所以该框架只能运行在Windo ...

  3. delphi的socket通讯 多个客户端 (转)

    ClientSocket组件为客户端组件.它是通信的请求方,也就是说,它是主动地与服务器端建立连接. ServerSocket组件为服务器端组件.它是通信的响应方,也就是说,它的动作是监听以及被动接受 ...

  4. Delphi的Socket编程步骤(repulish)

    转贴自:http://topic.csdn.net/t/20010727/16/212155.html ClientSocket 和ServerSocket几个重要的属性:   1.client和se ...

  5. 客户端技术的一点思考(数据存储用SQLite, XMPP通讯用Gloox, Web交互用LibCurl, 数据打包用Protocol Buffer, socket通讯用boost asio)

    今天看到CSDN上这么一篇< 彻底放弃没落的MFC,对新人的忠告!>, 作为一个一直在Windows上搞客户端开发的C++程序员,几年前也有过类似的隐忧(参见 落伍的感觉), 现在却有一些 ...

  6. 闲来无事,写个基于TCP协议的Socket通讯Demo

    .Net Socket通讯可以使用Socket类,也可以使用 TcpClient. TcpListener 和 UdpClient类.我这里使用的是Socket类,Tcp协议. 程序很简单,一个命令行 ...

  7. 试解析Tomcat运行原理(一)--- socket通讯

    关于这篇文章也确实筹划了很久,今天决定开篇写第一篇,说起tomcat首先很容易联想到IIS,因为我最开始使用的就是.net技术,我第一次使用asp写学生成绩管理系统后,很茫然如何让别人都能看到或者说使 ...

  8. c# TCP Socket通讯基础

    在做网络通讯方面的程序时,必不可少的是Socket通讯. 那么我们需要有一套既定的,简易的通讯流程. 如下: <pre name="code" class="csh ...

  9. Android笔记:Socket通讯常见问题

    经验证的socket通讯问题 1.如果是模拟器和本机PC直接通讯,需要使用本机IP地址 而不是 10.0.2.2  如本机的静态地址为192.168.1.2 则直接使用该地址 2.接收和连接代码不能在 ...

随机推荐

  1. 织梦dedecms5.7手机站页面首页正常其他页面显示pc页面解决方法

       最近遇到的问题,用的是织梦的dedecms从以前的版本升级上来的最新版5.7sp2,客户需要手机版的,要做一个百度的验证.   这个站首页显示算是基本正常,点开里面随便一个页面会跳转到pc页面上 ...

  2. 使用docker部署STF服务(CentOS环境)

    一.安装docker环境 更新软件 sudo yum update 执行安装 sudo yum install docker 查看docker镜像 sudo docker images 二.拉取相关镜 ...

  3. 完美解决读取Excel的数字单元格时Cannot get a STRING value from a NUMERIC cell 报错处理

    我使用的是Poi(最新的4.1.0)方式读取Excel ,我的方法如下: 在打印cell内容时,抛出下面的错误 Exception in thread "main" java.la ...

  4. CDH集群安装配置(七)--CDH组件的安装和配置

    1. Clouder Manger页面的配置 访问主节点IP:(cdh1)192.168.80.81:7180 默认用户名和密码:admin,admin 选择一个版本 选择集群的服务器(agent), ...

  5. hadoop踩坑:localhost:50070 无法访问 关闭防火墙

    ubuntu 关闭防火墙:ufw disable hadoop3.0以下版本web访问端口50070:3.0及以上web访问端口9870 参考链接:https://blog.csdn.net/qq_3 ...

  6. condition实现原理

    condition是对线程进行控制管理的接口,具体实现是AQS的一个内部类ConditionObject,主要功能是控制线程的启/停(这么说并不严格,还要有锁的竞争排队). condition主要方法 ...

  7. MySQL移动数据目录出现权限问题

    MySQL移动数据目录出现权限问题 环境: ubuntu 14.04.4 LTS 现象 今天把/var/lib/mysql下的数据文件移动到其他目录下,之后发现启动mysql报错,并且mysql无法运 ...

  8. Mybatis Dao开发方法(二)

    使用Mapper代理的方式进行开发 Mapper开发的原理   使用Mybatis的Mapper代理方式进行开发,使用该方式,只需要编写Mapper接口,不再需要编写实现类,由Mybatis框架通过接 ...

  9. <数据挖掘导论>读书笔记3--分类

    1.分类的基本概念 分类任务就是通过学习得到一个目标函数f,把每个属性集x映射到一个预先定义的类标号y 目标函数也称为分类模型. 2. 解决分类问题的一般方法: 决策树分类法 基于规则的分类法 神经网 ...

  10. Javascript的构造函数和constructor属性

    原型链 function Foo() { this.value = 42;}Foo.prototype = { method: function() {}}; function Bar() {} // ...