So far, I have been working with Sockets. One socket for sending and receiving. I've been told that that is stupid, however I was not able to find the correct way to use socketpairs on client and server from ground up. I will give my current way, and hope someone could tell me how to do socketpairs with this. Using TCP by the way.
#client
import socket
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = "127.0.1.1"
port = 22222
my_socket.connect((ip, port))
#server
import socket
import multiprocessing
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip_address = socket.gethostbyname(hostname)
port = 22222
s.bind((ip_address, port))
s.listen(20)
connections=[]
connection_threads=[]
while True:
clientsocket, address = s.accept()
#does this create a new socket for every incoming connection by the way?
new_client = client.Client(address, clientsocket)
connections.append(new_client)
new_process = multiprocessing.Process(target=client_butler.connect_client, args=(new_client,))
new_process.start()
connection_threads.append(new_process)
My end goal would be that the server has one socketpair for every client. I know that this is probably not good implementation as it is completely ignoring errorhandling and so on, but I will look at that in the future as all I am trying to do is me self teaching this. So the question is, how would I transform this into working with socketpairs?
Problems I have:
- How do I connect the socketpair as in my client via
my_socket.connect
? - If I just connect both sockets individually, how do I 'reassemble' the pair in the server
- Am I even doing this correctly, or is this the completely wrong approach?
Also: I checked this out, but even if I could copy the code and it would work I wouldn't know why it would work, which isn't what I want, I don't want this to work I'd also like to learn from this.
Thanks!