Due to work needs, we have recently developed such a server-side program. Strictly speaking, this program is not a server program for normal chat. It is a Broadcast Server processor in a game system. However, you only need to make some modifications to make it a chat server.
First, let's briefly introduce the functions (we hope our programmers will not feel the trouble). The game has been launched. Based on our ideas, we also want to build a broadcast system, that is, some events have occurred to users in the game. In the PHP code, we send these events to the front-end flash page through the backend server, let all users know such a thing.
This is a simple program running. the Linux server is mainly responsible for receiving data from the PHP program and pushing the data to the front-end page.
I use socket to implement such a system. Today, we will not describe how PHP handles socket. We have already introduced related files. In addition, we will not introduce the protocol used for data transmission here, as long as we make unified regulations on servers and PHP programs. Our focus is to summarize the implementation process of this broadcast Background:
Let's take a simple look at what needs to be done using the socket server:
1. Load the SOCKET protocol,
2. Create a socket object,
3. Bind the socket to a port,
4. Listen to the corresponding port (using listener ),
5. Accept the client's socket,
6. Create a database for transmission.
Most socket programming uses the above structure. Here we also use the TCP protocol. For our program, there is a special requirement here. We don't face one-to-one data transmission, we just accept data from one place, however, we need to push this data to many clients, so our client connection is an array. In my program, I use this structure.
Typedef struct client {
Int FD;
Struct sockaddr_in ADDR;
} Client;
Client client [fd_setsize]; // set fd_setsize to 1024
While (1)
{
Confd = accept (slisten, (struct sockaddr *) & ADDR, & Len) =-1;
If (confd)
{
For (I = 0; I <fd_setsize; I ++)
{
If (Client [I]. FD <0)
{
Client [I]. FD = confd;
Client [I]. ADDR = ADDR;
}
}
}
}
The above Code fills up such a client. If some clients are disconnected, we can use the new connection to complete it.
As mentioned above, this program also needs to accept the data of the PHP program, so we must use the Recv and send functions, but we know that these functions are blocking I/O, if no data comes in, the program will stay here, so that the data to be pushed cannot be sent (you can implement it by yourself ,). To solve this problem, you must use non-blocking programming.
Here I use the select function. For details, you can check it online:
Fd_set is a basic structure of select. Here we define
Fd_set allset, rset;
Fd_zero (& allset); // Initialization
Fd_set (slisten, & allset); // place the server socket in select
Maxfd = slisten;
Nready = select (maxfd + 1, & rset, null, null, & TV); // note that the first parameter must be added. This function is very important and can be found clearly during use.
We will perform different processing based on the nready value:
If (nready = 0) // timeout
Else if (nready <0) // Error
Else
{
// Data Transmission
// Use fd_isset to determine which one has a reflection, such as a new interface and receiving data.
}
Here is another question. I just mentioned that a program can only process 1024 connections. What should I do if the number of users is greater than 1024?