473,386 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

PHP Socket server performance

258 100+
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

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. error_reporting(E_ALL);
  3. set_time_limit(0);
  4.  
  5. ob_implicit_flush();
  6.  
  7. $address = '192.168.56.1';
  8. $port = 1234;
  9.  
  10. function send_Message($listOfClients, $buf){
  11.     if (is_array($listOfClients)){
  12.         foreach($listOfClients as $currentClient){
  13.             socket_write($currentClient, $buf);
  14.         }
  15.     } else {
  16.         socket_write($listOfClients, $buf.chr(0));
  17.     }
  18. }
  19.  
  20. if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) {die ("socket_create() failed, reason: " . socket_strerror($master) . "\n");}
  21.  
  22. socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1);
  23.  
  24. if (($ret = socket_bind($master, $address, $port)) < 0){die("socket_bind() failed, reason: " . socket_strerror($ret) . "\n");}
  25.  
  26. if (($ret = socket_listen($master, 5)) < 0){
  27.     die("socket_listen() failed, reason: " . socket_strerror($ret) . "\n");
  28. } else {
  29.     echo 'Server Running...';
  30. }
  31.  
  32.  
  33.  
  34. $read_sockets = array($master);
  35.  
  36. while (true) {
  37.    $changed_sockets = $read_sockets;
  38.  
  39.    $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, 5);
  40.  
  41.    foreach($changed_sockets as $socket) {
  42.        if ($socket == $master) {
  43.            if (($client = socket_accept($master)) < 0){
  44.                //echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . "\n";
  45.                continue;
  46.            } else {
  47.               array_push($read_sockets, $client);
  48.               send_Message($client, $client); 
  49.            }
  50.        } else {
  51.            $bytes = socket_recv($socket, $buffer, 2048, 0);
  52.  
  53.            if ($bytes == 0){
  54.                $index = array_search($socket, $read_sockets);
  55.                unset($read_sockets[$index]);
  56.                socket_close($socket);
  57.            } else {
  58.                    $allclients = $read_sockets;
  59.                 array_shift($allclients);
  60.                 send_Message($allclients, $buffer.chr(0));
  61.                 if (trim($buffer) == 'exit'){socket_close($master); die('<br>Server stoped!!!');}
  62.             }
  63.  
  64.        }
  65.    }
  66.  
  67. }
  68. ?>
  69.  
Oct 30 '13 #1
1 2140
bnashenas1984
258 100+
Anybody? Any suggestions will be highly appreciated...
Nov 5 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
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...
2
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...
7
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...
2
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...
2
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...
9
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...
0
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...
9
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....
4
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
0
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...
0
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
0
BarryA
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...
1
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...
0
marktang
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,...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.