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.