在实际开发中,有时会遇到PHP和C语言结构的互相转化问题,这时候PHP的函数pack和unpack就能帮上大忙了. ------------------------------------------------
/*server.c*/ -------------------------------------------------------- <? -------------------------------------- socket_accept() 接受一个Socket连接 |
=========================================
关于PHP与C语言SOCKET通信的问题
我用PHP(客户端)与C(服务器端)语言建立SOCKET连接,发送字符串的时候正常,但是发送整型数的时候就收到的是一堆莫名奇妙的数字,为什么?
PHP端程序如下:
<?php
$fp = fsockopen("202.112.25.163", 3333, &$errno, &$errstr, 10);
if(!$fp) {
echo "$errstr ($errno)<br>/n";
} else {
$a=0;
fputs($fp,(string)$a);
while(!feof($fp)) {
echo fgets($fp,128);
}
fclose($fp);
}
?>
解决:
fputs($fp,(string)$a);
===>
fputs($fp,"$a");
把
fputs($fp,(string)$a);
改为
fputs($fp,"$a");
==========================================================
in PHP manual :
Socket example: Simple TCP/IP client
This example shows a simple, one-shot HTTP client. It simply connects to a page, submits a HEAD request, echoes the reply, and exits.
<?php
error_reporting(E_ALL);
echo "<h2>TCP/IP Connection</h2>/n";
/* Get the port for the WWW service. */
$service_port = getservbyname('www', 'tcp');
/* Get the IP address for the target host. */
$address = gethostbyname('www.example.com');
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
echo "socket_create() failed: reason: " . socket_strerror($socket) . "/n";
} else {
echo "OK./n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result < 0) {
echo "socket_connect() failed./nReason: ($result) " . socket_strerror($result) . "/n";
} else {
echo "OK./n";
}
$in = "HEAD / HTTP/1.1/r/n";
$in .= "Host: www.example.com/r/n";
$in .= "Connection: Close/r/n/r/n";
$out = '';
echo "Sending HTTP HEAD request...";
socket_write($socket, $in, strlen($in));
echo "OK./n";
echo "Reading response:/n/n";
while ($out = socket_read($socket, 2048)) {
echo $out;
}
echo "Closing socket...";
socket_close($socket);
echo "OK./n/n";
?>
====================================================
I need a php page to connect to a python created socket and sent and
receive data. below is the python code which opens a socket on the
localhost @ port 21567:
#!/usr/bin/python2
from socket import *
from time import time,ctime
HOST ='127.0.0.1'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)
tcpS = socket(AF_INET,SOCK_STREAM)
tcpS.bind(ADDR)
tcpS.listen(5)
while 1:
print "waiting for connection..."
tcpC ,addr = tcpS.accept()
print '...conected from:',addr
while 1:
data = tcpC.receive(BUFSIZ)
if not data: break
tcpC.send('[%s] %s' % (ctime(time()),data))
tcpC.close()
tcpS.close()
################## end of code
The above works fine I can attach via a python client on another machine.
Below is the php code for the page to attach to the socket at present
(for debugging) they are running on the same machine, it attaches and
sends the message "it is working" to the socket @ 21567 and it sould
return with the same message timestamped:
<?php
error_reporting (E_ALL);
echo "<h2Socket Test</h2/n";
$address = '127.0.0.1';
$service_port = 21567;
$socket = socket_create (AF_INET, SOCK_STREAM, 0);
if ($socket < 0)
{
echo "socket_create() failed: reason: " . socket_strerror ($socket) .
"/n";
}
else
{
echo "OK./n";
}
echo "Attempting to connect to '$address' on port '$service_port'...";
$in = "is it working";
$out = '';
echo "Sending Info request...";
socket_write ($socket, $in, strlen ($in));
echo "OK./n";echo "Reading response:/n/n";
while ($out = socket_read ($socket, 1024))
{
echo $out;
}
echo "Closing socket...";
socket_close ($socket);
echo "OK./n/n";
?
below are the results:
Socket Test
OK. Attempting to connect to '127.0.0.1' on port '21567'...Sending Info
request...
Warning: socket_write() unable to write to socket 10 [32]: Broken pipe in
/var/www/html/tcpsock.php on line 20
OK. Reading response:
Warning: socket_read() unable to read from socket [107]: Transport
endpoint is not connected in /var/www/html/tcpsock.php on line 22
Closing socket...OK.
====================================================