Errno 111连接拒绝使用套接字Python

时间:2018-06-08 11:00:10

标签: python-2.7 sockets

我是编程新手,我用我的代码面对错误111。服务器部分运行良好但不是客户端,代码由2个BeagleBone黑卡执行,其中包含linux内核和python库(2个卡通过以太网链接)。我使用的代码是来自互联网的基本示例:来自此网站:https://pymotw.com/2/socket/tcp.html

---服务器---

import socket

import sys

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

server_address = ('localhost', 10000)

print >>sys.stderr, 'starting up on %s port %s' % server_address

sock.bind(server_address)

sock.listen(1)

while True:

print >>sys.stderr, 'waiting for a connection'

connection, client_address = sock.accept()

try:

    print >>sys.stderr, 'connection from', client_address

    while True:

        data = connection.recv(16)

        print >>sys.stderr, 'received "%s"' % data

        if data:

            print >>sys.stderr, 'sending data back to the client'

            connection.sendall(data)

        else:

            print >>sys.stderr, 'no more data from', client_address

            break           

finally:

    connection.close()

---客户---

import socket

import sys

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

server_address = ('localhost', 10000)

print >>sys.stderr, 'connecting to %s port %s' % server_address

sock.connect(server_address)

try:

    message = 'This is the message.  It will be repeated.'

    print >>sys.stderr, 'sending "%s"' % message

    sock.sendall(message)

    amount_received = 0

   amount_expected = len(message)

   while amount_received < amount_expected:

   data = sock.recv(16)

   amount_received += len(data)

   print >>sys.stderr, 'received "%s"' % data
  finally:

  print >>sys.stderr, 'closing socket'

  sock.close()

我面临连接拒绝错误。如何解决?

0 个答案:

没有答案