PHP and Servlet communication through Socket _ PHP Tutorial

Source: Internet
Author: User
PHP and Servlet communicate through Socket. This experiment costs a lot to implement PHP and Servlet communication ........ In addition, the existing materials on the Internet are incomplete. I am excited to take the errors encountered in this experiment. in order to realize the communication between PHP and Servlet, I have paid a lot of attention ........ In addition, the existing materials on the Internet are incomplete. I am excited to share with you the errors and solutions encountered in this experiment:

PHP acts as the Client, Servlet acts as the Server

On the Server side, the general practice is:

1. write a Servlet class and set the Servlet as the class loaded when the server starts.

Login

Servlet. LoginServlet

0

2. if the Server-side code is compiled separately in the init () method of the Servelt and the Server-side code is listened

While (true ){

Socket = server. accept ();

}

The Tomcat startup times out.

The correct method should be to write a listening processing thread-class SocketServer separately, and then start the thread in init () using multiple threads:

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, I could not receive the data sent by the Server. I checked the code carefully and found that OS. flush () was not found ();

Start a new thread in the init () method, and then start a new thread for each request through the new thread to respond to the Socket request from the Client.

Written in PHP on the Client side,

The Client sends data to the Server first, then receives the data returned by the Server for display.

$ UserName = 'veverrr ';

$ Password = 'swjtu ';

$ Write_str = '';

If ($ userName = null | $ password = null ){

// Jump back to the logon 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 ($ sockets, 1024 );

Echo $ return_str;

Socket_close ($ socket );

When the Server uses is. readline (), it gets stuck directly. later, it finds a method on the foreign language website and passes the experiment.

It is mainly to add chr (13). chr (10) after the data sent by the Client, which means to press enter first and then wrap it into a format that can be recognized by java.

There are so many processes and notes for communication between PHP and Servlet.

From the column of veverrr

It takes a lot of weeks for the producer to communicate with the Servlet ........ In addition, the existing materials on the Internet are incomplete, so I am excited to learn about the mistakes I encountered in this experiment...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.