http를 통해 PHP 서버에 통신하는 클라이언트 측 Java 응용 프로그램을 작성했습니다. php 서버에 의해 만들어진 요청에 응답하기 위해 자바 (클라이언트) 측에 리스너를 구현해야합니다. 현재 자바 애플리케이션은 1 분마다 업데이트되는 서버상의 텍스트 파일을 치고 있습니다.java php communication
이 작업은 정상적으로 수행되었지만 이제는 클라이언트 Java 응용 프로그램의 수가 증가하고 있으며이 원시 시스템은 고장입니다.
이것을 변경하는 가장 좋은 방법은 무엇입니까? Java 클라이언트 응용 프로그램에서 Java ServerSocket 수신기를 시도했지만 작동하지 않습니다. 의사 소통을 완료하는 데 문제가 있습니다. 웹상의 모든 예제는 localhost를 IP 주소 예제로 사용하고, PHP 서버는 원격 호스트합니다.
클라이언트 컴퓨터의 IP 주소를 가져 와서 php 서버로 보내면 php가 메시지를 보낼 위치를 알 수 있습니까? 여기에 자바 코드는 ...이 ... 모든 웹을 통해
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
내 집 컴퓨터. 나는 정상적인 무선 라우터를 통과 할 것이다. BTW, 방화벽이 이것을 죽이려고한다면, 잘못된 길일 수 있습니다. 내 고객은 기술적 인 지식이 없기 때문에 사용자를위한 쉬운 솔루션이 필요합니다. – rob345
아마도 액세스를 차단하는 방화벽이있을 것입니다. 그것은 케이블 모뎀 레벨 및/또는 더 상류에있을 수 있습니다. 그리고 예, 방화벽은 설계상의 장벽이 될 것입니다. 디자인을 재고해야합니다. 데이터를 클라이언트에 푸시하는 대신 클라이언트가 서버를 폴링하여 업데이트를 확인하십시오. – Asaph