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.