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
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
def listen(self, backlog): """ Set socket listen count :param backlog Backlog count :type backlog int() """ udt4.listen(self.__sock, backlog)
def runServer(self): udt.bind(self.sender, '127.0.0.1', 8002) udt.listen(self.sender, 10) client, host = udt.accept(self.sender) for k, i in enumerate([('a', 1), ('b', 2), ('c', 3)]): buf = i[0] * (10**i[1]) if k % 2 == 0: self.assertEquals(udt.sendmsg(client, buf, len(buf)), len(buf)) else: self.assertEquals(udt.recvmsg(client, len(buf)), buf)
def runServer(self): udt.bind (self.sender, '127.0.0.1', 8002) udt.listen(self.sender, 10) client, host = udt.accept(self.sender) for k, i in enumerate([('a', 1), ('b', 2), ('c', 3)]): buf = i[0] * (10 ** i[1]) if k % 2 == 0: self.assertEquals( udt.sendmsg(client, buf, len(buf)), len(buf) ) else: self.assertEquals( udt.recvmsg(client, len(buf)), buf )
#!/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'