PHP Request Socket Interface Test example detailed

Source: Internet
Author: User
Tags local time
This article mainly introduces the PHP request socket Interface Test example detailed, interested in the friend's reference, I hope to help you.

Using PHP to read the data of the socket interface, pass the request method and request parameters through PHP, get the return result

PHP Files:

<?phpclass test{  const ip= ' 127.0.0.1 ';  Const PORT=10003;  public static function main () {    header ("content-type:text/html; Charset=utf-8 ");    $socket =new Test ();    $result = $socket->connsocket ("getmaillist\r\n{' id ': 2}\r\n");    echo $result;  }  Private Function Connsocket ($str) {    $socket = socket_create (af_inet,sock_stream,sol_tcp);    $res = @socket_connect ($socket, self::ip,self::p ort);    if (! $res) {      return;    }    Socket_write ($socket, $str);    $result = "";    while ($data = Socket_read ($socket, 1024x768)) {      $result. = $data;    }    Socket_close ($socket);    return $result;  }} Test::main ();

Socket server for Java:

Import Java.io.inputstream;import java.io.printwriter;import Java.net.serversocket;import java.net.Socket;public Class Test {public static void main (string[] args) throws Exception {ServerSocket serversocket = new ServerSocket (10    003);      while (true) {Socket socket = serversocket.accept ();      InputStream is = Socket.getinputstream ();      Byte[] B = new byte[1024];      int len = Is.read (b);      String inputstring = new String (b, 0, Len);      PrintWriter pw=new PrintWriter (Socket.getoutputstream (), true);            String result= "";        Processing the sent data if (Inputstring.contains ("\ r \ n")) {string[] Params=inputstring.split ("\ r \ n");          if (Params[0].equals ("Getmaillist")) {String maillist=getmaillist ();        result= "Request method:" +params[0]+ ", request parameter:" +params[1]+ ", request result:" +maillist;        }else{result= "illegal parameter 2";      }}else{result= "illegal parameter 1";      } pw.println (Result);    Socket.close (); }} public static String getmaillist (){return "Shanghai, China/July 28, 2015 – Sina (NASDAQ Gs:sina), a leading online media company serving China and the global Chinese community Unaudited financial report for the second quarter of 2016, scheduled for Monday, after the close of the stock market closed on August 8, 2016 in the U.S. local time. Then, the Sina management team will hold a conference call at 10:10 P.M. EST to inform the company's financial and business status.  "; }}

Effect:

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.