[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: