In order to realize the communication between PHP and Servlet, this experiment took a lot of trouble ... And the online ready-made information is incomplete, with an excited mood will be encountered in this experiment errors and solutions to share with you:
PHP as client side, Servlet as server side
On the server side, the general practice is to:
1. Write a servlet class and set the servlet to the class that is loaded when the server starts.
Login
Servlet.loginservlet
0
2. If you write the server-side code separately in the Init () method of the Servelt, and listen
while (true) {
Socket = Server.accept ();
}
Causes the Tomcat boot timeout to occur.
The correct approach should be to write a listener handler thread class Socketserver separately, and then Init () to start the thread in a multithreaded manner:
public void init (ServletConfig config) {
New Thread () {
public void Run () {
try{
ServerSocket Server =new ServerSocket (4700);
Socket socket = NULL;
while (true) {
Socket=server.accept ();
Socketserver sserver=new socketserver (socket);
Sserver.start ();
}
}catch (IOException e) {
System.out.println (E.getmessage ());}
}
}.start ();
}
public class Socketserver extends thread{
private socket socket;
Public socketserver (socket socket) {
This.socket = socket;
}
public void Run () {
String output = "";
try {
BufferedReader is=new BufferedReader (New InputStreamReader (Socket.getinputstream ()));
PrintWriter os=new PrintWriter (Socket.getoutputstream ());
String line = null;
line = Is.readline ();
System.out.println (line);
Output = "Server Send";
OS.PRINTLN (output);
Os.flush ();
Is.close ();
Os.close ();
Socket.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Once on the client side, receive the server side send past data, carefully check the code, found no Os.flush ();
By starting a new thread in the Init () method, and then starting a new thread for each request through the new threads to respond to the socket request from the client side
On the client side, written in PHP,
The client sends the data to the server and then receives the server-side return data for display.
$userName = ' veverrr ';
$password = ' swjtu ';
$write _str = ";
if ($userName = = NULL | | $password = = NULL) {
Jump back to login system
}
$socket _host = ' localhost ';
$socket _port = 4700;
$socket = Socket_create (Af_inet, Sock_stream, sol_tcp);
$connection = Socket_connect ($socket, $socket _host, $socket _port);
$write _str. = $userName. $password. chr (13). Chr (10);
if (!socket_write ($socket, $write _str,strlen ($write _str))) {
echo "Connect write";
}
$return _str = Socket_read ($socket, 1024);
echo $return _str;
Socket_close ($socket);
When the server was using Is.readline (), it got stuck directly, and later found the method on the foreign language website, and a trial passed.
It is mainly the data sent by client side with Chr. chr (10) indicates that the carriage return and the line are converted into a format that Java can recognize.
PHP and servlet communication all the process and the attention point is so much.
Excerpt from Veverrr's column
http://www.bkjia.com/PHPjc/478482.html www.bkjia.com true http://www.bkjia.com/PHPjc/478482.html techarticle in order to realize the communication between PHP and Servlet, this experiment took a lot of trouble ... And the online ready-made information is incomplete, with an excited mood will be encountered in this experiment error ...