php套接字服务器实现 [英] php socket server implementation
本文介绍了php套接字服务器实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
I have developed a PHP Socket Server in which I am receiving data from GSM Modules and storing it in MySQL. I want to make it more clever as soon as we get the data we send it to another server at the same time. For the second option I want user to give the host ip address and port to send data. I developed a user input page but I dont know how to configure it. This is my server code. I dont want to restart my server everytime when I change the Ip Address and port of the second host on which i want to send data.
< br $> b $ b
<?php
error_reporting(~E_NOTICE);
set_time_limit(0);
session_start();
require ('manageData.php');
require ('clientInfo.php');
require ('dataCheck.php');
require ('endsession.php');
require ('sendhost.php');
$address = "0.0.0.0";
$port = 9000;
$max_clients = 10;
if (!($sock = socket_create(AF_INET, SOCK_STREAM, 0))) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Couldn't create socket: [$errorcode] $errormsg \n");
}
// Bind the source address
if (!socket_bind($sock, $address, $port)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not bind socket : [$errorcode] $errormsg \n");
}
if (!socket_listen($sock, 10)) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
//array of client sockets
$client_socks = array();
//array of sockets to read
$read = array();
//start loop to listen for incoming connections and process existing connections
while (true) {
//prepare array of readable client sockets
$read = array();
//first socket is the master socket
$read[0] = $sock;
//now add the existing client sockets
for ($i = 0;$i < $max_clients;$i++) {
if ($client_socks[$i] != null) {
$read[$i + 1] = $client_socks[$i];
}
}
//now call select - blocking call
if (socket_select($read, $write, $except, null) === false) {
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
die("Could not listen on socket : [$errorcode] $errormsg \n");
}
//if ready contains the master socket, then a new connection has come in
if (in_array($sock, $read)) {
for ($i = 0;$i < $max_clients;$i++) {
if ($client_socks[$i] == null) {
$client_socks[$i] = socket_accept($sock);
if (socket_getpeername($client_socks[$i], $address, $port)) {
echo "$address \n";
if (strlen($address) > 0) writeToClientInfo("client.csv", $address);
}
socket_write($client_socks[$i], $message);
break;
}
}
}
//check each client if they send any data
for ($i = 0;$i < $max_clients;$i++) {
if (in_array($client_socks[$i], $read)) {
$input = socket_read($client_socks[$i], 4096);
if ($input == null) {
//zero length string meaning disconnected, remove and close the socket
unset($client_socks[$i]);
socket_close($client_socks[$i]);
writeToEndSession("client.csv", $address);
}
$n = trim($input);
$handle = ',';
if (strlen($n) > 0) {
if (strpos($n, $handle) !== false) {
writeToFile('GSM.csv', $n);
sendData($n);
} else if (strpos($n, $handle) === false || strpos($n, "/") !== false) {
writeToRaw('RAW.csv', $n);
}
} else break;
//send response to client
socket_write($client_socks[$i], "Data Received \n");
}
}
}
?>
发送数据功能。
send data function .
<?php
function sendData($n) {
date_default_timezone_set("Europe/London");
$date = date('Y-m-d H:i:s');
if (isset($_POST['submit'])) {
$thost = $_GET['hostt'];
$tport = $_GET['portt'];
}
$th = $thost;
$tp = $tport;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket === false) {
echo "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
}
$conn = socket_connect($socket, $th, $tp);
if ($conn === false) {
echo "socket_connect() failed.\nReason: ($conn) " . socket_strerror(socket_last_error($socket)) . "\n";
}
if($th !== 0) echo "Settings Saved Successfully";
else echo "Error Saving the Settings";
socket_write($socket, $n, strlen($n));
?>
和输入的html代码< br $> b $ b
and html code for input
<form action="sendhost.php" id="login-form" name="form1" method="post">
<br> Server IP:
<br>
<center>
<input type="text" value="" class="form-control required" name="hostt" />
</center>
<br> Server Port:
<br>
<center>
<input type="text" value="" class="form-control required" name="portt" />
<br>
</center>
<br>
<center>
<div class="group submit">
<label class="empty"></label>
<div><input type="submit" value="Save Details" action="sendhost.php"/></div>
</div>
</center>
</form>
推荐答案
address = 0.0.0.0;
address = "0.0.0.0";
port = 9000 ;
port = 9000;
max_clients = 10 ;
if (!(
max_clients = 10; if (!(
这篇关于php套接字服务器实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文