【问题标题】:Does socket in SWI-Prolog have a big bug?SWI-Prolog 中的套接字是否有大错误?
【发布时间】:2015-03-21 23:33:28
【问题描述】:

我用 Python 写了一个服务器,用 SWI-Prolog 写了一个客户端。 客户端可以向服务器发送消息,但不能接收 从服务器发送的消息。

两个程序的源码如下。

1). The server 

导入套接字

如果 "ma​​in" == 名称

try:

    sock = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    sock.bind(('localhost',8888))
    sock.listen(5)
except:
    print("init socket err!")

while True:
    print('\n listren for client...')
    conn,addr = sock.accept()
    print('get client')
    print(addr)

    conn.settimeout(5)
    szBuf = conn.recv(1024)
    print(byt)

    if '0' == szBuf:
        conn.send(b'exit')
    else:
        conn.send(b'welcome client!')

    conn.close()
    print('end of the service')

2). The client 

:- use_module(library(socket)).

create_client :-
    setup_call_catcher_cleanup(
        tcp_socket(Socket),
        tcp_connect(Socket, localhost:8888),
        exception(_),
        tcp_close_socket(Socket)
    ),
    setup_call_cleanup(
        tcp_open_socket(Socket, In, Out),
        chat_to_server(In, Out),
        close_connection(In, Out)
    ).

close_connection(In, Out) :-
    close(In, [force(true)]),
    close(Out, [force(true)]).

chat_to_server(In, Out) :-
    write(Out,'....... 12345 .........'),
    read(In,Term),
    nl,write(Term),nl.

当客户端运行时,服务器很快退出并显示错误:

Traceback (most recent call last):
      File "D:\PyQt4\learn\sockets\server2.py", line 24, in <module>
      szBuf = conn.recv(1024)
socket.timeout: timed out

服务器然后停止,直到重新启动服务器。

但是如果删除谓词,读取(In,Term), 客户端和服务器都会正常运行。

新的子句如下:

chat_to_server(In, Out) :-
    write(Out,'....... 12345 .........')

为什么会出现问题,如何解决?

提前致谢。

【问题讨论】:

    标签: sockets stream swi-prolog


    【解决方案1】:

    您设置了 5 毫秒的超时时间,这太短了,所以读取超时。您的意思可能是 5 秒,即 5000 的参数值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-07
      • 2013-06-23
      • 1970-01-01
      • 2021-09-04
      相关资源
      最近更新 更多