我简单说吧,我用JAVA做了个socket服务端,用PHP去链接它,问题出来了:
每次只要从PHP端传到服务器端数据后,再读取从服务器端发来的数据就读不了了。为什么
也就是
fputs()后再fgets()就读不了了,超时了。
为什么,在线等,这问题困扰我很长时间了。
每次只要从PHP端传到服务器端数据后,再读取从服务器端发来的数据就读不了了。为什么
也就是
fputs()后再fgets()就读不了了,超时了。
为什么,在线等,这问题困扰我很长时间了。
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();
}
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)
{
}
}
}
}
重发遍
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)
{
}
}
}
}
有断开的fclose();不是这个问题
我发现,是不是php里的Socket连接读取缓存就是一个文件形式的,写入文件后文件就不能再读取,因为已经被锁定了。如果是这样,这样的方式连接就是完全不可能的了。研究了很长时间。得出结果就是这样,不知道还有没高人见解!!!!望成全!!!
我的意思是PHP打开一个SOCKET连接时,其实JAVA服务器端创建了一个线程,这个线程就负责处理这个PHP的连接,当线程里的语句执行完,返回结果后,就断开连接,结束这个线程。PHP取到结果,连接断了,PHP就执行完了,把结果显示就行了我当是也是遇到了这样的问题,最后这样解决的。