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:
<?PHPclasstest{Constip= ' 127.0.0.1 '; Constport=10003; Public Static functionMain () {Header("content-type:text/html; Charset=utf-8 "); $socket=NewTest (); $result=$socket->connsocket ("getmaillist\r\n{' id ': 2}\r\n"); Echo $result; } Private functionConnsocket ($str){ $socket= Socket_create (Af_inet,sock_stream,sol_tcp); $res[Email Protected]_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 ();
Socket server for Java:
ImportJava.io.InputStream;ImportJava.io.PrintWriter;ImportJava.net.ServerSocket;ImportJava.net.Socket; Public classTest { Public Static voidMain (string[] args)throwsException {serversocket serversocket=NewServerSocket (10003); while(true) {Socket Socket=serversocket.accept (); InputStream is=Socket.getinputstream (); byte[] B =New byte[1024]; intLen =Is.read (b); String inputstring=NewString (b, 0, Len); PrintWriter PW=NewPrintWriter (Socket.getoutputstream (),true); String result=""; //processing the data that is sent 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 StaticString getmaillist () {return"Shanghai, China/July 28, 2015 – The leading online media company in China and the global Chinese Community Sina Corporation (Nasdaq Gs:sina) 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:
[PHP] PHP Request Socket Interface Test