C
C#2y ago
mr_rox

✅ conversations between users

hello i am trying to make a server that clients can connect to him and send messages between them like whatsapp just easier the server gets theard as clients
51 Replies
canton7
canton72y ago
What's your question?
mr_rox
mr_rox2y ago
hm how i can make the threads sending messages between the thread they chose of course with the server
canton7
canton72y ago
I'm slightly confused. Presumably your users are on different computers?
mr_rox
mr_rox2y ago
no its local
canton7
canton72y ago
OK, so there's one single application, for multiple input boxes for different "users", displayed at the same time on the same screen?
mr_rox
mr_rox2y ago
the server can get five threads one thread is one client
canton7
canton72y ago
Wait, you're using sockets? So users are on different computers?
mr_rox
mr_rox2y ago
i am using sockets but on local ip
canton7
canton72y ago
OK, same difference. Cool
mr_rox
mr_rox2y ago
yep
canton7
canton72y ago
So your problem is that one thread receives a message from a client, and it wants to send that to all other clients?
mr_rox
mr_rox2y ago
i am stuck here
mr_rox
mr_rox2y ago
to the client he will choose
canton7
canton72y ago
So somewhere in your code you need to have a list of connected users, with their username and Socket
mr_rox
mr_rox2y ago
yep i have
mr_rox
mr_rox2y ago
canton7
canton72y ago
Cool, and when a thread gets a message to send and a user to send it to, it looks in that list and finds the Socket belonging to that user Then it sends the message to that Socket
mr_rox
mr_rox2y ago
how can i choose socket to send the massege to what the method for that?
canton7
canton72y ago
☝️ Your list just contains usernames. Store each user's socket next to their name in that list
mr_rox
mr_rox2y ago
so i should use dictionary?
canton7
canton72y ago
You could do, that would be sensible Or a list of classes
mr_rox
mr_rox2y ago
hm ok but then what?
canton7
canton72y ago
Then once you've got the Socket, you can write the message to it
mr_rox
mr_rox2y ago
with SendTo method?
canton7
canton72y ago
The C# equivalent, yeah
mr_rox
mr_rox2y ago
ok thx
canton7
canton72y ago
(then you can start introducing some encapsulation, and making a User class which hides the Socket and is responsible for reading and writing messages etc, but as a first step...)
mr_rox
mr_rox2y ago
ok do you know anyway to get the user socket?
canton7
canton72y ago
Hmm? It's right there in your first code snippet
mr_rox
mr_rox2y ago
oh i didnt know accept socket give me the user socket
canton7
canton72y ago
The socketListen method has a reference to the Socket
mr_rox
mr_rox2y ago
as value oh ok thx
canton7
canton72y ago
And it uses that Socket to read messages from the user, presumably So when it's read the username, store the Socket and the username together in that dictionary
mr_rox
mr_rox2y ago
ok
canton7
canton72y ago
Then other threads can use the socket to send messages to that user
mr_rox
mr_rox2y ago
hm ok i will try
mr_rox
mr_rox2y ago
i added the user and the socket
mr_rox
mr_rox2y ago
now what
canton7
canton72y ago
..... You've got the user you want to send a message to, and the message to send. Look up the user in that dictionary, get that user's socket, and send the message to that Socket
mr_rox
mr_rox2y ago
mr_rox
mr_rox2y ago
like this?
canton7
canton72y ago
Your screenshots above show you sending messages to users, such as "please enter ur username". How are you sending those?
mr_rox
mr_rox2y ago
with send but its to the same client not the other one
canton7
canton72y ago
So do... Exactly the same here? I don't see where the confusion is coming from Can you post this code?
mr_rox
mr_rox2y ago
yes
mr_rox
mr_rox2y ago
here i am sending the users
canton7
canton72y ago
So....globals.users[username].Send(message)?
mr_rox
mr_rox2y ago
sure it will work?
canton7
canton72y ago
I'm guessing based on what little of your code I've seen But the concept is correct
mr_rox
mr_rox2y ago
you are right thx again
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.