我根据python中关于套接字的指南编写了两个程序。

我使用的是rasbperirypi3作为客户机,一台普通的linux ubuntu计算机作为服务器。这是服务器软件:import socket

import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

server_address = ('localhost', 15000)

print("starting up on %s port %s" % server_address, file=sys.stderr)

sock.bind(server_address)

# Listen for incoming connections

sock.listen(1)

while True:

# Wait for a connection

print("waiting for a connection", file=sys.stderr)

connection, client_address = sock.accept()

try:

print("connection from ", client_address, file=sys.stderr)

# Receive the data in small chunks and retransmit it

while True:

data = connection.recv(16)

print("received %s" % data, file=sys.stderr)

if data:

print("sending data back to the client", file=sys.stderr)

connection.sendall(data)

else:

print("no more data from ", client_address, file=sys.stderr)

break

finally:

# Clean up the connection

connection.close()

这是客户端软件:

^{pr2}$

这是服务器上的输出:peter@GIGAS:~/thermServer$ python3 thermServer.py

starting up on localhost port 15000

waiting for a connection

这是树莓派的输出:pi@raspberrypi:~ $ python3 thermClient.py

connecting to 192.168.18.250 port 15000

Traceback (most recent call last):

File "thermClient.py", line 10, in

sock.connect(server_address)

ConnectionRefusedError: [Errno 111] Connection refused

我已经转发了路由器中的端口,但由于这是内部通信,这应该无关紧要,我是错过了在服务器中添加一些可以正常打开端口的东西,还是需要在linux机器上处理项目之外的东西?在

评论关闭
IT源码网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!