Beispiel #1
0
def create_socket(host, port, blockSize=65536, timeout=NGAMS_SOCK_TIMEOUT_DEF):

    global g_udt_started
    global udt_start_lock

    udt_start_lock.acquire()
    try:
        if (not g_udt_started):
            udt4.startup()
            g_udt_started = True
    finally:
        udt_start_lock.release()

    print('create_client(%s, %s)' % (host, port))

    socket = udt4.socket(socklib.AF_INET, socklib.SOCK_STREAM,
                         socklib.AI_PASSIVE)

    #print "UDT_SNDTIMEO = %d" % udt4.getsockopt(socket, udt4.UDT_SNDTIMEO)
    #print "UDT_RCVTIMEO = %d" % udt4.getsockopt(socket, udt4.UDT_RCVTIMEO)
    #
    # set sock options
    #
    if (not timeout or timeout < 0):
        loc_timeout = NGAMS_SOCK_TIMEOUT_DEF
    else:
        loc_timeout = timeout

    opts = [(udt4.UDP_SNDBUF, blockSize), (udt4.UDP_RCVBUF, blockSize),
            (udt4.UDT_SNDTIMEO, loc_timeout)]

    for opt in opts:
        udt4.setsockopt(socket, opt[0], opt[1])
    #print "UDT_SNDTIMEO = %d" % udt4.getsockopt(socket, udt4.UDT_SNDTIMEO)
    #print "UDT_RCVTIMEO = %d" % udt4.getsockopt(socket, udt4.UDT_RCVTIMEO)


#
#
#    udt4.setsockopt(socket, udt4.UDP_SNDBUF,640000)
#    udt4.setsockopt(socket, udt4.UDP_RCVBUF,640000)
#
#print "UDP_SNDBUF = %d" % udt4.getsockopt(socket, udt4.UDP_SNDBUF)
#print "UDP_RCVBUF = %d" % udt4.getsockopt(socket, udt4.UDP_RCVBUF)

#    udt4.setsockopt(socket, udt4.UDT_SNDBUF,64)
#    udt4.setsockopt(socket, udt4.UDT_RCVBUF,64)
    localhost = '146.118.84.66'
    udt4.bind(socket, localhost, port)

    #print('connecting client')
    udt4.connect(socket, host, port)
    #    try:
    #        udt4.connect(socket, host, port)
    #    except Exception as err:
    #        #print('Exception: %s' % err)
    #        raise err

    #print "%s ---------" % str(socket.UDTSOCKET)
    return socket
Beispiel #2
0
def create_client(host, port):
    print('create_client(%s, %s)' % (host, port))

    global client 

    socket = udt4.socket(
            socklib.AF_INET, socklib.SOCK_STREAM, socklib.AI_PASSIVE
            ) 
    
    #
    # set sock options 
    #
    opts = [ (udt4.UDT_SNDBUF   , 64),
             (udt4.UDT_RCVBUF   , 64)
             ]
    
    #for opt in opts:
    #    udt4.setsockopt(socket, opt[0], opt[1]) 
   
    print('connecting client')
    try:
        udt4.connect(socket, host, port)
    except Exception as err:
        print('Exception: %s' % err)
        return 0
   
    client = socket 

    return True
Beispiel #3
0
def create_server(host, port):
    print('create_server(%s, %s)' % (host, port))
    
    global server 
    
    socket = udt4.socket(
            socklib.AF_INET, 
           (socklib.SOCK_STREAM, socklib.SOCK_DGRAM)[mode == 'DGRAM'], 
            socklib.AI_PASSIVE
            ) 
    
    print(
            ('socklib.SOCK_STREAM', 'socklib.SOCK_DGRAM')[mode == 'DGRAM']
            )
    
    #
    # set sock options 
    #
    opts = [ (udt4.UDT_SNDBUF   , 64 * 1024),
             (udt4.UDT_RCVBUF   , 64 * 1024),
             (udt4.UDT_REUSEADDR, True     )
             ]

    for opt in opts:
        udt4.setsockopt(socket, opt[0], opt[1])
    
    udt4.bind(socket, host, port)
    udt4.listen(socket, 10)
    
    server = socket

    return True 
Beispiel #4
0
def create_client(host, port):
    print('create_client(%s, %s)' % (host, port))

    global client

    socket = udt4.socket(socklib.AF_INET, socklib.SOCK_STREAM,
                         socklib.AI_PASSIVE)

    #
    # set sock options
    #
    opts = [(udt4.UDT_SNDBUF, 64), (udt4.UDT_RCVBUF, 64)]

    #for opt in opts:
    #    udt4.setsockopt(socket, opt[0], opt[1])

    print('connecting client')
    try:
        udt4.connect(socket, host, port)
    except Exception as err:
        print('Exception: %s' % err)
        return 0

    client = socket

    return True
Beispiel #5
0
def create_server(host, port):
    print('create_server(%s, %s)' % (host, port))
    
    global server 
    
    socket = udt4.socket(
            socklib.AF_INET, socklib.SOCK_STREAM, socklib.AI_PASSIVE
            ) 
    
    #
    # set sock options 
    #
    opts = [ (udt4.UDT_SNDBUF   , 64 * 1024),
             (udt4.UDT_RCVBUF   , 64 * 1024),
             (udt4.UDT_REUSEADDR, True     )
             ]

    #for opt in opts:
    #    udt4.setsockopt(socket, opt[0], opt[1])
    
    udt4.bind(socket, host, port)
    udt4.listen(socket, 10)
    
    server = socket

    return True 
Beispiel #6
0
    def __init__(self, family = socklib.AF_INET, type = socklib.SOCK_STREAM,
                 protocol = 0, _sock = None):
        """
        :param  family:     UDT requires this family to be AF_INET 
        :type   family:     int() 

        :param  type:       SOCK_STREAM or SOCK_DGRAM 
        :type   type:       int() 
        
        :param  protocol:   AI_PASSIVE or int(0) are tested working.
        :type   protocol:   int() 
        
        :param  _sock:      Internally used to implement a dup()  
        :type   _sock:      UdtSocket() 

        """
        if _sock != None:
            self.__sock = _sock
            self.__udpsock = None
        else:
            self.__udpsock = socklib.socket(family,
                                            socklib.SOCK_DGRAM,
                                            protocol)
            self.__sock = udt4.socket(family, type, protocol)
            udt4.bind_to_udp(self.__sock, self.__udpsock.fileno())
Beispiel #7
0
    def __init__(self, family = socklib.AF_INET, type = socklib.SOCK_STREAM,
                 protocol = 0, _sock = None):
        """
        :param  family:     UDT requires this family to be AF_INET 
        :type   family:     int() 

        :param  type:       SOCK_STREAM or SOCK_DGRAM 
        :type   type:       int() 
        
        :param  protocol:   AI_PASSIVE or int(0) are tested working.
        :type   protocol:   int() 
        
        :param  _sock:      Internally used to implement a dup()  
        :type   _sock:      UdtSocket() 

        """
        if _sock != None:
            self.__sock = _sock
        else:
            self.__sock = udt4.socket(family, type, protocol) 
Beispiel #8
0
#!/usr/bin/env python

import socket as socklib
import udt4
import struct

udt4.startup()

socket = udt4.socket(socklib.AF_INET, socklib.SOCK_STREAM, 0)

try:
    udt4.connect(socket, '127.0.0.1', 3001)
except RuntimeError as err:
    print(err)
    import sys
    sys.exit(0)

# recv message #
msg_len = struct.unpack('I', udt4.recv(socket, 4))[0]
message = udt4.recv(socket, msg_len)

print('received message: %s' % message)
assert len(message) == msg_len, 'invalid message received 0'

# send message #
message = 'no bottle found'
udt4.send(socket, struct.pack('I', len(message)), 4)
udt4.send(socket, message, len(message))

# cleanup #
udt4.close(socket)
Beispiel #9
0
 def setUp(self):
     self.sender = udt.socket(socklib.AF_INET, socklib.SOCK_STREAM,
                              socklib.AI_PASSIVE)
     self.recevr = udt.socket(socklib.AF_INET, socklib.SOCK_STREAM,
                              socklib.AI_PASSIVE)
Beispiel #10
0
 def setUp(self):
     self.sender = udt.socket(socklib.AF_INET, socklib.SOCK_DGRAM
                             , socklib.AI_PASSIVE)
     self.recevr = udt.socket(socklib.AF_INET, socklib.SOCK_DGRAM
                             , socklib.AI_PASSIVE)
Beispiel #11
0
#!/usr/bin/env python 

import socket as socklib
import struct
import udt4 

socket = udt4.socket(socklib.AF_INET, socklib.SOCK_STREAM, 0)

udt4.bind(socket, '127.0.0.1', 3001)
udt4.listen(socket, 10)

sock, host = udt4.accept(socket)

# send message #
message = 'message in a bottle'

udt4.send(sock, struct.pack('I', len(message)), 4)
udt4.send(sock, message, len(message))


# recv message #
msg_len = struct.unpack('I', udt4.recv(sock, 4))[0]

message = udt4.recv(sock, msg_len) 

print(
    'received message: %s' % message
    )

assert len(message) == msg_len, 'invalid message'