Python套接字:错误接收数据

时间:2018-03-11 16:15:25

标签: python python-2.7 sockets

我写了一个网络应用程序。服务器能够在给定的子网上查找客户端。如果客户端从服务器接收验证消息,它将响应服务器。一切都很好,但服务器,它不能从客户接收。

客户:

def ListenServer():
    # Listen init signal from Server to send data

    HOST = ''                 # Symbolic name meaning all available interfaces
    PORT = 50007              # Arbitrary non-privileged port

    # UDP Socket
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.bind((HOST, PORT))
    data, addr = s.recvfrom(1024)
    if data == 'Authen':
        SocketConnect(addr[0])

def SocketConnect(HOST):
    # Connect to Server to send data
    print HOST
    PORT = 50008              # The same port as used by the server

    # Create Socket
    print "Create Socket"
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error, e:
        print "Error creating socket: %s" %e
        sys.exit(1)

    # Connect
    print "Connect"
    try:
        s.connect((HOST, PORT))
    except socket.error, e:
        print "Connection error: %s" %e
        sys.exit(1)

    # Send Data
    print "Send Data"
    try:
        s.sendall('Hello, world')
    except socket.error, e:
        print "Error sending data: %s" % e
        sys.exit(1)


    # Close Socket
    s.close()
    print "Close Socket"

ListenServer()

服务器:

from netaddr import IPAddress
import socket
import sys
import ipaddress
import time


def FindAgent():
    PORT = 50007          # Port use to find Agent

    #Find broadcast address

    """IPAddress("255.255.255.0").netmask_bits()        #Convert Subnet Mask to Prefix Length, Result is 24"""
    try :
        HOST = str(ipaddress.ip_network(u'192.168.10.0/24')[-1])
    except ValueError as e :
        """e = sys.exc_info()[0]  # Find Exception you need"""
        print e

    # UDP client
    MESSAGE = "Authen"
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    for x in range(0,2):
        sock.sendto(MESSAGE, (HOST, PORT))


def ListenClient():
    # Listen Client sent data
    HOST = socket.gethostbyname(socket.gethostname())
    PORT = 50008
    # TCP socket

    # Create Socket
    print "Create Socket"
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    except socket.error, e:
        print "Error creating socket: %s" %e
        sys.exit(1)

    # Bind
    print "Bind"
    try:
        s.bind((HOST, PORT))
    except socket.error, e:
        print "Error bind: %s" %e
        sys.exit(1)

    # Listen
    print "Listen"
    try:
        s.listen(10)
    except socket.error, e:
        print "Error listen: %s" %e
        sys.exit(1)

    # Accept data from client
    print "Accept data from client"
    try:
        conn, addr = s.accept()
        data = s.recv(1024)
    except socket.error, e:
        print "Error listen: %s" %e
        sys.exit(1)

    print data
    s.close()

FindAgent()
ListenClient()
服务器上的

错误:

Create Socket
Bind
Listen
Accept data from client
Error listen: [Errno 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
[Finished in 0.8s with exit code 1]
[shell_cmd: python -u "C:\Users\Win7_Lab\Desktop\Server.py"]
[dir: C:\Users\Win7_Lab\Desktop]
[path: C:\Python27\;C:\Python27\Scripts;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\]

在服务器上没有 data = s.recv(1024)行,它运行正常。但有了它,错误就出现了。有谁能告诉我它为什么会发生?

1 个答案:

答案 0 :(得分:0)

崩溃来自s.recv(1024)正如您所说的那样,因为您的服务器上的接收(.recv())方法需要在客户端连接上调用。

请遵循以下示例:here

服务器文件:

conn, addr = s.accept()
data = conn.recv(1024)

客户档案:

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((HOST, PORT))
data = s.recv(1024)

如您所见,客户端从套接字(服务器)接收数据。 服务器从客户端连接接收数据。

希望这很有用。

  

编辑添加一些示例链接。

您可以在这些教程中找到您想要的内容: