Advertisement
JAS_Software

Server Client Socket Code

Jun 16th, 2021
152
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # SERVER CODE
  2. import socket
  3.  
  4. # Creates a server socket
  5. # Waits for the a client to connect on the specified port the
  6. # Prints clients connection details
  7.  
  8.  
  9. def get_port():
  10.     return 8081
  11.  
  12.  
  13. def get_socket_type():
  14.     # IP Socket v4
  15.     return socket.AF_INET
  16.  
  17.  
  18. def get_socket_protocol():
  19.     # TCP Socket
  20.     return socket.SOCK_STREAM
  21.  
  22.  
  23. def get_internal_address():
  24.     return '127.0.0.1'
  25.  
  26.  
  27. def get_external_address():
  28.     return '0.0.0.0'
  29.  
  30.  
  31. server_socket = socket.socket(get_socket_type(), get_socket_protocol())
  32. server_socket.bind((get_external_address(), get_port()))
  33. # Bind socket to use the specified address and port
  34. # 0.0.0.0 = All addresses on this computer. server will accept external and internal connections
  35. # Pick up on port 8081, typically used for testing
  36.  
  37. server_socket.listen()
  38. print('Waiting for connection...')
  39.  
  40. connection_socket, address = server_socket.accept() # Waits for connection to be made to port 8081
  41. # connection to the client
  42. # address of the client - IP Address and Port
  43. print('Connection from client established')
  44.  
  45. # send message to client
  46. message = 'Hello, and thanks for connecting to this machine - ' # + address + ' - ' + connection_socket
  47. data = message.encode()
  48. connection_socket.send(data)
  49.  
  50. # receive message from client
  51. data = connection_socket.recv(1024)
  52. message = data.decode()
  53. print(message)
  54.  
  55. # close connections
  56. connection_socket.close()
  57. server_socket.close()
  58.  
  59.  
  60.  
  61.  
  62. # CLIENT CODE
  63. import socket
  64.  
  65. # Creates a client
  66. # Connect to server
  67.  
  68.  
  69. def get_port():
  70.     return 8081
  71.  
  72.  
  73. def get_socket_type():
  74.     # IP Socket v4
  75.     return socket.AF_INET
  76.  
  77.  
  78. def get_socket_protocol():
  79.     # TCP Socket
  80.     return socket.SOCK_STREAM
  81.  
  82.  
  83. def get_internal_address():
  84.     return '127.0.0.1'
  85.  
  86.  
  87. def get_external_address():
  88.     return '0.0.0.0'
  89.  
  90.  
  91. client_socket = socket.socket(get_socket_type(), get_socket_protocol())
  92. client_socket.connect((get_internal_address(), get_port()))
  93. print('Connected to server')
  94.  
  95. # receive message from server
  96. data = client_socket.recv(1024)
  97. message = data.decode()
  98. print(message)
  99.  
  100. # send message to server
  101. message = 'Received message'
  102. data = message.encode()
  103. client_socket.send(data)
  104.  
  105. # closing connection
  106. client_socket.close()
  107.  
  108.  
  109.  
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement