一个小程序,服务端terminal1里运行一个25*25的地图,给客户端传送24个字节的视野信息。客户端terminal2里是agent,一个5*5的视野。老师给的是java版的。客户端和服务端tcp连接。因为不熟悉java 我用python写客户端。但接受5*5的数据总是有问题。比如最开始接受的数据是长度为1的空byte类型。之后每次显示的5*5视野都比原来的慢2步。想问下是哪地方出了问题。map:~~ T> T k ~~~~ ~~* *~~ ~~~~*-* *-*~~~~ ** ** ~~~~ g ** ** a ~~~~ * * ~~view+-----+| T ||~* || ^ || ||* |+-----+2312324接受的数据按道理长度应该是24,但实际接受的数据长度分别是23,1,23,24。好像是24被分成了23和1。用java版的agent就没这个问题。这是java的服务端输出的代码: InputStream in = null; OutputStream out = null; ServerSocket serverSocket = null; Socket clientSocket = null; int i,j; try { serverSocket = new ServerSocket( port ); clientSocket = serverSocket.accept(); serverSocket.close(); in = clientSocket.getInputStream(); out = clientSocket.getOutputStream(); } catch( IOException e ) { swanSong( "Could not listen on port: "+ port ); } try { for( m=1; m <= maxmoves; m++ ) { stepping.get_view(); for( i=0; i < 5; i++ ) { for( j=0; j < 5; j++ ) { if( !(( i == 2 )&&( j == 2 ))) { out.write( stepping.view[i][j] ); } } } out.flush(); action = (char) in.read();这是java版正确的agent代码: try { // scan 5-by-5 wintow around current location while( true ) { for( i=0; i < 5; i++ ) { for( j=0; j < 5; j++ ) { if( !(( i == 2 )&&( j == 2 ))) { ch = in.read(); if( ch == -1 ) { System.exit(-1); } view[i][j] = (char) ch; } }
添加回答
举报
0/150
提交
取消