linux 下 python 和 java, socket通讯问题。

查看:143
本文介绍了linux 下 python 和 java, socket通讯问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

一个小程序,服务端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   |
|~*   |
|  ^  |
|     |
|*    |
+-----+

23
1
23
24
接受的数据按道理长度应该是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;
                  }
               }
            }
            agent.print_view( view ); // COMMENT THIS OUT BEFORE SUBMISSION
            action = agent.get_action( view );
            out.write( action );
         }
      }

这是我的python代码

import socket

HOST = 'localhost'
PORT = 31415
ADDR = (HOST, PORT) 

sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
sock.connect(ADDR)
while True:
    action = input("dirtion:")
    sock.send(action.encode())
    data = sock.recv(24).decode()
    a = 0
    if len(data)>23:
        for i in range(5):
            for j in range(5):
                if len(data)> 23 and i !=2 or j != 2:
                    print(data[a],end='')
                    a += 1
                else:
                    print('^',end='')
        print()

解决方案

哈哈 解决了 原来是要等到制定字节数的数据,需要这个sock.recv(24,MSG_WAITALL)
MSG_WAITALL

这篇关于linux 下 python 和 java, socket通讯问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆