• 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)."
    ";
    //}
    //
    // if(($ret = socket_bind($sock,$ip,$port)) < 0) {
    //         echo "socket_bind() 失败的原因是:".socket_strerror($ret)."
    ";
    // }
    //
    // if(($ret = socket_listen($sock,4)) < 0) {
    //        echo "socket_listen() 失败的原因是:".socket_strerror($ret)."
    ";
    //}
    //
    // $count = 0;
    //
    // do {
    //        if (($msgsock = socket_accept($sock)) < 0) {
    //                 echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "
    ";
    //        break;
    //     } else {
    //
    //         //发到客户端
    //        $msg ="测试成功!
    ";
    //         socket_write($msgsock, $msg, strlen($msg));
    //
    //        echo "测试成功了啊
    ";
    //         $buf = socket_read($msgsock,8192);
    //
    //
    //         $talkback = "收到的信息:$buf
    ";
    //        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>
    ";
    $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()) . "
    ";
    } else {
        echo "OK. 
    ";
    }
    
    echo "Attempting to connect to '$address' on port '$service_port'...";
    $result = socket_connect($socket, $address, $service_port);
    if($result === false) {
        echo "socket_connect() failed.
    Reason: ($result) " . socket_strerror(socket_last_error($socket)) . "
    ";
    } else {
        echo "OK 
    ";
    }
    $in="我上线啦!";
    $a=mb_convert_encoding($in,"GBK","UTF-8");
    //$in = "HEAD / http/1.1
    ";
    //$in .= "HOST: localhost 
    ";
    //$in .= "Connection: close
    
    ";
    //$out = "";
    echo "sending http head request ...";
    socket_write($socket, $a, strlen($a));
    echo  "OK
    ";
    
    echo "Reading response:
    
    ";
    while ($out = socket_read($socket, 8192)) {
        echo $out;
    }
    echo "closeing socket..";
    socket_close($socket);
    echo "ok .
    
    ";
    /***************************客户端*******************************/

     

    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.

    http://blog.csdn.net/s371795639/article/details/53980181

  • 相关阅读:
    只要肯下功夫,十岁也能学得会的 Docker 精简版!
    sprintf和sscanf的用法
    ubuntu在线安装vscode
    Makefile模板
    本地Git配置绑定远程Github账户
    mysql多表查询
    VS error 2019 错误
    Oracle--约束
    Oracle--增删查
    Oracle--子查询
  • 原文地址:https://www.cnblogs.com/findumars/p/6323494.html
Copyright © 2020-2023  润新知