[PHP] PHP request Socket interface test, phpsocket

Source: Internet
Author: User

[PHP] PHP request Socket interface test, phpsocket

Use php to read data from the socket interface, and pass the Request Method and request parameters through php to obtain the returned results.

PHP file:

<?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::port);        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 Server:

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 (10003); while (true) {Socket 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 = ""; // process 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 =" invalid parameter 2 ";}} else {result =" invalid parameter 1 ";} pw. println (result); socket. Close () ;}} public static String getmaillist () {return "China Shanghai/September July 28, 2015 -- SINA Corporation (Nasdaq GS: SINA), a leading online media company serving Chinese and global Chinese communities) the unaudited financial report for the second quarter of 2016, ending on Monday, us time, is scheduled to be released. Then, the Sina management team will hold a conference call at a.m. eastern United States time to inform the company of its financial and operating status. ";}}

Effect:

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.