Linux Chat Room: 50 Questions
==================================== Linux Chat Room: 50 Questions ====================================
-
Question: How to implement multiple terminals? And listening?
-
What steps does a chat room application typically break down into?
-
Open a Socket, bind it to another port, and continuously listen on this port.
-
Does a server need many Sockets, or one Socket with many ports? Answer: It has many sockets, managed by an array.
-
Server (Socket Array): Basically multi-threaded: one thread listens, and after
Accept, creates a thread to handle the request.
———————————————————————————————————— Ideas: 1: The server has a Listener thread for listening. 2: The server also has a server socket thread for connecting with clients. 3: When a client connection is detected, a server socket thread is created to connect with that client independently. 4: The server needs a structure to store client information. You can write a class to encapsulate this, such as user IP, etc. ——————————————————————————————————————
-
When a client connection is detected, a server socket thread is created to connect with that client independently. This implies that sockets can be created dynamically.
-
There is a pairing relationship between listening and connecting.
-
What about
bind? Socket and port withbind? Answer: It's likely binding the created socket to the local IP. -
Think about QQ, with hundreds of millions of users, how is it managed? How does it speed up QQ login? I want to ask, is there a limit to the number of clients a terminal can listen to? Does the program's processing speed slow down as more clients connect and more threads are opened? I need to connect at least 500 clients for data interaction; will there be issues? Pushing this question again~~
-
ServerSocket.vb Responsible for listening for client connections and opening threads to accept client Sockets.
———————————————————————————————————— Public Svr As IPEndPoint 'Server address family Public SvrListenSocket As Socket 'Server listening socket Public SvrListener As Socket 'Represents the state of an asynchronous operation Public SvrListenThread As Thread 'Server listening thread Public Done As New ManualResetEvent(True) 'Notifies one or more waiting threads that an event has occurred Public alsock As New ArrayList 'Queue for sockets ——————————————————————————————————————
-
'Listen for 50 clients and start threads 'Initiate an asynchronous operation to accept an incoming connection attempt SvrListenSocket.BeginAccept(New AsyncCallback(AddressOf AcceptCallback), SvrListenSocket)
-
After a connection is found, a thread is established to handle it, and when another comes, it's recreated! public sealed class Sockets { private Sockets() { // Connections can be established here }
public static Sockets GetInstance(IPEndPoint point) { // Threads and connections can be established here } }
-
Is my example program using processes or threads for communication? I don't know. It's sockets. But?
-
Creating multiple threads is fine. Create a thread for each connection to handle it. while (true) { // Get the socket containing client information Socket client = server.Accept();
// Create a message service thread object ClientThread newclient = new ClientThread(client);
// Delegate the ClientService method of the ClientThread class to the thread Thread newthread = new Thread(new ThreadStart(newclient.ClientService));
// Start the message service thread newthread.Start(); }
-
When communicating, the client needs to know the server's IP address. It is the active party.
-
The client requests a temporary port from the server (system), releases it after use, and disconnects. It reconnects when needed. So, when QQ opens a window, is it continuously connected? (It should be. If no window is open, there's no connection. It's just connected to the server.)
-
The server, during communication, is generally the passive party and does not know the client's IP and port information.
-
Use
connectto bind and connect the client socket to a specific server address. -
Connect the socket to a remote host (client.c) or local IP (server.c).
-
Receive and send data via socket as required.
-
Port addresses are allocated per process. Sending and receiving must use the same port.
-
Why do processes block when a server handles multiple clients, but threads do not? Each client corresponds to two threads (one for sending, one for receiving); as for the client itself, it only needs two.
-
Request Sending Process
- Use
gethostbyname()to obtain host information. - Initialize the socket port.
- Use the
connectfunction to send its IP address and other information to the host, waiting for the host to call theacceptfunction to accept the request.
- Host receives request and performs data communication
- The host uses
acceptto receive the request. - Create a child thread and display a welcome message.
- Receive return information and display connection success.
- Close the client socket.
- Close the server socket and terminate the child thread.
-
What is the purpose of
define BACKLOG 10? -
So, my
Listencan only listen to one? What about multiple clients? How can concurrent listening be achieved?
To be continued...