这是我正在做的事情的前提:
我有一个程序在后台运行,一直是套接字服务器。
我创建了一个调用php脚本(使用AJAX)的网站,该脚本打开客户端套接字,连接到服务器,发送请求并接收它,然后回显它。
这个回声是响应是'返回值'对于我的AJAX脚本。
这就是这个问题:
当我单击按钮时,信息会发送,但有时,信息永远不会返回到站点。
网站代码:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input type="submit" class="button" name="weather" value="weather" />
<input type="submit" class="button" name="time" value="time" />
<input type="submit" class="button" name="date" value="date" />
</form>
jQuery代码:
$(document).ready(function(){
$('.button').click(function(){
var clickBtnValue = $(this).val();
var ajaxurl = 'php/portserverclient.php',
dataSend = {'action': clickBtnValue};
$.post(ajaxurl, dataSend, function (response) {
// Response div goes here.
//$('#servermessage').val($('#servermessage').val()+response);
alert(response);
});
});
PHP代码:
<?php
// -------------------------------------
// ----- Get Information Sent from Site
// -------------------------------------
if (isset($_POST['action']))
{
switch ($_POST['action']) {
case 'weather':
$in = "3";
$request = "Weather";
break;
case 'time':
$in = "2";
$request = "Time";
break;
case 'date':
$in = "1";
$request = "Date";
}
}
else
{
$in = "3";
$request = "Weather";
}
// -------------------------------------
// ----- Open Client Side Socket
// -------------------------------------
error_reporting(E_ALL);
$A1 = "Starting TCP/IP Connection... \n";
/* Get the port for the WWW service. */
$service_port = 51713;
/* Get the IP address for the target host. */
$address = '192.168.8.129';
/* Create a TCP/IP socket. */
$socket = socket_create(AF_INET, SOCK_STREAM, 0);
if ($socket === false)
{
$A2 = "socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n";
exit;
}
else {
$A2 = "Socket Created...OK.\n";
}
socket_set_option($socket, SOL_SOCKET, SO_REUSEADDR, 1);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 5, 'usec' => 0));
socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec' => 5, 'usec' => 0));
$A3 = "Attempting to connect to '$address' on port '$service_port'...";
$result = socket_connect($socket, $address, $service_port);
if ($result === false)
{
$A4 = "socket_connect() failed.\nReason: ($result) " . socket_strerror(socket_last_error($socket)) . "\n";
exit;
}
else
{
$A4 = "OK.\n";
}
// -------------------------------------
// ----- Send message through socket
// -------------------------------------
$A5 = "Sending $request request...";
$sent = socket_write($socket, $in, strlen($in));
$A6 = "OK.\n";
// -------------------------------------
// ----- Receive response through socket
// -------------------------------------
$out = ' ';
do{$out = socket_read($socket, 20, PHP_NORMAL_READ) ;}
while($out === false);
// -------------------------------------
// ----- Close Client Side Socket
// -------------------------------------
$A7 = "Closing socket...";
$A8 = "OK.\n";
echo "\n From PHP END: ", $A1,$A2,$A3,$A4,$A5,$A6,$A7,$A8,"Message: ", $out,"\n";
socket_shutdown($socket,2);
socket_close($socket);
exit;
?>
C ++服务器端代码:
//---- read buffer ---
sizeofread = read(newsockfd, msg, 20);
if (sizeofread <= 0 || msg[0] == 0x0A || msg[0] == 0x0D || msg[0] == 0x00)
{
// Stops any buggy send (size of read <= 0)
// Stops terminal from sending new line (0x0A & 0x0D) after each press enter
// Stops terminal from sending 0 constantly (0x00)
}
else
{
printf("Received from TCP: %s \n",msg);
//--- Send to Arduino ---
printf( "Sending to Arduino: %s \n", msg);
write( uartt, msg, 20 );
delay(50); // Here to account for how long it takes to send & receive 20 bytes at 9600 Baud
//--- Receive from Arduino ---
while (counter <= 2 && found == false)
{
if ( !( msgLength = serialDataAvail(uartt) ))
{
printf("Nothing to read \n");
delay(1071); // Here to account for how long it takes to receive 1024 bytes at 9600 Baud
counter++;
}
else
{
found = true;
read(uartt, msg, msgLength);
msg[msgLength] = 0;
if (write(newsockfd, msg, 20) < 0)
{
printf("Failed to send to socket \n");
}
else
{
printf("Succesfully sent to socket \n");
}
printf("From the Arduino: ");
printf("%s \n", msg);
}
delay(100);
serialFlush(uartt);
}
counter = 0;
found = false;
close(newsockfd);
goto repeat;
}
答案 0 :(得分:1)
原来这个谜语的答案是我需要禁用提交按钮默认事件。通常,在HTML中,提交按钮刷新页面,必须停止。
这样就停止了:
$('.button').click(function(event){
var clickBtnValue = $(this).val();
var ajaxurl = 'php/portserverclient.php',
dataSend = {'action': clickBtnValue};
$.post(ajaxurl, dataSend, function (response) {
alert(response);
});
event.preventDefault();
});
谢谢你们