1

我正在尝试创建一个 Jython(实际上是 monkeyrunner)程序,它接收来自其他 python 的消息(CPython,因为它使用 OpenCV)

首先,我尝试实现一个聊天程序示例(服务器端),但遇到了问题。

虽然该示例使用阻塞套接字进行选择,但 Jython 选择不支持它。

因此,我在设置套接字时输入了代码'server_socket.setblocking(0)',但没有任何改变。

另外,我尝试了'from select import cpython_compoatible_select as select',但它会导致属性错误,'function' object has no attribute 'select'

下面是我的代码

    # coding: iso-8859-1
    import socket,select
    #Function to broadcast chat messages to all connected clients
    def broadcast_data (sock, message):
        #Do not send the message to master socket and the client who has send us the message
        for socket in CONNECTION_LIST:
            if socket != server_socket and socket != sock :
                try :
                    socket.send(message)
                except :
                    # broken socket connection may be, chat client pressed ctrl+c for example
                    socket.close()
                    CONNECTION_LIST.remove(socket)

    if __name__ == "__main__":

        # List to keep track of socket descriptors
        CONNECTION_LIST = []
        RECV_BUFFER = 4096 # Advisable to keep it as an exponent of 2
        PORT = 5000

        server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # this has no effect, why ?
        #JYTHON never supports blocking-mode socket so make it unblock
        server_socket.setblocking(0)  
        server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        server_socket.bind(("0.0.0.0", PORT))
        server_socket.listen(10)

        # Add server socket to the list of readable connections
        CONNECTION_LIST.append(server_socket)

        print "Chat server started on port " + str(PORT)

        while 1:
            # Get the list sockets which are ready to be read through select
            #JYTHON never supports blocking-mode socket so make it unblock
            server_socket.setblocking(0)          
            read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])

            for sock in read_sockets:
                #New connection
                if sock == server_socket:
                    # Handle the case in which there is a new connection recieved through server_socket
                    #JYTHON never supports blocking-mode socket so make it unblock
                    server_socket.setblocking(0)                        
                    sockfd, addr = server_socket.accept()

                    CONNECTION_LIST.append(sockfd)
                    #print "Client (%s, %s) connected" % addr

                    broadcast_data(sockfd, "[%s:%s] entered room\n" % addr)

                #Some incoming message from a client
                else:
                    # Data recieved from client, process it
                    try:
                        #In Windows, sometimes when a TCP program closes abruptly,
                        # a "Connection reset by peer" exception will be thrown
                        data = sock.recv(RECV_BUFFER)
                        if data:
                            print data
                            broadcast_data(sock, "\r" + '<' + str(sock.getpeername()) + '> ' + data)                

                    except:
                        broadcast_data(sock, "Client (%s, %s) is offline" % addr)
                        print "Client (%s, %s) is offline" % addr
                        sock.close()
                        CONNECTION_LIST.remove(sock)
                        continue

        server_socket.close()
        #see http://www.binarytides.com/code-chat-application-server-client-sockets-python/

和我的错误信息

C:\NVPACK\android-sdk-windows\tools\lib>monkeyrunnerUTF chatserver.py
Chat server started on port 5000
130815 17:06:17.418:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions
] Script terminated due to an exception
130815 17:06:17.418:S [MainThread] [com.android.monkeyrunner.MonkeyRunnerOptions
]Traceback (most recent call last):
  File "C:\NVPACK\android-sdk-windows\tools\chatserver.py", line 41, in <module>

    read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],
[])
  File "C:\NVPACK\android-sdk-windows\tools\lib\jython-standalone-2.5.3.jar\Lib\
select.py", line 225, in native_select
  File "C:\NVPACK\android-sdk-windows\tools\lib\jython-standalone-2.5.3.jar\Lib\
select.py", line 106, in register
select.error: (20000, 'socket must be in non-blocking mode')

先感谢您 :)

4

1 回答 1

0

AndroidViewClient 的测试使用实现MockViewServermonkeyrunner,将套接字设置为非阻塞并使用

from select import cpython_compatible_select as select

供选择。

请参阅https://github.com/dtmilano/AndroidViewClient/blob/master/AndroidViewClient/tests/com/dtmilano/android/mocks.py#L758的源代码

这适用于 Linux 和 OSX(您的里程可能因 Windows 而异)

于 2013-08-16T18:06:10.203 回答