Advertisement
Leonard_M

NetworkingFunctions

Oct 29th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. def readInt(sock):
  2.     data = sock.recv(4)
  3.     data = struct.unpack("!i", data)[0]
  4.     return data
  5.  
  6. def sendInt(sock, toSend):
  7.     sock.send(struct.pack("!i", toSend))
  8.  
  9. def sendChar(sock, toSend):
  10.     sock.send(struct.pack("!c", toSend.encode('ascii')))
  11.  
  12. def readChar(sock):
  13.     data = sock.recv(1)
  14.     data = struct.unpack("!c", data)[0].decode('ascii')
  15.     return data
  16.  
  17. def sendIntArray(sock, toSend):
  18.     sendInt(sock, len(toSend))
  19.     for value in toSend:
  20.         sendInt(sock, value)
  21.  
  22. def sendCharArray(sock, toSend):
  23.     sendInt(sock, len(toSend))
  24.     for value in toSend:
  25.         sendChar(sock, value)
  26.  
  27. def readIntArray(sock):
  28.     buff = []
  29.     length = readInt(sock)
  30.     for i in range(length):
  31.         buff.append(readInt(sock))
  32.     return buff
  33.  
  34. def readCharArray(sock):
  35.     buff = ""
  36.     length = readInt(sock)
  37.     for i in range(length):
  38.         buff+=readChar(sock)
  39.     return buff
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement