简体   繁体   中英

How can I return socket created by function and pass it to another?

I would like to define function that creates socket, binds socket to specific address and port, get data from source(incessantly) and then return this socket so I could use it in other function. Is it possible? If so, how can I do that?

I would like to multithread socket_analyser , does it imply multithreading socket_receiver also?

The problem is that after invoking socket_creator , the function doesn't return sock . When I try to pass it to socket_receiver in socket_analyser it underline parameter sock saying it's not defined.

I also tried to define testsocket = sock just after socket_creator(MCAST_GRP[0],MCAST_PORT) to pass it to socket_receiver(testsock, BUFFER_UDP) however - the same result.

import socket
import struct
import time
import threading


MCAST_GRP = ['239.0.1.104','239.0.1.105']
MCAST_PORT = 12345
BUFFER_UDP = 2048

def socket_creator(multicast_group, multicast_port):

    MCAST_GRP = multicast_group
    MCAST_PORT = multicast_port    
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)   
    sock.bind(('', MCAST_PORT))
    mreq = struct.pack("4sl", socket.inet_aton(MCAST_GRP), socket.INADDR_ANY)
    sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
    return sock
#testsocket = sock

def socket_receiver(sockett, bufferUDP):
    BUFFER_UDP = bufferUDP
    sock_rcv = sockett
    for x in range(10):       
        data, address = sock_rcv.recvfrom(BUFFER_UDP)
        totalSize += len(data)
    return totalSize

def socket_analyser(multicast_group, multicast_port, totalSize):
    print("_____.:|   Starting analysis!   |:._____\n")
    print("͞◌͞◌͞◌͞◌͞.:|   IP: {}    PORT: {}  |:.͞◌͞◌͞◌͞͞◌͞◌".format(multicast_group,multicast_port))    
    socket_analyser(sock, BUFFER_UDP)
    print(totalSize)
    totalSize = 0

socket_creator(MCAST_GRP[0],MCAST_PORT)
socket_creator(MCAST_GRP[1],MCAST_PORT)

t1 = threading.Thread(target=socket_analyser, args=(MCAST_GRP[0],MCAST_PORT,BUFFER_UDP, sock))
t2 = threading.Thread(target=socket_analyser, args=(MCAST_GRP[1],MCAST_PORT,BUFFER_UDP, sock))

t1.start()
t2.start()

t1.join()
t2.join()

print('End of test')
time.sleep(5)

Thank you in advance for anything bringing me closer to the solution

I am seeing a couple of logical issues in the code

  1. sock object scope is only inside socket_creator so it would not be available outside unless you receive the return result into another variable
  2. You are passing an undefined variable sock to the analyzer method
  3. You are recursively calling socket_analyser method inside socket_analyser method

I just modified a few things to fix

def socket_analyser(BUFFER_UDP, sock):
    socket_receiver(sock, BUFFER_UDP)
    print(BUFFER_UDP)


sock1 = socket_creator(MCAST_GRP[0],MCAST_PORT)
sock2 = socket_creator(MCAST_GRP[1],MCAST_PORT)

t1 = threading.Thread(target=socket_analyser, args=(BUFFER_UDP, sock1))
t2 = threading.Thread(target=socket_analyser, args=(BUFFER_UDP, sock2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM