【发布时间】:2017-01-05 20:30:14
【问题描述】:
我有一个自己的文件,用于接收发布请求 ajax,但请求永远不会到来。
观点:
<script type="text/javascript">
$(document).ready(function(){
$("#bloquear").click(function(e){
$.post("proyectodam/app/Lib/ServerSocket.php", {op: 1});
});
});
</script>
我在我创建的目录中拥有它的php,他的命名空间:
namespace proyectodam\Lib;
文件 PHP:
<?php
namespace proyectodam\Lib;
try{
set_time_limit(0);
$address = '127.0.0.1';
$port = 5555;
// Create a TCP Stream socket
$sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP); // 0 for SQL_TCP
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address'); //0 for localhost
// Start listening for connections
socket_listen($sock);
//loop and listen
while(true){
/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);
//$_SESSION[socket_getpeername($client, AF_INET, 5555)] = socket_getpeername($client, AF_INET, 5555);
// Read the input from the client – 1024000 bytes
//$input = socket_read($client, 1024000);
// from here you need to do your database stuff
// and handle the response
// Display output back to client
if(isset($_POST["op"])){
$message = 1;
socket_write($client, $message, strlen($message));
}
socket_close($client);
}
// Close the master sockets
socket_close($sock);
}catch(Exception $e){
echo 'Excepción capturada: ', $e->getMessage(), "\n";
}
客户端 java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
public class ClienteSocket{
public static void main(String args[]){
try {
Socket client = new Socket("127.0.0.1", 5555);
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
OutputStreamWriter wr = new OutputStreamWriter(client.getOutputStream());
String op;
while((op = in.readLine()) != null){
System.out.println(op);
}
}catch (IOException e){
System.out.println("error");
e.printStackTrace();
}
}
}
失败了?
【问题讨论】:
-
您确定
proyectodam/app/Lib/ServerSocket.php位置/文件可以从浏览器访问吗?? -
我认为你的问题并没有说太多,但为什么不直接将文件包含到你的路由控制器中呢?
-
我的文件 php 是一个服务器套接字,它在命令行上运行,我需要从发送信息将他花费到 verver 并将这个发送到客户端 pd:我设法连接客户端与服务器
-
我没有工作,我在java中有客户端的部分