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

  • Home
  • SEARCH
  • 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 8655849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:08:20+00:00 2026-06-12T15:08:20+00:00

Possible Duplicate: A PHP Socket Server with Flash Clients I am building an app

  • 0

Possible Duplicate:
A PHP Socket Server with Flash Clients

I am building an app in my server with the help of a flash developer, and he asked me to build a socket server to communicate with the database. He recommended me JAVA but I am not very good at JAVA and I was wondering if it was possible to build a Socket Server in PHP.

I should allow connections to multiple TCP client connections. I know in JAVA this is done thought threading, but I am not sure if this can also be achieved with PHP.

Could someone please show me the basic skeleton of a PHP Socket Server with these characteristics?

The connection has to be TCp (persistent) from the beginning of the connection to the app, until the end.

  • 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-12T15:08:22+00:00Added an answer on June 12, 2026 at 3:08 pm

    You have to run your socket server as a service from the command line.
    This is a part of what I have used before. It closes the socket after a read but can easy be modified to keep a array of connections.

    • You would have to build some kind of watchdog to see if a connection is still alive.
    • You need a identifying mechanism to identify different connections.

    The code:

    set_time_limit( 0 );
    // Set the ip and port we will listen on
    $address = '127.0.0.1';
    $port = 6789;
    
    // Create a TCP Stream socket
    $sock = socket_create( AF_INET, SOCK_STREAM, 0 ); // 0 for  SQL_TCP
    // Bind the socket to an address/port
    socket_bind( $sock, 0, $port ) or die( 'Could not bind to address' );  //0 for localhost
    // Start listening for connections
    socket_listen( $sock );
    
    //loop and listen
    while (true) {
      /* Accept incoming  requests and handle them as child processes */
      $client = socket_accept( $sock );
      // Read the input  from the client – 1024000 bytes
      $input = socket_read( $client, 1024000 );
    
      // from here you need to do your database stuff
      // and handle the response 
    
       // Display output  back to client
      socket_write( $client, $response );
      socket_close( $client );
    }
    // Close the master sockets
    socket_close( $sock );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: PHP server on local machine? Is it possible to run .php files
Possible Duplicate: PHP & MySQL Pagination Update Help So, in my site, there are
Possible Duplicate: PHP get both array value and array key I am using Codeigniters'
Possible Duplicate: PHP - reversed order in if statement Checking for null - what
Possible Duplicate: PHP create a file without fopen I want to create a file
Possible Duplicate: PHP Get end string on url between / and / I have
Possible Duplicate: php false place in condition I have noticed that a lot of
Possible Duplicate: php loop through associative arrays i have a variable which prints an
Possible Duplicate: PHP function to strip tags, except a list of whitelisted tags and
Possible Duplicate: php calculate float The accuracy of PHP float calculate Why does PHP

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.