コード例 #1
0
    if(TCP_alive < 1):
        newthread = threading.Thread(target=UDP_rdr, daemon=True, args=[conn_proxy2]) 
        newthread.start()
    newthread = threading.Thread(target=TCP_rdr, daemon=True, args=[conn, id, conn_proxy2])
    newthread.start()
    print('TCP rdr retorna, esperando UDP_rdr...')

# Main

if len(sys.argv) != 4:
    print('Use: '+sys.argv[0]+' port-in host port-out')
    sys.exit(1)
portin = sys.argv[1]
host = sys.argv[2]
portout = sys.argv[3]
socket_tcp = jsockets.socket_tcp_bind(portin)
if socket_tcp is None:
    print('bind fallo')
    sys.exit(1)
conn_proxy2 = jsockets.socket_udp_connect(host, portout)
if conn_proxy2 is None:
    print('conexion UDP rechazada por '+host+', '+portout)
    sys.exit(1)
conn_proxy2.setsockopt(socket.SOL_SOCKET, socket.SO_RCVTIMEO, struct.pack("LL",1,0))
while True:
    print('Aceptando cliente')
    index = findFirstNone(clientes)
    clientes[index][0], addr = socket_tcp.accept()
    proxy(clientes[index][0], index, conn_proxy2)
コード例 #2
0
def childdeath(signum, frame):
  os.waitpid(-1, os.WNOHANG)
  chld_cnt -= 1

def server(conn):
    while True:
        data = conn.recv(1024)
        if not data: break
        conn.send(data)
    conn.close()
    print('Cliente desconectado')
    sys.exit(0)
    

signal.signal(signal.SIGCHLD, childdeath)
s = jsockets.socket_tcp_bind(1818)
if s is None:
    print('could not open socket')
    sys.exit(1)
while True:
    conn, addr = s.accept();
    print('Cliente '+str(chld_cnt)+' conectado')
    if chld_cnt >= MAX_PROCS:
        conn.close()
        continue

    pid = os.fork()
    if pid == 0:
        s.close()
        server(conn)
        sys.exit(0)