Scenario. I have a client with two network connections to the server. One connection uses a mobile phone, and the other uses a wlan connection.
The way I solved this is that the server is listening on two ports. But the mobile connection should be slower than the wlan connection. The data that is sent is usually a string of text. I solved the problem of a slow connection by allowing the mobile connection to send data in five seconds, the wlan connection has an interval of one second.
But is there a better way to slow down? Perhaps sending smaller packets, i.e. I need to send more data?
Any ideas on a good way to slow down one of the compounds?
Orjanp
A simple example of a single connection client.
def client(): import sys, time, socket port = 11111 host = '127.0.0.1' buf_size = 1024 try: mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mySocket.connect((host, port)) except socket.error, (value, message): if mySocket: mySocket.close() print 'Could not open socket: ' + message sys.exit(1) mySocket.send('Hello, server') data = mySocket.recv(buf_size) print data time.sleep(5) mySocket.close() client()
A simple server listening on a single port.
def server(): import sys, os, socket port = 11111 host = '' backlog = 5 buf_size = 1024 try: listening_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM) listening_socket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) listening_socket.bind((host, port)) listening_socket.listen(backlog) except socket.error, (value, message): if listening_socket: listening_socket.close() print 'Could not open socket: ' + message sys.exit(1) while True: accepted_socket, adress = listening_socket.accept() data = accepted_socket.recv(buf_size) if data: accepted_socket.send('Hello, and goodbye.') accepted_socket.close() server()
python networking network-programming
Orjanp
source share