httpを使ってphpサーバに通信するクライアント側のJavaアプリケーションを作成しました。私はPHPサーバーによって行われた要求に応答するために、Java(クライアント)側でリスナーを実装する必要があります。現在のところ、javaアプリケーションは毎分更新されるサーバー上のテキストファイルに当たっています。java php communication
これはうまくいきましたが、クライアントJavaアプリケーションの数が増えており、このプリミティブなシステムが壊れ始めています。
これを変更するにはどうすればよいですか?私はjavaクライアントアプリケーションでJava ServerSocketリスナーを試しましたが、動作させることはできません。私はコミュニケーションを完了するのに問題があります。 Web上のすべての例は、IPアドレスの例としてlocalhostを使用しています。私のPHPサーバはリモートホストされています。
クライアントマシンのIPアドレスを取得してphpサーバに送信する必要がありますので、phpはメッセージの送信先を知りますか?ここでは、Javaコードがある...これは...、すべてWeb上で
public class MyJavaServer
{
public static void main(String[] args)
{
int port = 4444;
ServerSocket listenSock = null; //the listening server socket
Socket sock = null; //the socket that will actually be used for communication
try
{
System.out.println("listen");
listenSock = new ServerSocket(port);
while (true)
{
sock = listenSock.accept();
BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
String line = "";
while ((line = br.readLine()) != null)
{
bw.write("PHP said: " + line + "\n");
bw.flush();
}
//Closing streams and the current socket (not the listening socket!)
bw.close();
br.close();
sock.close();
}
}
catch (IOException ex)
{
System.out.println(ex);
}
}
}
です...と、ここでこれは、単に動作しないPHP
$PORT = 4444; //the port on which we are connecting to the "remote" machine
$HOST = "ip address(not sure here)"; //the ip of the remote machine(of the client java app's computer???
$sock = socket_create(AF_INET, SOCK_STREAM, 0)
or die("error: could not create socket\n");
$succ = socket_connect($sock, $HOST, $PORT)
or die("error: could not connect to host\n");
$text = "Hello, Java!\n"; //the text we want to send to the server
socket_write($sock, $text . "\n", strlen($text) + 1)
or die("error: failed to write to socket\n");
$reply = socket_read($sock, 10000, PHP_NORMAL_READ)
or die("error: failed to read from socket\n");
echo $reply;
です。 Javaアプリケーションはリッスンしますが、PHPスクリプトは決して接続しません。
また、これは私のニーズに最も適した方法ですか?おかげさまで
着信要求をブロックしているファイアウォールがあるかどうかを確認しましたか? – Asaph
私の家のコンピュータ。私は通常のワイヤレスルータを経由しています。ところで、もしファイアウォールがこれを殺すならば、それは間違った方法かもしれません。私のクライアントは技術に精通していないので、ユーザーにとっては簡単な解決策が必要です。 – rob345
ファイアウォールにアクセスできない可能性があります。それは、ケーブルモデムレベルおよび/またはさらに上流にある可能性がある。そして、はい、ファイアウォールはおそらくあなたのデザインの障壁になるでしょう。あなたはデザインを考え直すべきです。クライアントにデータをプッシュするのではなく、クライアントが更新をサーバーにポーリングするようにします。 – Asaph