drp935159
2013-01-14 01:02
浏览 35
已采纳

使PHP服务器读取基于Java的客户端

public class Client {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {
    Socket sock = new Socket();
    sock.connect(new InetSocketAddress(InetAddress.getLocalHost(), 43594));

    DataOutputStream out = new DataOutputStream(sock.getOutputStream());
    DataInputStream in = new DataInputStream(sock.getInputStream());

    out.writeByte(255);
    out.writeLong(133713371337L);

    System.out.println("Response: " + in.readByte() + ", " + in.readLong());

    sock.close();
}

and the PHP server meant to read and write back to the java client:

<?php
require 'Stream.php';
$stream = new Stream();

$address = '127.0.0.1';
$port = 43594;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, 0, $port) or die('Could not bind to address');
socket_listen($sock);
echo "Server listening on port " . $port . "...";
$client = socket_accept($sock);

$data = socket_read($client, 1, PHP_BINARY_READ);
$byte_array = unpack('C*', $data);
$stream->setStream($byte_array);
$b2 = $stream->readUnsignedByte();

$l1 = socket_read($client, $stream->readQWord());
echo $b1 . " " . $l1;
socket_write($client, 0);
socket_write($client, $stream->writeQWord(2148000000));

The stream class being used: http://pastebin.com/p1vc9aPG

I'm trying to write a client in java that contacts, sends and reads data to/from the server. The server is written in PHP and ran via cli, while the client is written in java. Please help if you can, there probably aren't many people who can help with this issue.

The protocol is already established and I'm just trying to implement it in PHP. I can't seem to read/write back to the java client from the PHP page.

The client side in Java is defined how I want it to be. What I'm looking for is the equivalent server in PHP and I can't seem to get it to work.

2条回答 默认 最新

相关推荐 更多相似问题