我简单说吧,我用JAVA做了个socket服务端,用PHP去链接它,问题出来了:
每次只要从PHP端传到服务器端数据后,再读取从服务器端发来的数据就读不了了。为什么
也就是
fputs()后再fgets()就读不了了,超时了。
为什么,在线等,这问题困扰我很长时间了。

解决方案 »

  1.   

    $fp = fsockopen("192.168.0.119",5000,$errno,$errstr,30);
    stream_set_timeout($fp,30);
    stream_set_blocking( $fp , true );
    if( !$fp )
    {
    die("error ".$errstr." ".$errno);
    }
    else
    {
    echo ($tmp = fgets($fp))."<br>";
    $status = stream_get_meta_data( $fp ) ;
        //发送数据超时
        if( $status['timed_out'] )
        {
            echo "read 1 time out" ;
            fclose( $fp );
            die();
        } echo "link success!"."<br>";
    }

    fputs($fp,"order1");
    //echo "fputs order1"; $tmp = fgets($fp); $status = stream_get_meta_data( $fp );
        //发送数据超时
        if( $status['timed_out'] )
        {
            echo "read 2 time out" ;
            fclose( $fp );
            die();
        }
      

  2.   


    import java.net.*;
    import java.io.*;
    import java.sql.*;
    import org.gjt.mm.mysql.Driver;public class soa_server extends Thread
    {
    private Socket server_socket;
    private int server_id;
    private String drivename;
    Connection connection;
    Statement statement;
    ResultSet resultset;

    public soa_server( Socket tmp_s , int id)
    {
    this.server_socket = tmp_s;
    this.server_id = id;
    this.drivename = "org.gjt.mm.mysql.Driver";
    try
    {
    Class.forName(this.drivename).newInstance();
    //System.out.println("test");
    String url = "jdbc:mysql://localhost:3306/cww";
    this.connection = DriverManager.getConnection(url,"root","");
    if( this.connection == null )
    {
    System.out.println("Connection is NULL");
    }
    this.statement = null;
    this.resultset = null;
    }
    catch( Exception e)
    {
    System.out.println("Connection new is faile");
    }
    }

    public void protocol_introduce()
    {
    System.out.println("char \"order1\" for query");
    System.out.println("char \"order2\" for insert");
    System.out.println("char \"order3\" for update");
    System.out.println("char \"order4\" for delete");
    } public void run()
    {
    // if( this.server_socket = null )
    // return;
    try
    {
    System.out.println("Socket["+this.server_id+"] connection success.");
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter( this.server_socket.getOutputStream())),true);
    out.println("Socket["+this.server_id+"] welcome client");
    //out.flush();
    out.println("Socket["+this.server_id+"]2 welcome client");
    protocol_introduce();
    BufferedReader in = new BufferedReader( new InputStreamReader( this.server_socket.getInputStream())); while( true )
    {
    // String ss = in.readLine();
    String ss = "order1";
    if( ss.compareTo("order1") >= 0 )
    {
    System.out.println("in");
    this.statement = this.connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

    String sql = "select * from articles";
    this.resultset = this.statement.executeQuery(sql);
    if( this.resultset.next() )
    {
    System.out.println("in");
    String tmp = this.resultset.getString("id");
    System.out.println(tmp);
    out.println( tmp );
    System.out.println("Output the id!");
    }
    }
    System.out.println("Socket["+this.server_id+"] receives: "+ss);
    }
    }
    catch( Exception e )
    {
    System.out.println("Something wrong happens.");
    }
    finally
    {
    System.out.println("Socket["+this.server_id+"] is closing.");
    try
    {
    this.server_socket.close();
    }
    catch( Exception e)
    {
    System.out.println("When the socket close something is wrong!");
    }
    }
    }

    public static void main( String args[] )
    {
    int server_num = 1;
    ServerSocket server = null;
    soa_server tmp = null;
    try
    {
    server = new ServerSocket(5000);
    System.out.println("Server start!");
    }
    catch( Exception e)
    {

    }
    while( true )
    {
    try
    {
    Socket s = server.accept();
    tmp = new soa_server(s,server_num++);
    tmp.start();
    }
    catch( Exception e)
    {

    }
    }

    }

    }
      

  3.   

    第2个发错了
    重发遍
    import java.net.*;
    import java.io.*;
    import java.sql.*;
    import org.gjt.mm.mysql.Driver;public class soa_server extends Thread
    {
    private Socket server_socket;
    private int server_id;
    private String drivename;
    Connection connection;
    Statement statement;
    ResultSet resultset;

    public soa_server( Socket tmp_s , int id)
    {
    this.server_socket = tmp_s;
    this.server_id = id;
    this.drivename = "org.gjt.mm.mysql.Driver";
    try
    {
    Class.forName(this.drivename).newInstance();
    //System.out.println("test");
    String url = "jdbc:mysql://localhost:3306/cww";
    this.connection = DriverManager.getConnection(url,"root","");
    if( this.connection == null )
    {
    System.out.println("Connection is NULL");
    }
    this.statement = null;
    this.resultset = null;
    }
    catch( Exception e)
    {
    System.out.println("Connection new is faile");
    }
    }

    public void protocol_introduce()
    {
    System.out.println("char \"order1\" for query");
    System.out.println("char \"order2\" for insert");
    System.out.println("char \"order3\" for update");
    System.out.println("char \"order4\" for delete");
    } public void run()
    {
    // if( this.server_socket = null )
    // return;
    try
    {
    System.out.println("Socket["+this.server_id+"] connection success.");
    PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter( this.server_socket.getOutputStream())),true);
    out.println("Socket["+this.server_id+"] welcome client");
    protocol_introduce();
    BufferedReader in = new BufferedReader( new InputStreamReader( this.server_socket.getInputStream())); while( true )
    {
    String ss = in.readLine();
    if( ss.compareTo("order1") >= 0 )
    {
    System.out.println("in");
    this.statement = this.connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

    String sql = "select * from articles";
    this.resultset = this.statement.executeQuery(sql);
    if( this.resultset.next() )
    {
    System.out.println("in");
    String tmp = this.resultset.getString("id");
    System.out.println(tmp);
    out.println( tmp );
    System.out.println("Output the id!");
    }
    }
    System.out.println("Socket["+this.server_id+"] receives: "+ss);
    }
    }
    catch( Exception e )
    {
    System.out.println("Something wrong happens.");
    }
    finally
    {
    System.out.println("Socket["+this.server_id+"] is closing.");
    try
    {
    this.server_socket.close();
    }
    catch( Exception e)
    {
    System.out.println("When the socket close something is wrong!");
    }
    }
    }

    public static void main( String args[] )
    {
    int server_num = 1;
    ServerSocket server = null;
    soa_server tmp = null;
    try
    {
    server = new ServerSocket(5000);
    System.out.println("Server start!");
    }
    catch( Exception e)
    {

    }
    while( true )
    {
    try
    {
    Socket s = server.accept();
    tmp = new soa_server(s,server_num++);
    tmp.start();
    }
    catch( Exception e)
    {

    }
    }

    }

    }
      

  4.   

    问题就在while( true )上,连接一直在,处理完服务器端要断开连接,PHP在服务器端断开连接才会执行完毕,否则会一直等下去的。毕竟是HTTP协议,一个PHP页面不能一直保持和服务器的连接的。我在做PHP连接VC编写的SOCKET服务器端也遇到了这样的问题。
      

  5.   

    试了一下。。客户端一样的PHP代码,我用python也是开线程.放在while true里就可以取到fget,java放进去就不行,只能放在while true外面- -|
      

  6.   

    厄……python也不是每次都可以fgets,成功率50%左右。。
      

  7.   


    有断开的fclose();不是这个问题
      

  8.   


    我发现,是不是php里的Socket连接读取缓存就是一个文件形式的,写入文件后文件就不能再读取,因为已经被锁定了。如果是这样,这样的方式连接就是完全不可能的了。研究了很长时间。得出结果就是这样,不知道还有没高人见解!!!!望成全!!!
      

  9.   

    本来想用这个做一个类似SOA的面向服务模式的东西,这些都是测试用例,这都通不过。就很纳闷了,用JAVA做客户端却可以通过测试,但用PHP做就不行了,应该是机制不同吧,一个是基于WEB,一个是应用程序,传送数据通道不同。
      

  10.   

    说下我的理解吧:
    我的意思是PHP打开一个SOCKET连接时,其实JAVA服务器端创建了一个线程,这个线程就负责处理这个PHP的连接,当线程里的语句执行完,返回结果后,就断开连接,结束这个线程。PHP取到结果,连接断了,PHP就执行完了,把结果显示就行了我当是也是遇到了这样的问题,最后这样解决的。