php socket编程实例

Eddy 发布于2012-12-24 14:46:14 分类: 网站相关 已浏览loading 网友评论0条 我要评论

因为用到客户端程序与网页三方支付接口交互,我觉得可以这样来简单实现,在三方支付回调页面中放一个socket服务端,客户端程序与这个socket服务端进行交互,以此来进行最终业务逻辑处理。

php中socket编程,简单实例代码如下:

服务端:


<?php
set_time_limit(0);

if(!extension_loaded('sockets')){
	//die('服务器未加载sockets扩展');
	if(strtoupper(substr(PHP_OS, 3)) == “WIN”) 
	{ 
		dl(‘php_sockets.dll’);
	}
	else
	{
		dl(‘sockets.so’); 
	} 
}

$sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if(!$sock){
	die('创建socket失败');
}

if(!socket_bind($sock,$_SERVER['SERVER_ADDR'],9394)){
	die('socket绑定失败');
}

if(!socket_listen($sock)){
	die('socket监听失败');
}

$client_id = 0;
do{
	//阻塞,直到有客户端连接
	$connection = socket_accept($sock);
	++$client_id;
	printf("Socket connected-$client_id\n");

	$buffer='Hello, world!';
	socket_write($connection, $buffer . "\n");

	do{
			$buf =socket_read($connection, 1024, PHP_NORMAL_READ);
			//退出连接条件
			if($buf=="100\n"){
				socket_write($connection, 'bye' . "\n");
				break;
			}
			echo $buf;
			socket_write($connection, 'copy that' . "\n");
	}while(1);

	socket_close($connection);
}while(1);

socket_close($sock);
?>

客户端:


<?php
if(!extension_loaded('sockets')){
	//die('服务器未加载sockets扩展');
	if(strtoupper(substr(PHP_OS, 3)) == “WIN”) 
	{ 
		dl(‘php_sockets.dll’);
	}
	else
	{
		dl(‘sockets.so’); 
	} 
}

$socket = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
if(!$socket){
	die('创建socket失败');
}

$connection = socket_connect($socket,'localhost', 9394);

$send=0;
do{
	$buffer = socket_read($socket, 1024, PHP_NORMAL_READ);
	//退出连接条件
	if($buffer=="bye\n"){
		break;
	}

	echo $buffer;
	++$send;
	socket_write($socket, $send . "\n");
}while(1);

socket_close($socket);
?>

与c++程序交互,客户端:


#include <WinSock2.h>
#include <IOSTREAM>
#pragma comment(lib,"ws2_32.lib")
using namespace std;

int main(int argc, char* argv[])
{
	WSADATA wsaData;
	WORD wVersionRequested=MAKEWORD(2,2);
	int err = WSAStartup(wVersionRequested,&wsaData);
	if (!err)
	{
		cout<<"inital socket success"<<endl;
		SOCKET sock = WSASocket(AF_INET,SOCK_STREAM,0,NULL,0,WSA_FLAG_OVERLAPPED);
		if (sock != INVALID_SOCKET)
		{
			cout<<"create socket success"<<endl;
			
			SOCKADDR_IN addr;
			addr.sin_addr.S_un.S_addr=inet_addr("127.0.0.1");
			addr.sin_family=AF_INET;
			addr.sin_port=htons(9394);
			int errcon = connect(sock,(SOCKADDR*)&addr,sizeof(SOCKADDR));
			if (errcon == SOCKET_ERROR)
			{
				cout<<"connection failed:"<<WSAGetLastError()<<endl;
				closesocket(sock);
				WSACleanup();
				return -1;
			}
			cout<<"connection success"<<endl;
			char sendBuf[1024]={0};
			char recvBuf[1024]={0};

			recv(sock,recvBuf,1024,0);
			cout<<recvBuf<<endl;

			while (TRUE)
			{
				cout<<"Please input data:"<<endl;
				cin>>sendBuf;
				if (sendBuf==NULL)
				{
					break;
				}

				send(sock,sendBuf,strlen(sendBuf),0);
				Sleep(500);
				recv(sock,recvBuf,1024,0);
				cout<<recvBuf<<endl;
				if (!strcmp(recvBuf,"bye"))
				{
					cout<<"connection over"<<endl;
					break;
				}
				Sleep(500);
			}
			closesocket(sock);
			WSACleanup();
		} 
		else
		{
			cout<<"create socket failed"<<endl;
			WSACleanup();
		}
	}
	else
	{
		cout<<"inital socket failed"<<endl;
	}
	return 0;
}

与c/c++程序交互,php服务端读取数据应该用二进制模式读取:

另外,发送数据时,字符串末尾要加\0:


do{
			//$buf =socket_read($connection, 1024, PHP_NORMAL_READ);
			$buf =socket_read($connection, 1024, PHP_BINARY_READ);
			//退出连接条件
			var_dump($buf);
			if($buf=="quit"){
				socket_write($connection, "bye\0");
				break;
			}else if($buf=="isok"){
				socket_write($connection, "ok\0");
			}
			echo $buf;
			socket_write($connection, "copy that\0");
	}while(1);

已经有(0)位网友发表了评论,你也评一评吧!
原创文章如转载,请注明:转载自Eddy Blog
原文地址:http://www.rrgod.com/webdesign/864.html     欢迎订阅Eddy Blog

记住我的信息,下次不用再输入 欢迎给Eddy Blog留言