> php教程 > php手册 > [PHP] PHP 요청 소켓 인터페이스 테스트

[PHP] PHP 요청 소켓 인터페이스 테스트

WBOY
풀어 주다: 2016-08-04 08:53:15
원래의
1264명이 탐색했습니다.

PHP를 사용하여 소켓 인터페이스의 데이터를 읽고, PHP를 통해 요청 메소드와 요청 매개변수를 전달하고, 반환 결과를 가져옵니다

PHP 파일:

<?<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 소켓 서버:

<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>
로그인 후 복사

효과:

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿