def test0(sock): """ Send a basic message """ print('server: test0') message = 'message in a bottle' if mode == 'DGRAM': udt4.sendmsg(sock, struct.pack('I', len(message)), 4) udt4.sendmsg(sock, message, len(message)) else: udt4.send(sock, struct.pack('I', len(message)), 4) udt4.send(sock, message, len(message))
def test1(sock): """ Send a basic message on a padded size, this is useful if you might be sending serialized data where the receiver already has a fixed buffer prepared """ print('server: test1') pad = 64 msg = 'words are only words except when they are not' print(pad) if mode == 'DGRAM': udt4.sendmsg(sock, struct.pack('I', pad), 4) udt4.sendmsg(sock, msg, pad) else: udt4.send(sock, struct.pack('I', pad), 4) udt4.send(sock, msg, pad)
def send(self, data, padding = None): """ In non-blocking mode this will raise an exception, in blocking mode (default) the call will wait on freed buffer space. :param data Data to be written :type data str() :param padding Set a specified size the data should be written onto. eg: len(data) => 45 padding = 64, data would be sent as |45 bytes data|19 byte bzero| :type padding int() """ if None == padding: return udt4.send(self.__sock, data, len(data)) else: return udt4.send(self.__sock, data, padding )
def send(self, data, padding=None): """ In non-blocking mode this will raise an exception, in blocking mode (default) the call will wait on freed buffer space. :param data Data to be written :type data str() :param padding Set a specified size the data should be written onto. eg: len(data) => 45 padding = 64, data would be sent as |45 bytes data|19 byte bzero| :type padding int() """ if None == padding: return udt4.send(self.__sock, data, len(data)) else: return udt4.send(self.__sock, data, padding)
def runTest(self): Thread(target=self.runServer).start() udt.connect(self.recevr, '127.0.0.1', 8001) for k, i in enumerate([('a', 1), ('b', 2), ('c', 3)]): buf = i[0] * (10**i[1]) if k % 2 != 0: self.assertEquals(udt.send(self.recevr, buf, len(buf)), len(buf)) else: self.assertEquals(udt.recv(self.recevr, len(buf)), buf)
def runServer(self): udt.bind(self.sender, '127.0.0.1', 8001) 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.send(client, buf, len(buf)), len(buf)) else: self.assertEquals(udt.recv(client, len(buf)), buf)
def reliableUDTSend(udtsocket, buf): tosend = len(buf) sent = 0 ret = 0 while (tosend): ret = udt4.send(udtsocket, buf[sent:], tosend) if (ret <= 0): return -1 sent += ret tosend -= ret if (sent != len(buf)): print "reliable UDT send failed. Sent %d bytes out of %d bytes" % ( sent, len(buf)) return sent
def runTest(self): Thread(target = self.runServer).start() udt.connect(self.recevr, '127.0.0.1', 8001) for k, i in enumerate([('a', 1), ('b', 2), ('c', 3)]): buf = i[0] * (10 ** i[1]) if k % 2 != 0: self.assertEquals( udt.send(self.recevr, buf, len(buf)), len(buf) ) else: self.assertEquals( udt.recv(self.recevr, len(buf)), buf )
def runServer(self): udt.bind (self.sender, '127.0.0.1', 8001) 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.send(client, buf, len(buf)), len(buf) ) else: self.assertEquals( udt.recv(client, len(buf)), buf )
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) udt4.cleanup()
#!/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'