Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8244745
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:57:24+00:00 2026-06-07T21:57:24+00:00

I have php cli socket server with the code below; clients connect and send

  • 0

I have php cli socket server with the code below; clients connect and send requests

<?php
// PHP SOCKET SERVER
error_reporting(E_ERROR);
// Configuration variables
$host = "127.0.0.1";
$port = 5600;
$max = 500;
$client = array();

// No timeouts, flush content immediatly
set_time_limit(0);
ob_implicit_flush();

// Server functions
function rLog($msg){
             $msg = "[".date('Y-m-d H:i:s')."] ".$msg;
             echo($msg."\n");

}
// Create socket
$sock = socket_create(AF_INET,SOCK_STREAM,0) or die("[".date('Y-m-d H:i:s')."] Could not create socket\n");
// Bind to socket
socket_bind($sock,$host,$port) or die("[".date('Y-m-d H:i:s')."] Could not bind to socket\n");
// Start listening
socket_listen($sock) or die("[".date('Y-m-d H:i:s')."] Could not set up socket listener\n");

rLog("Server started at ".$host.":".$port);
// Server loop
while(true){
             socket_set_block($sock);
             // Setup clients listen socket for reading
             $read[0] = $sock;
             for($i = 0;$i<$max;$i++){
                          if($client[$i]['sock'] != null)
                                       $read[$i+1] = $client[$i]['sock'];
             }
             // Set up a blocking call to socket_select()
             $ready = socket_select($read,$write = NULL, $except = NULL, $tv_sec = NULL);
             // If a new connection is being made add it to the clients array
             if(in_array($sock,$read)){
                          for($i = 0;$i<$max;$i++){
                                       if($client[$i]['sock']==null){
                                                    if(($client[$i]['sock'] = socket_accept($sock))<0){
                                                                 rLog("socket_accept() failed: ".socket_strerror($client[$i]['sock']));
                                                    }else{
                                                                 rLog("Client #".$i." connected");
                                                    }
                                                    break;
                                       }elseif($i == $max - 1){
                                                    rLog("Too many clients");
                                       }
                          }
                          if(--$ready <= 0)
                          continue;
             }
             for($i=0;$i<$max;$i++){
                          if(in_array($client[$i]['sock'],$read)){
                                       $input = socket_read($client[$i]['sock'],1024);

                                        if($input){

                                                    rLog("Client ".$i." Call:".$input.")");

                                                    ## <<<<<<<<<<< Clients Request Show Here >>>>>>>>>>>> ##
                                                    ## <<<<<<<<<<< Clients Request Show Here >>>>>>>>>>>> ##
                                                    ## <<<<<<<<<<< Clients Request Show Here >>>>>>>>>>>> ##
                                                    ## <<<<<<<<<<< Clients Request Show Here >>>>>>>>>>>> ##
                                                    ## <<<<<<<<<<< Clients Request Show Here >>>>>>>>>>>> ##

                                        }
                          }
             }
}
// Close the master sockets
socket_close($sock);
?>

Question:
– How can I detect the IP Address of clients connected to my php cli socket server ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:57:27+00:00Added an answer on June 7, 2026 at 9:57 pm

    Immediately after the line where you successfully call socket_accept(), put a call to socket_getpeername():

    if (($client[$i]['sock'] = socket_accept($sock)) < 0) {
        rLog("socket_accept() failed: ".socket_strerror($client[$i]['sock']));
    } else {
        rLog("Client #".$i." connected");
        socket_getpeername($client[$i]['sock'], $address, $port);
    }
    

    $address now contains the remote host’s IP address, and $port contains the remote port. Do with that information as you will.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay - I'm beaten. I have a (PHP-CLI) server written using PHP's socket_* functions.
I have php script that must be runned from console (php-cli). But configuration of
I have a PHP CLI script ( http://codepad.org/w6iyLLdv ) which stubbornly returns exit code
Im using Debian testing, i have installed php-cli and when i try to execute
Hi guys I have a weird question, I have a cli php script runs
I have a php script that is running in CLI and I want to
I have php code that puts some values into an Array as follows: $hunter=addslashes($MessageArray[1]);
Below is partial code to an experimental http server app I'm building from scratch
I have never used PHP with CLI, but I have seen scripts running with
I have a really puzzling problem: I am writing a PHP CLI application running

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.