Python Forum
Python socket : Error receive data
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python socket : Error receive data
#1
I write a network application. The server has ability to find client base on given subnet. If the client receive authentication message from server, it will respond to server. Everything working good but server, it can't receiver from client.

Client :

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()
Server :

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()
Error on Server :

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\]
Without the line data = s.recv(1024) on Server, it working fine. But with it, the error show up. Can anybody please tell me why it happen ?
Reply
#2
Hello,

To the line 61 of Server script, try to replace "data = s.recv(1024)" by "data = conn.recv(1024)"

Avorane
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Socket to stream data and tranmit and revieve data kiyoshi7 0 2,340 Aug-11-2022, 10:52 PM
Last Post: kiyoshi7
  how to split socket data onto multiple clients for separate processing ConcealedFox70 0 1,905 Jan-11-2022, 08:26 PM
Last Post: ConcealedFox70
  python socket connect only sometimes espDino 0 1,501 Jul-15-2020, 12:13 PM
Last Post: espDino
  socket without blocking loop and automatically retrieve data from the port RubenP 3 3,488 Jun-21-2020, 10:59 PM
Last Post: Gribouillis
  Python 2.7 vs Python 3.8 (Socket Module) MattB 8 6,522 Mar-18-2020, 01:02 PM
Last Post: MattB
  how to get your own ip using python (NO SOCKET:) ) julio2000 3 2,256 Mar-02-2020, 09:35 PM
Last Post: buran
  TCP socket, data transnission Simba 4 3,227 Dec-07-2019, 03:15 PM
Last Post: Simba
  socket.error Karin 0 2,722 Nov-11-2019, 08:53 AM
Last Post: Karin
  Raw Socket Error therealherby 1 3,746 Oct-24-2019, 06:33 AM
Last Post: markfilan
  Soft Access Point & Socket Data Streaming groger57 1 2,477 Aug-01-2019, 02:53 PM
Last Post: groger57

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020