1

I'm developing a multi room chat for a University Project, using C.

I managed to create a server, which listens to clients and everything works. But right now, what i made is a chat with a single room, and many clients can connect to it.

I have to change it: a client should be able to choose a room, where he has to be matched with a single client. Many clients can enter the same room, but can be matched only to one other client. To make it simple: if a room has 10 clients, and i get in, i should wait for someone to connect here too.

The problem is: i don’t know how to implement this. What is, exactly, ‘’a room’’? Is it another socket? So a client connets to the server’s socket, and then gets redirected to ANOTHER socket? Or should i use ports? I really don’t know what should be the best implementation here.

KarlKorr
  • 31
  • 4
  • A room doesn't actually exist. The operating system doesn't have rooms. It's something that your program has to create. When the assignment says "draw a square", you don't draw a square, you draw a bunch of *s and spaces, to make it *look* like a square. Does that make sense? – user253751 Aug 23 '22 at 16:36
  • I understand what you're meaning, but still, i don't know what would be the proper way to create that ''illusion''. And, I don't know (since i'm new to this type of programming) if this should be the proper way. That's why I asked this question, to understand what's the best way to develop this. – KarlKorr Aug 23 '22 at 16:45
  • When a client connects, send a list of rooms to the client, and ask which room they want to join. You'll need to keep a list of clients and the rooms they've selected. An array of structs can be used to store that information. So when a new client connects and selects a room, you search the list for another client waiting in that room. If there is someone waiting, then connect the two. Otherwise, add the new client to the list. – user3386109 Aug 23 '22 at 16:56
  • You could make a list of all the clients that aren't paired yet, and their room names. When a new client connects, you could ask them for their room name. Then, you could look in the list to see if any other client has the same room name, and if you find one, remove from the list and pair them, otherwise add this client to the list so that another client can find it later. As long as it all acts like there are rooms, then there are rooms - you created rooms from nothing! – user253751 Aug 23 '22 at 16:57
  • Thanks to both of you, now i have a starting point! – KarlKorr Aug 23 '22 at 17:05
  • And if you wanted to have rooms instead of pairs, you could make a list of all the clients and their room names, and whenever a client sends a message, only send it to the other clients that have the same room name. Or (a bit more efficient) you could make a list of rooms and then make each room have its own list of clients. – user253751 Aug 23 '22 at 18:20

0 Answers0