473,388 Members | 1,346 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,388 software developers and data experts.

How to accept multiple connection to client

1
Hi, I am having trouble on how can i accept multiple connection from my device (client)..my socket code will only accept single connection from client.and if it is disconnected it will accept another client one at time,but i want all my client can send data to our server or can connect to my server.


Expand|Select|Wrap|Line Numbers
  1.  
  2. // Set time limit to indefinite execution
  3. set_time_limit (0);
  4. // Set the ip and port we will listen on
  5. $address = '192.168.0.11';//server ip demo only.
  6. $port = 123;
  7. $max_clients = 10;
  8. // Array that will hold client information
  9. $client = array();
  10. // Create a TCP Stream socket
  11. $sock = socket_create(AF_INET, SOCK_STREAM, 0);
  12. // Bind the socket to an address/port
  13. socket_bind($sock, $address, $port) or die('Could not bind to address');
  14. // Start listening for connections
  15. socket_listen($sock);
  16. // Loop continuously
  17. while (true) {
  18.    $file = fopen('txt.log','a'); 
  19.     // Setup clients listen socket for reading
  20.     $read[0] = $sock;
  21.     for ($i = 0; $i < $max_clients; $i++)
  22.     {
  23.         if (isset($client[$i]))
  24.         if ($client[$i]['sock']  != null)
  25.             $read[$i + 1] = $client[$i]['sock'] ;
  26.     }
  27.     // Set up a blocking call to socket_select()
  28.     $write = null;
  29.     $except = NULL;
  30.     $tv_sec = NULL;
  31.     echo "connecting";
  32.     $ready = socket_select($read,$write,$except,$tv_sec);
  33.     /* if a new connection is being made add it to the client array */
  34.     if (in_array($sock, $read)) {
  35.         for ($i = 0; $i < $max_clients; $i++)
  36.         {
  37.             if (!isset($client[$i])) {
  38.  
  39.                 $client[$i] = array();
  40.                  socket_set_nonblock($sock); 
  41.                 $client[$i]['sock'] = socket_accept($sock);
  42.                   $file = fopen('txt.log','a'); 
  43.  
  44.                 echo("Accepting incomming connection...\n");
  45.                 break;
  46.             }
  47.             elseif ($i == $max_clients - 1)
  48.                 print ("too many clients");
  49.         }
  50.         if (--$ready <= 0)
  51.             continue;
  52.     } // end if in_array
  53.  
  54.     // If a client is trying to write - handle it now
  55.     for ($i = 0; $i < $max_clients; $i++) // for each client
  56.     {
  57.         if (isset($client[$i]))
  58.         if (in_array($client[$i]['sock'] , $read))
  59.         {
  60.             $input = socket_read($client[$i]['sock'] , 1024);
  61.             if ($input == null) {
  62.                 // Zero length string meaning disconnected
  63.                 echo("Client disconnected\n");
  64.                 unset($client[$i]);
  65.                 fclose($file);
  66.             }
  67.             $n = trim($input);
  68.             if ($n == 'exit') {
  69.                 echo("Client requested disconnect\n");
  70.                 // requested disconnect
  71.                 socket_close($client[$i]['sock']);
  72.             }
  73.             elseif ($input) {
  74.                 echo("Receiving data\n");
  75.                 // strip white spaces and write back to user
  76.                 $output = ($input);
  77.                 //socket_write($client[$i]['sock'],$output);
  78.                  fwrite($file,$output);
  79.                  echo $output."\r\n";
  80.  
  81.  
  82.             }
  83.         } else {
  84.             // Close the socket
  85.             if (isset($client[$i]))
  86.             echo("Client disconnected\n");
  87.  
  88.             if ($client[$i]['sock'] != null){ 
  89.                 socket_close($client[$i]['sock']); 
  90.                 unset($client[$i]); 
  91.  
  92.  
  93.             }
  94.  
  95.         }
  96.  
  97.     }
  98.     fclose($file);//closing file
  99. } // end while
  100. // Close the master sockets
  101. echo("Shutting down\n");
  102. socket_close($sock);
  103. fclose($file);
  104.  
  105.  
Thank you in advance.
Jul 24 '14 #1
0 1089

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

Similar topics

0
by: Greg | last post by:
I got the following problem: A client connects to the MySQL server from a remote machine and creates a lock using GET_LOCK() method. Then the client crashes or the connection crashes (e.g.somebody...
3
by: Florian | last post by:
I need to set multiple values for some SQL statements, for example SET NUMERIC_ROUNDABORT OFF GO SET ANSI_PADDING,ANSI_WARNINGS,CONCAT_NULL_YIELDS_NULL,ARITHABORT,QUOTED_IDENTIF IER,ANSI_NULLS...
2
by: Michel Esber | last post by:
Hello, I have an environment that contains V8 (FP 11) and V7 (FP13) Linux servers. One of our applications reads data from V8 servers and writes summarized data into V7 servers that canīt be...
3
by: Dan Holmes | last post by:
The first tim ethis app is run it works just fine as well as every restart. What i can't figure is how to get it to accept a connection after the first one. This is a console app but i don't...
2
by: johnny | last post by:
How do you create multiple connection in the treads. Lets say I will have at most 5 threads and I want to create at most 5 connections. If I create a connection in the "worker method", does it...
0
by: palvin | last post by:
Any sample or any open source SSL libary to use to support client server connection on SSL.
7
by: fjm | last post by:
I would like to make sure I understand this correctly. I just came from php's site and read that multiple queries are not supported. Does this mean that I would not be able to do the following? ...
5
by: wild | last post by:
My development machine has Win XP pro and IIS 5.1 I have a 'default web site' with 2 virtual directories, let's say 'siteA' and 'siteB'. Both point to the same application in the same phisical...
1
by: Euge | last post by:
Hi, I really hope someone will be able to help me with this one as I am sure im just missing something simple. I have an unbound form which has 20 yes/no unbound check boxes. The purpose of...
1
by: Jetean | last post by:
Hi: How to make connections to multiple different Socket Servers Asyncronously? What I looking at is to have a program (C#) that log into different servers. I know to to code 1 client to 1...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.