简体   繁体   English

PHP客户端和Java Server之间的客户端 - 服务器网络

[英]Client-Server Networking Between PHP Client and Java Server

I have a university project which is already 99% completed. 我有一个已完成99%的大学项目。 It consists of two parts-website (PHP) and desktop (Java). 它由两部分组成 - 网站(PHP)和桌面(Java)。

People have their accounts on the website and they wish to query different information regarding their accounts. 人们在网站上拥有自己的帐户,他们希望查询有关其帐户的不同信息。 They send an SMS which is received by desktop application which queries database of website (MySQL) and sends the reply accordingly. 他们发送一个由桌面应用程序接收的短信,该短信查询网站(MySQL)的数据库并相应地发送回复。 This part is working superbly. 这部分工作得非常好。 The problem is that some times website wishes to instruct the desktop application to send a specific SMS to a particular number. 问题在于,有时网站希望指示桌面应用程序将特定SMS发送到特定号码。 Apparently there seems no way other than putting all the load to the DB server... This is how I made it work. 显然除了把所有负载都放到数据库服务器之外似乎没办法......这就是我使它工作的方式。 Website puts SMS jobs in a specific table. 网站将SMS作业放在特定的表格中。 Java application polls this table again and again and if it finds a job, it executes it. Java应用程序一次又一次地轮询该表,如果它找到了一个作业,它就会执行它。 Even this part is working correctly but unfortunately it is not acceptable by my university to poll the DB like this . 即使这部分工作正常,但不幸的是,我的大学不能接受这样的DB调查 :( :(

The other approach I could think of is to use client-server one. 我能想到的另一种方法是使用客户端 - 服务器。 I tried making Java server and its PHP client. 我尝试制作Java服务器及其PHP客户端。 So that whenever an SMS is to be sent, the website opens a socket connection to desktop application and sends two strings (cell # and SMS message). 因此,无论何时发送SMS,网站都会打开与桌面应用程序的套接字连接并发送两个字符串(单元格#和SMS消息)。 Unfortunately I am unable to do this. 不幸的是我无法做到这一点。 I was successfully to make a Java server which works fine when connected by a Java client, similarly my PHP client connects correctly to a PHP server, but when I try to cross them, they start hating each other... PHP shows no error but Java gives StreamCorruptedException when it tries to read header of input stream . 我成功地创建了一个Java服务器,当它通过Java客户端连接时工作正常,类似我的PHP客户端正确连接到PHP服务器,但当我尝试跨越它们时,他们开始互相讨厌... PHP没有显示错误但是Java在尝试读取输入流的标头时给出StreamCorruptedException

Could someone please tell what I can try to make PHP client and Java server work together? 有人可以告诉我可以尝试让PHP客户端和Java服务器一起工作吗? Or if the said purpose can be achieved by another means, how? 或者,如果上述目的可以通过其他方式实现,如何实现?

Regards, Yasir 此致,Yasir

Wait... are you using object streams? 等等......你在使用对象流吗? According to the java documentation StreamCorruptedException is "Thrown when control information that was read from an object stream violates internal consistency checks." 根据java文档,StreamCorruptedException是“当从对象流中读取的控件信息违反内部一致性检查时抛出”。 I doubt your PHP app is sending what Java considers a serialized object. 我怀疑你的PHP应用程序正在发送Java认为是序列化对象的东西。 Why don't you go low-tech and read a string? 你为什么不去低科技并读一个字符串? The following had worked for me back in the day: 以下在当天对我有用:

       ServerSocket serverSocket = new ServerSocket(port);
       Socket clientSocket = serverSocket.accept();
       BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

       while((inputLine = in.readLine())!=null)
      {
        //Do whatever
      }

You might try looking into Quercus. 您可以尝试研究Quercus。 It's a server that runs PHP inside java. 它是一个在java中运行PHP的服务器。 You can call java called directly from PHP as if it was native PHP functions. 您可以直接从PHP调用java调用,就好像它是本机PHP函数一样。 You won't have to worry about streams then. 那么你不必担心流。

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

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