-1

I have multi-threaded python socket server:

connection, client_address = sock.accept()

And i need to store 'connection' to use for other thread.

How to do that?

Marek Grzyb
  • 177
  • 13
  • Please what do you mean by "store"? Can you give us more of your code perhaps? – Peter Majko Aug 12 '17 at 20:26
  • @Peter Majko I can explain od course. For example: there are connected 2 clients, both have for example unique id, and if client 1 send specified data, server sends to client 2 some data. My server creates thread for every connection – Marek Grzyb Aug 12 '17 at 20:31
  • More code would help really. Your explanation in words is not enough. I have the same :) socket server in multithreading, in multiprocessing, based on twisted, based on socketserver and even socket. However I do not understand WHERE in your code you have client 1, client 2, where do you handle connection events, etc... – Peter Majko Aug 12 '17 at 20:41
  • @Peter Majko, I'll see tomorrow your 'list' example, if that wouldn't satisfy me, i'll show my code :) – Marek Grzyb Aug 12 '17 at 20:44

2 Answers2

2

Perhaps a list?

connections = []
connection, client_address = sock.accept()
connections.append(connection)

connection[0]  # this is your connection
Peter Majko
  • 1,241
  • 11
  • 22
1

right, when you use s.accept(), it creates like a subchannel you can talk to that client privatly in. so, if you do this:connections.append(connection), you can then select the one in the list you want and goconnections[i].send(str.encode("xyz")) or, if you just want to send a message to every client, just use sock.send("xyz"), which will send it to everyone. hope this is helpfull :)

dragon445
  • 121
  • 1
  • 4