Delphi Server and PHP client communicate via socket

Source: Internet
Author: User

Look at the effect before you start

PHP page as a client sends a request to the Delphi application as a server

The PHP client page opens as follows


The Delphi server application opens as follows


Every time the PHP page refreshes, the Delphi text box shows "I'm online!" "It's rough, but it seems to be a demo of some kind of function.

The PHP code is as follows:

<?php/***************************Service Side*******************************/**//* Created by phpstorm.//* user:administrator//* DATE:2017/1/2 0002//* Time: PM 3:54//*///////Ensure that the client is connected without timeout// Set_time_limit (0);////$ip = ' 192.168.1.110 ';//$port = 1025;/////*//9 +-------------------------------//10 * @socket-Pass  Letter the whole process//11 +-------------------------------//12 * @socket_create//13 * @socket_bind//14 * @socket_listen//15 * @socket_accept//16 * @socket_read//17 * @socket_write//18 * @socket_close//19 +------------------------- -------//20 *//////*----------------The following actions are-------------------*///if ($sock = Socket_create (af_inet,sock_str EAM,SOL_TCP)) < 0) {//echo "Socket_create () failed due to:". Socket_strerror ($sock). " \ n ";//}////if ($ret = Socket_bind ($sock, $ip, $port)) < 0) {//echo" Socket_bind () failed because: ". Socket_strerror ($ret )." \ n ";//}////if (($ret = Socket_listen ($sock, 4)) < 0) {//echo" Socket_listen () failed due to: ". Socket_strerror ($ret)." \ n ";//}////$count = 0;////do {//if ($msgsock = SocKet_accept ($sock)) < 0) {//echo "Socket_accept () Failed:reason:". Socket_strerror ($msgsock). "\ n";//break;//} else {//////Send to client//$msg = "Test succeeded! \ n "//Socket_write ($msgsock, $msg, strlen ($msg)),////echo" Test succeeded AH \ n "//$buf = Socket_read ($msgso                         ck,8192);//////$talkback = "message Received: $BUF \ n";//echo $talkback;////if (+ + $count >= 5) {// break;//};//////}////echo $buf;//Socket_close ($msgsock);////} while (true);////sock Et_close ($sock);/***************************Service Side*******************************///?><?php/***************************Client*******************************/Error_reporting (E_all);Echo"&LT;H2&GT;TCP/IP connection 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 _por T);if($result = = =false) {Echo"Socket_connect () Failed.\nreason: ($result)". Socket_strerror (Socket_last_error ($socket)). "\ n";}Else{Echo"OK \ n";} $in = "I'm online!" "; $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";/***************************Client*******************************/

Delphi

Set up:

The action of ServerSocket1 is true;

ServerSocket1 's Port is 1025

The code is as follows:

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 (' Whisper to you ' + Socket.receivetext);
End

End.

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

Delphi Server and PHP client communicate via socket

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.