Code2Design.com

User login

The Layout

Programming

Graphic Design

Resources

Navigation

C2D Projects

Unsystematic Affiliates

Tutorials Expert phpBB Tutorials-db XeonFX 

Change Language

Who's online

There are currently 0 users and 3 guests online.

Help with sockets in php

<?php
   
class User
   
{
        var 
$spawn;
        var 
$nic "";
        var 
$inn false;
        function 
User($socket_spawn)
        {
           
$this->spawn $socket_spawn;
        }
    }
   
$users = array();
    function 
checkForName($name)
    {
        global 
$users;
        foreach(
$users as $user)
        {
            if(
strtoupper($user->nic) == strtoupper($name))
            {
                return 
false;
            }
        }
        return 
true;
    }
    function 
findUserFromSoc($soc)
    {
        global 
$users;
       
$i 0;
        foreach(
$users as $user)
        {
            if(
$user->spawn == $soc)
            {
                return 
$i;
            }
           
$i++;
        }
        return 
false;
    }
    function 
handle_clients($socket$buffer)
    {
        global 
$users;
        foreach(
$users as $user)
        {
           
$nic $users[findUserFromSoc($socket)]->nic;
           
socket_write($user->spawn"$nic wrote: $buffer\n\r");
        }
    }
    function 
shut_down()
    {
        global 
$users$socket;
        foreach(
$users as $user)
        {
           
socket_close($user->spawn);
        }
       
socket_close($socket);
        die();
    }
   
//Timeout
   
set_time_limit(0);
   
//Create the socket
   
$socket socket_create(AF_INETSOCK_STREAM0) or die("Could not create socket");
   
//Binding the socket to a port
   
$result socket_bind($socket"127.0.0.1"1234) or die("Could not bind socket to port");
   
//Setting up a listener
   
$result socket_listen($socket);
    echo 
"Listener's up running\n\r";
    while(
true)
    {
       
$changed_sockets = array($socket);
        foreach(
$users as $user)
        {
           
$changed_sockets[] = $user->spawn;
        }
        echo 
"Number off sockets: ".count($changed_sockets)."\n\r";
       
$num_changed_sockets socket_select($changed_sockets$write NULL$except NULLNULL);
        foreach(
$changed_sockets as $soc) {
            if (
$soc == $socket) {
               
$client socket_accept($socket) or die("Could not accept connection");
               
socket_write($client"Welcom to my humble chat. In order to join you need to enter a username. What would you like as username?\n\r");
               
$user = new User($client);
               
array_push($users$user);
            } else {
               
$user findUserFromSoc($soc);
                if(!
is_int($user))
                {
                    die(
"Unknown error");
                }
                if(
$users[$user]->nic == "")
                {
                   
$bytes socket_recv($soc$buffer20480);
                    if (
$bytes == 0) {
                       
$index findUserFromSoc($soc);
                        if(
is_int($index))
                        {
                            unset(
$users[$index]);
                        }
                       
socket_close($soc);
                    }
                    else
                    {
                        if(
$buffer == "Q")
                        {
                           
shut_down();
                        }
                        if(
checkForName($buffer))
                        {
                           
$users[$user]->nic $buffer;
                        }
                        else
                        {
                           
socket_write($soc"Sorry, but the username ".$buffer." has already been taken. Pleas select another one.\n\rWhat would you like your username to be?\n\r");
                        }
                    }
                }
                else
                {
                   
$bytes socket_recv($soc$buffer20480);
                    if (
$bytes == 0) {
                       
$index findUserFromSoc($socket);
                        if(
is_int($index))
                        {
                            unset(
$users[$index]);
                        }
                       
socket_close($soc);
                    }
                    else
                    {
                        if(
$buffer == "Q")
                        {
                           
shut_down();
                        }
                        echo 
"Received $buffer\n\r";
                       
handle_clients($soc$buffer);
                    }
                }
            }
        }
    }
?>

Problem: Name and strings can only be one character long :S


Submitted by Alxandr on November 7, 2007 - 9:04pm.
printer friendly version

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br> <br /> <h3>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use BBCode tags in the text, URLs will be automatically converted to links
More information about formatting options



Like what you see?

Why not add more? C2D is looking for other Christian Web Masters who would like to help write articles for this site. If you have expericance in FLASH, CSS/HTML, PHP/MySQL, PhotoShop/GIMP, Blender, Javascript, or just General Design - our users would love to hear what you have to say. Contact Us

delicious   digg   reddit   magnoliacom   newsvine   furl   google   yahoo   technorati