简体   繁体   English

简单的Java TCP服务器和PHP客户端问题

[英]Simple Java TCP Server and PHP Client Problems

I am trying to set up a simple TCP connection on a single port between a Java application that will act as the TCP server and a PHP scrip that will act as the Client. 我试图在将充当TCP服务器的Java应用程序和充当客户端的PHP脚本之间的单个端口上建立简单的TCP连接。

I will post the code for each below, but the problem is: I can connect and send data to the Java server just fine. 我将在下面发布每个代码,但问题是:我可以连接并将数据发送到Java服务器就好了。 I can get that data and print it out. 我可以获取该数据并将其打印出来。 My problem arises in trying to send a response back to the php server. 我的问题出现在尝试将响应发送回php服务器。

When I comment out that last line of php "echo socket_read($socket, 14, PHP_NORMAL_READ);" 当我注释掉最后一行php“echo socket_read($ socket,14,PHP_NORMAL_READ);” The data gets to the Java server just fine. 数据到达Java服务器就好了。 When I add that line back in, the data does not even get to the Java server. 当我重新添加该行时,数据甚至不会到达Java服务器。

Because of this, I am assuming my problem has something to do with how I am either sending the data from Java, or trying to get the data in PHP from the server. 因此,我假设我的问题与我如何从Java发送数据或尝试从服务器获取PHP数据有关。

This has me really stumped, any help would be greatly appreciated! 这让我真的很难过,任何帮助都将不胜感激!

Java Server: Java服务器:

protected ServerSocket socket;
protected final int port = 9005;
protected Socket connection;
protected String command = new String();
protected String responseString = new String();


socket = new ServerSocket(port);


while(true)
{
    // open socket
    connection = socket.accept();
    // get input reader
    InputStreamReader inputStream = new InputStreamReader(connection.getInputStream());
    BufferedReader input = new BufferedReader(inputStream);
    // get output handler
    DataOutputStream response = new DataOutputStream(connection.getOutputStream());

    // get input
    command = input.readLine();

    // process input
    Logger.log("Command: " + command);
    responseString = command + " MC2 It Works!";

    // send response
    response.writeBytes(responseString);
    response.flush();
    response.close();
}

PHP Client: PHP客户端:

$address = 'example.com'; // obviously not the address I am using
$port = 9005;
$message = "Test";

$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));

socket_connect($socket, $address, $port)

if(socket_send($socket, $message, strlen($message), MSG_EOF) != FALSE)
{
    echo socket_read($socket, 14, PHP_NORMAL_READ);
}

SOLVED: Here is the solution I figured out what the problem was. 解决:这是我找出问题所在的解决方案。 When reading data back from the Java server, I was only reading one chunk and everything was hanging on the rest. 当从Java服务器读取数据时,我只读了一个块,其余的都挂了。 The data needs to be read in a loop to get all of it. 需要在循环中读取数据才能获得所有数据。

Also The data I was sending to the Java Server was not ending in a new line or carriage return. 我发送给Java Server的数据也没有以新行或回车结束。

the following re-written PHP client now make the entire thing work nicely 以下重写的PHP客户端现在使整个过程很好地工作

$address = 'minecraft.kayoticgamer.com';
$port = 9005;

$socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
socket_connect($socket, $address, $port);

$message .= chr(10);

$status = socket_sendto($socket, $message, strlen($message), MSG_EOF, $address, $port);
if($status !== FALSE)
{
    $message = '';
    $next = '';
    while ($next = socket_read($socket, 4096))
    {
        $message .= $next;
    }

    echo $message;
}
else
{
    echo "Failed";
}

socket_close($socket);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM