java 服务器和 php 客户端之间的 SocketServer 通信
SocketServer Communication between java server and php client
我正在尝试在 Java 中的 SocketServer(服务器)和 php 中的 Socket(客户端)之间建立通信。
客户端能够连接到主机,客户端能够发送消息并且服务器成功读取消息。但是问题出现在SocketServer向Client写入的时候,客户端收不到服务器的消息。
我已经阅读了关于同一场景的其他问题(java-php 套接字通信),但我似乎无法找到导致问题的原因。
如果我使用 Java 套接字作为客户端,那么双向通信都可以完美地进行。
服务器:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
protected ServerSocket socket;
protected final int port = 9005;
protected Socket connection;
protected String command = new String();
protected String responseString = new String();
public void init(){
System.out.println( "Launching Server: " );
try{
socket = new ServerSocket(port);
while(true)
{
// open socket
connection = socket.accept();
System.out.println( "Client Connected " );
// get input reader
InputStreamReader inputStream = new InputStreamReader(connection.getInputStream());
BufferedReader input = new BufferedReader(inputStream);
// get input
command = input.readLine();
// process input
System.out.println("Command: " + command);
responseString = command + " MC2 It Works!";
// get output handler
PrintStream response = new PrintStream(connection.getOutputStream());
// send response
response.println(responseString);
}
}
catch (IOException e){
e.printStackTrace();
}
}
}
客户:
class Client {
private $address;
private $port;
public function __construct($address, $port){
$this->address = $address;
$this->port = $port;
$this->init();
}
private function init(){
//create socket
if(! $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'))){
$this->showError("socket create");
};
//establish connection
socket_connect($socket, $this->address, $this->port);
//write to server
$message = "I am a client";
socket_write($socket, $message, strlen($message)); //Send data
echo "Listening to Server\n";
//read from server
if(!$reponse = socket_read($socket, 2048, PHP_NORMAL_READ)){
$this->showError("socket read");
}
//print response
echo "Response from server------------------\n";
echo $reponse;
socket_close($socket);
}
private function showError($message){
echo ("Error: ".$message);
exit(666);
}
}
$address="localhost";$port=9005;
echo "Testing Client Server\n";
$client = new Client($address, $port);
有人可以指导我解决这里的问题吗?
在服务器端,代码需要一行(以换行符结束),在php 你发送
socket_write($socket, $message, strlen($message));
请相应地检查您发送的数据,确保您发送了换行符。
我正在尝试在 Java 中的 SocketServer(服务器)和 php 中的 Socket(客户端)之间建立通信。 客户端能够连接到主机,客户端能够发送消息并且服务器成功读取消息。但是问题出现在SocketServer向Client写入的时候,客户端收不到服务器的消息。
我已经阅读了关于同一场景的其他问题(java-php 套接字通信),但我似乎无法找到导致问题的原因。
如果我使用 Java 套接字作为客户端,那么双向通信都可以完美地进行。
服务器:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Server {
protected ServerSocket socket;
protected final int port = 9005;
protected Socket connection;
protected String command = new String();
protected String responseString = new String();
public void init(){
System.out.println( "Launching Server: " );
try{
socket = new ServerSocket(port);
while(true)
{
// open socket
connection = socket.accept();
System.out.println( "Client Connected " );
// get input reader
InputStreamReader inputStream = new InputStreamReader(connection.getInputStream());
BufferedReader input = new BufferedReader(inputStream);
// get input
command = input.readLine();
// process input
System.out.println("Command: " + command);
responseString = command + " MC2 It Works!";
// get output handler
PrintStream response = new PrintStream(connection.getOutputStream());
// send response
response.println(responseString);
}
}
catch (IOException e){
e.printStackTrace();
}
}
}
客户:
class Client {
private $address;
private $port;
public function __construct($address, $port){
$this->address = $address;
$this->port = $port;
$this->init();
}
private function init(){
//create socket
if(! $socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'))){
$this->showError("socket create");
};
//establish connection
socket_connect($socket, $this->address, $this->port);
//write to server
$message = "I am a client";
socket_write($socket, $message, strlen($message)); //Send data
echo "Listening to Server\n";
//read from server
if(!$reponse = socket_read($socket, 2048, PHP_NORMAL_READ)){
$this->showError("socket read");
}
//print response
echo "Response from server------------------\n";
echo $reponse;
socket_close($socket);
}
private function showError($message){
echo ("Error: ".$message);
exit(666);
}
}
$address="localhost";$port=9005;
echo "Testing Client Server\n";
$client = new Client($address, $port);
有人可以指导我解决这里的问题吗?
在服务器端,代码需要一行(以换行符结束),在php 你发送
socket_write($socket, $message, strlen($message));
请相应地检查您发送的数据,确保您发送了换行符。