Hi everyone
I really need some help with my socket server code.
I'm using this code for gaming purpose which more than 20,000 users might be connected to at once.
I know that the code works fine right now with 4 or 5 users connected for testing. But what happens when 30,000 players are connected? Will it still work or does it get slow or crash?
I know this could be done with many other languages but right now I need it in PHP.
Thanks -
<?php
-
error_reporting(E_ALL);
-
set_time_limit(0);
-
-
ob_implicit_flush();
-
-
$address = '192.168.56.1';
-
$port = 1234;
-
-
function send_Message($listOfClients, $buf){
-
if (is_array($listOfClients)){
-
foreach($listOfClients as $currentClient){
-
socket_write($currentClient, $buf);
-
}
-
} else {
-
socket_write($listOfClients, $buf.chr(0));
-
}
-
}
-
-
if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {die ("socket_create() failed, reason: " . socket_strerror($master) . "\n");}
-
-
socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);
-
-
if (($ret = socket_bind($master, $address, $port)) < 0){die("socket_bind() failed, reason: " . socket_strerror($ret) . "\n");}
-
-
if (($ret = socket_listen($master, 5)) < 0){
-
die("socket_listen() failed, reason: " . socket_strerror($ret) . "\n");
-
} else {
-
echo 'Server Running...';
-
}
-
-
-
-
$read_sockets = array($master);
-
-
while (true) {
-
$changed_sockets = $read_sockets;
-
-
$num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, 5);
-
-
foreach($changed_sockets as $socket) {
-
if ($socket == $master) {
-
if (($client = socket_accept($master)) < 0){
-
//echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
-
continue;
-
} else {
-
array_push($read_sockets, $client);
-
send_Message($client, $client);
-
}
-
} else {
-
$bytes = socket_recv($socket, $buffer, 2048, 0);
-
-
if ($bytes == 0){
-
$index = array_search($socket, $read_sockets);
-
unset($read_sockets[$index]);
-
socket_close($socket);
-
} else {
-
$allclients = $read_sockets;
-
array_shift($allclients);
-
send_Message($allclients, $buffer.chr(0));
-
if (trim($buffer) == 'exit'){socket_close($master); die('<br>Server stoped!!!');}
-
}
-
-
}
-
}
-
-
}
-
?>
-
1 2140
Anybody? Any suggestions will be highly appreciated...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: zbcong |
last post by:
Hello:
I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
|
by: zhebincong |
last post by:
Hello:
I write a multithread c# socket server,it is a winform application,there is
a richtextbox control and button,when the button is click,the server begin
to listen the socket port,waiting...
|
by: Colin |
last post by:
I'm writing a little console socket server but I'm having some
difficulty. Can I ask your advice - where is the best place to get
some help on that topic? It would be nice if some people who knew...
|
by: james b |
last post by:
Hi all,
We are currently putting together a web based application which needs
real time communication with the server. In order to achieve this we
are writing an application which will run as a...
|
by: Macca |
last post by:
Hi,
My application uses an asynchronous socket server. The question I have is
what i should set my socket server buffer size to.
I will know the size of each data packet sent across the...
|
by: Macca |
last post by:
Hi,
I have a synchronous socket server which my app uses to read data from
clients.
To test this I have a simulated client that sends 100 byte packets.
I have set up the socket server so...
|
by: Macca |
last post by:
Hi,
I am writing an asychronous socket server to handle 20+ simulataneous
connections.
I have used the example in MSDN as a base. The code is shown at end of
question.
Each connection has a...
|
by: zxo102 |
last post by:
Hi everyone,
I am using a python socket server to collect data from a socket
client and then control a image location ( wxpython) with the data,
i.e. moving the image around in the wxpython frame....
|
by: Engineerik |
last post by:
I am trying to create a socket server which will listen for connections from
multiple clients and call subroutines in a Fortran DLL and pass the results
back to the client. The asynchronous socket...
|
by: doc |
last post by:
Hi
Hard to know where to post this and I don't want to double post it - please move it if you need to,
I am trying to connect a Flash client socket (XML) to a php socket server (using Flash 8 and...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |