PHP Request Socket Interface Test Instance _php instance

Source: Internet
Author: User
Tags local time

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

PHP File:

<?php
class 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, 1024)) {
      $result. = $data;
    }
    Socket_close ($socket);
    return $result;
  }
Test::main ();

Java Socket Service side:

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 Server

    Socket (10003);

      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 "China Shanghai/July 28, 2015--the leading online media company that serves China and the global Chinese community, Sina (Nasdaq Gs:sina), is scheduled for 2016 local time in the United States. August 8 Monday After the close of the stock market closed to the second quarter as at June 30, 2016 of the 2016 unaudited financial report. Subsequently, the Sina Management team will convene 10:10 P.M. Eastern Time conference call to inform the Company's financial and operating conditions.
  "; }
}

Effect:

The above PHP request socket Interface Test example is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.