Use php to read the data of the socket interface, pass the request method and request parameters through php, and get the return result
PHP file:
<?<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Test{ </span><span style="color: #0000ff;">const</span> IP='127.0.0.1'<span style="color: #000000;">; </span><span style="color: #0000ff;">const</span> port=10003<span style="color: #000000;">; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> main(){ </span><span style="color: #008080;">header</span>("Content-type:text/html;Charset=UTF-8"<span style="color: #000000;">); </span><span style="color: #800080;">$socket</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Test(); </span><span style="color: #800080;">$result</span>=<span style="color: #800080;">$socket</span>->connSocket("getmaillist\r\n{'id':2}\r\n"<span style="color: #000000;">); </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$result</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span> connSocket(<span style="color: #800080;">$str</span><span style="color: #000000;">){ </span><span style="color: #800080;">$socket</span> = socket_create(AF_INET,SOCK_STREAM,<span style="color: #000000;">SOL_TCP); </span><span style="color: #800080;">$res</span>=@socket_connect(<span style="color: #800080;">$socket</span>,self::IP,self::<span style="color: #000000;">port); </span><span style="color: #0000ff;">if</span>(!<span style="color: #800080;">$res</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">return</span><span style="color: #000000;">; } socket_write(</span><span style="color: #800080;">$socket</span>,<span style="color: #800080;">$str</span><span style="color: #000000;">); </span><span style="color: #800080;">$result</span>=""<span style="color: #000000;">; </span><span style="color: #0000ff;">while</span>(<span style="color: #800080;">$data</span> = socket_read(<span style="color: #800080;">$socket</span>,1024<span style="color: #000000;">)){ </span><span style="color: #800080;">$result</span>.=<span style="color: #800080;">$data</span><span style="color: #000000;">; } socket_close(</span><span style="color: #800080;">$socket</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$result</span><span style="color: #000000;">; } } Test</span>::main();
Java Socket server:
<span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.InputStream; </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.io.PrintWriter; </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.net.ServerSocket; </span><span style="color: #0000ff;">import</span><span style="color: #000000;"> java.net.Socket; </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Test { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> main(String[] args) <span style="color: #0000ff;">throws</span><span style="color: #000000;"> Exception { ServerSocket serverSocket </span>= <span style="color: #0000ff;">new</span> ServerSocket(10003<span style="color: #000000;">); </span><span style="color: #0000ff;">while</span> (<span style="color: #0000ff;">true</span><span style="color: #000000;">) { Socket socket </span>=<span style="color: #000000;"> serverSocket.accept(); InputStream is </span>=<span style="color: #000000;"> socket.getInputStream(); </span><span style="color: #0000ff;">byte</span>[] b = <span style="color: #0000ff;">new</span> <span style="color: #0000ff;">byte</span>[1024<span style="color: #000000;">]; </span><span style="color: #0000ff;">int</span> len =<span style="color: #000000;"> is.read(b); String inputString </span>= <span style="color: #0000ff;">new</span> String(b, 0<span style="color: #000000;">, len); PrintWriter pw</span>=<span style="color: #0000ff;">new</span> PrintWriter(socket.getOutputStream(),<span style="color: #0000ff;">true</span><span style="color: #000000;">); String result</span>=""<span style="color: #000000;">; </span><span style="color: #008000;">//</span><span style="color: #008000;">处理发来的数据</span> <span style="color: #0000ff;">if</span>(inputString.contains("\r\n"<span style="color: #000000;">)){ String[] params</span>=inputString.split("\r\n"<span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(params[0].equals("getmaillist"<span style="color: #000000;">)){ String maillist</span>=<span style="color: #000000;">getmaillist(); result</span>="请求方法:"+params[0]+",请求参数:"+params[1]+",请求结果:"+<span style="color: #000000;">maillist; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ result</span>="非法参数2"<span style="color: #000000;">; } }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ result</span>="非法参数1"<span style="color: #000000;">; } pw.println(result); socket.close(); } } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span><span style="color: #000000;"> String getmaillist(){ </span><span style="color: #0000ff;">return</span> "中国上海/2015年7月28日——服务于中国及全球华人社群的领先在线媒体公司新浪公司(Nasdaq GS: SINA)定于美国当地时间2016年8月8日周一股市收盘后公布截至2016年6月30日的2016第二季度未经审计的财务报告。随后,新浪管理团队将于美国东部时间晚10点10分召开电话会议,通报公司的财务和经营状况。"<span style="color: #000000;">; } }</span>
Effect: