Home > Mobile >  Message to a specific user Socket Python
Message to a specific user Socket Python

Time:11-24

How can I send a message to a specific user in the socket? I can send a message to a specific user if I simply bind an object with its address to a pointer with its name.

I am making a simple messenger on socket and I want to send a message to a SPECIFIC user.

Example:

users = {"user": <socket.socket fd=288, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.0.3', 9090), raddr=('178.70.167.46', 55663)>}

The problem is that if I restart the script (server), then the entire dictionary is zeroed out.

  1. Where can I save such an object so that it can be restarted? Could I just take it from JSON?

  2. Can I manually enter/create data to send to a specific client?

CodePudding user response:

1. You could keep a json file with the addresses of the recipients and then when it turns on, connect to them.

2. Yes, you can. But, it can't be sent as a list or dictionary, you will have to send it as a string or something

CodePudding user response:

You can't save a socket, ever. A socket is a connection between two programs. You can't save one end of the connection by itself, it doesn't make sense. When your server program dies, the whole connection goes away because the other program isn't connected to anything any more.

Maybe you can save the address and make a new connection after you restart the program.


You don't have "a specific user in the Socket". Most likely, you have one socket per user. If you want to send data to a user, you send data with that user's socket. Users["user"].send(b"hello world") or whatever.

  • Related