✅ 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
What's your question?
hm how i can make the threads sending messages between the thread they chose
of course with the server
I'm slightly confused. Presumably your users are on different computers?
no
its local
OK, so there's one single application, for multiple input boxes for different "users", displayed at the same time on the same screen?
the server can get five threads one thread is one client
Wait, you're using sockets? So users are on different computers?
i am using sockets but on local ip
OK, same difference. Cool
yep
So your problem is that one thread receives a message from a client, and it wants to send that to all other clients?
i am stuck here
to the client he will choose
So somewhere in your code you need to have a list of connected users, with their username and Socket
yep
i have
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
how can i choose socket to send the massege to
what the method for that?
☝️
Your list just contains usernames. Store each user's socket next to their name in that list
so i should use dictionary?
You could do, that would be sensible
Or a list of classes
hm ok but then what?
Then once you've got the Socket, you can write the message to it
with SendTo method?
The C# equivalent, yeah
ok thx
(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...)
ok
do you know anyway to get the user socket?
Hmm? It's right there in your first code snippet
oh i didnt know accept socket give me the user socket
The socketListen method has a reference to the Socket
as value
oh ok
thx
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
ok
Then other threads can use the socket to send messages to that user
hm ok
i will try
i added the user and the socket
now what
..... 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
like this?
Your screenshots above show you sending messages to users, such as "please enter ur username". How are you sending those?
with send
but its to the same client
not the other one
So do... Exactly the same here?
I don't see where the confusion is coming from
Can you post this code?
yes
here i am sending the users
So....
globals.users[username].Send(message)
?sure it will work?
I'm guessing based on what little of your code I've seen
But the concept is correct
you are right thx again
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.