First of all you need a client code to connect with your server.
This is a TCP socket, therefore it is connection oriented.
This means that prior to any data transference (socket.recv(), socket.send() ) you need to request a connection from the client to the server, and the server must accept the connection.
After the connection is estabilished you will be able to freely send data between the sockets.
this is an example of this simple socket design that can be applied genericaly to your program:
Client Example
import socket
# create an ipv4 (AF_INET) socket object using the tcp protocol (SOCK_STREAM)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connect the client
# client.connect((target, port))
client.connect(('0.0.0.0', 9999))
# send some data (in this case a String)
client.send('test data')
# receive the response data (4096 is recommended buffer size)
response = client.recv(4096)
print(response)
Server Example
import socket
import threading
bind_ip = '0.0.0.0'
bind_port = 9999
max_connections = 5 #edit this to whatever number of connections you need
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip, bind_port))
server.listen(max_connections) # max backlog of connections
print (('Listening on {}:{}').format(bind_ip, bind_port))
def handle_client_connection(client_socket):
request = client_socket.recv(4096 )
print (str(request))
client_socket.send('ACK!')
client_socket.close()
while True:
client_sock, address = server.accept()
print (('Accepted connection from {}:{}').format(address[0], address[1]))
client_handler = threading.Thread(
target=handle_client_connection,
args=(client_sock,) # without comma you'd get a... TypeError: handle_client_connection() argument after * must be a sequence, not _socketobject
)
client_handler.start()
This example should print test data in the server console and ACK! in the client console.
edit: Not sure about python3 prints working as i wrote them here... but it's just a small detail. The general idea is what matters in this case. When i get to a pc I will try to run this and correct the prints if there is any syntax mistake