Delphi服务端和PHP客户端通过Socket通信

在开始之前看下效果

PHP页面作为客户端发送请求给作为服务端的Delphi应用程序

PHP客户端页面打开如下


Delphi服务端应用程序打开如下


每次PHP页面刷新一下,Delphi的文本框都显示"我上线啦!"做的很粗糙,但是似乎可以作为某种功能的demo


PHP代码如下:

<?php
/***************************服务端*******************************/
///**
// * Created by PhpStorm.
// * User: Administrator
// * Date: 2017/1/2 0002
// * Time: 下午 3:54
// */
//
确保在连接客户端时不会超时
//set_time_limit(0);
//
//$ip = '192.168.1.110';
//$port = 1025;
//
///*
// 9  +-------------------------------
//10  *    @socket通信整个过程
//11  +-------------------------------
//12  *    @socket_create
//13  *    @socket_bind
//14  *    @socket_listen
//15  *    @socket_accept
//16  *    @socket_read
//17  *    @socket_write
//18  *    @socket_close
//19  +--------------------------------
//20  */
//
// /*----------------    以下操作都是手册上的    -------------------*/
// if(($sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP)) < 0) {
//   echo "socket_create() 失败的原因是:".socket_strerror($sock)."\n";
//}
//
// if(($ret = socket_bind($sock,$ip,$port)) < 0) {
//         echo "socket_bind() 失败的原因是:".socket_strerror($ret)."\n";
// }
//
// if(($ret = socket_listen($sock,4)) < 0) {
//        echo "socket_listen() 失败的原因是:".socket_strerror($ret)."\n";
//}
//
// $count = 0;
//
// do {
//        if (($msgsock = socket_accept($sock)) < 0) {
//                 echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
//        break;
//     } else {
//
//         //发到客户端
//        $msg ="测试成功!\n";
//         socket_write($msgsock, $msg, strlen($msg));
//
//        echo "测试成功了啊\n";
//         $buf = socket_read($msgsock,8192);
//
//
//         $talkback = "收到的信息:$buf\n";
//        echo $talkback;
//
//         if(++$count >= 5){
//                         break;
//         };
//
//
//    }
//     //echo $buf;
//     socket_close($msgsock);
//
// } while (true);
//
//socket_close($sock);
/***************************服务端*******************************/
//?>




<?php
/***************************客户端*******************************/
//error_reporting(E_ALL);
echo "<h2>tcp/ip connection </h2>\n";
$service_port = 1025;
$address = '192.168.1.110';

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
    echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
} else {
    echo "OK. \n";
}

echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if($result === false) {
    echo "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
} else {
    echo "OK \n";
}
$in="我上线啦!";
$a=mb_convert_encoding($in,"GBK","UTF-8");
//$in = "HEAD / http/1.1\r\n";
//$in .= "HOST: localhost \r\n";
//$in .= "Connection: close\r\n\r\n";
//$out = "";
echo "sending http head request ...";
socket_write($socket, $a, strlen($a));
echo  "OK\n";

echo "Reading response:\n\n";
while ($out = socket_read($socket, 8192)) {
    echo $out;
}
echo "closeing socket..";
socket_close($socket);
echo "ok .\n\n";
/***************************客户端*******************************/


Delphi

设置:

ServerSocket1的Action为True;

ServerSocket1的Port为1025

代码 如下:

unit Unit1;


interface


uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ScktComp;


type
  TForm1 = class(TForm)
    ServerSocket1: TServerSocket;
    StatusBar1: TStatusBar;
    Button1: TButton;
    Memo1: TMemo;
    Label4: TLabel;
    procedure ServerSocket1ClientConnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocket1ClientDisconnect(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure ServerSocket1ClientRead(Sender: TObject;
      Socket: TCustomWinSocket);
    procedure Button1Click(Sender: TObject);


  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;


implementation


{$R *.dfm}


procedure TForm1.ServerSocket1ClientConnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
StatusBar1.SimpleText := 'connect';
end;


procedure TForm1.ServerSocket1ClientDisconnect(Sender: TObject;
  Socket: TCustomWinSocket);
begin
  StatusBar1.SimpleText := 'disconnect';
end;


procedure TForm1.ServerSocket1ClientRead(Sender: TObject;
  Socket: TCustomWinSocket);
begin
 Memo1.Lines.Add('悄悄的对你说'+ Socket.ReceiveText);
end;

end.





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
delphi socket call php socket 例子,可根据需要扩展写成聊天室、网站助理类似淘宝助理,有订单提醒。 <?php //确保在连接客户端时不会超时 set_time_limit(0); $port = 10081 ; $ip = '192.168.1.102'; // create a streaming socket, of type TCP/IP $sock = socket_create ( AF_INET , SOCK_STREAM , SOL_TCP ); // set the option to reuse the port socket_set_option ( $sock , SOL_SOCKET , SO_REUSEADDR , 1 ); // "bind" the socket to the address to "localhost", on port $port // so this means that all connections on this port are now our resposibility to send/recv data, disconnect, etc.. socket_bind ( $sock , $ip , $port ); // start listen for connections socket_listen ( $sock ); // create a list of all the clients that will be connected to us.. // add the listening socket to this list $clients = array( $sock ); while ( true ) { // create a copy, so $clients doesn't get modified by socket_select() $read = $clients ; // get a list of all the clients that have data to be read from // if there are no clients with data, go to next iteration if ( socket_select ( $read , $write = NULL , $except = NULL , 0 ) < 1 ) continue; // check if there is a client trying to connect if ( in_array ( $sock , $read )) { // accept the client, and add him to the $clients array $clients [] = $newsock = socket_accept ( $sock ); // send the client a welcome message socket_write ( $newsock , "这是一个delphi(客户端) socketPHP_socket(服务器) 通信的例子 测试,交流QQ:410578660。 but ill make an exception :)\n" . "There are " .( count ( $clients ) - 1 ). " client(s) connected to the server\n" ); socket_getpeername ( $newsock , $ip ); echo "New client connected: { $ip } \n" ; // remove the listening socket from the clients-with-data array $key = array_search ( $sock , $read ); unset( $read [ $key ]); } // loop through all the clients that have data to read from foreach ( $read as $read_sock ) { // read until newline or 1024 bytes // socket_read while show errors when the client is disconnected, so silence the error messages $data = @ socket_read ( $read_sock , 1024 , PHP_NORMAL_READ ); // check if the client is disconnected if ( $data === false ) { // remove client for $clients array $key = array_search ( $read_sock , $clients ); unset( $clients [ $key ]); echo "client disconnected.\n" ; // continue to the next client to read from, if any continue; }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值