Nolan's Blog

a PHP JS UI APP developer

PHP Socket with multiple clients


error_reporting(E_ALL);

/* Allow the script to hang around waiting for connections. */
set_time_limit(0);

/* Turn on implicit output flushing so we see what we're getting as it comes in. */
ob_implicit_flush();

$address = '127.0.0.1';

$port = 10000;

// create a streaming socket, of type TCP/IP
$sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

socket_set_option($sock, SOL_SOCKET, SO_REUSEADDR, 1);

socket_bind($sock, $address, $port);

socket_listen($sock);

// create a list of all the clients that will be connected to us..
// add the listening socket to this list
$clients = array($sock);

while (true)
{
    // create a copy, so $clients doesn't get modified by socket_select()
    $read = $clients;
    $write = null;
    $except = null;

    // get a list of all the clients that have data to be read from
    // if there are no clients with data, go to next iteration
    if (socket_select($read, $write, $except, 0) < 1)
        continue;

    // check if there is a client trying to connect
    if (in_array($sock, $read))
    {
        $clients[] = $newsock = socket_accept($sock);

        socket_write($newsock, "There are ".(count($clients) - 1)." client(s) connected to the server\n");

        socket_getpeername($newsock, $ip, $port);
        echo "New client connected: {$ip}\n";

        $key = array_search($sock, $read);
        unset($read[$key]);
    }

    // loop through all the clients that have data to read from
    foreach ($read as $read_sock)
    {
        // read until newline or 1024 bytes
        // socket_read while show errors when the client is disconnected, so silence the error messages
        $data = @socket_read($read_sock, 4096, PHP_BINARY_READ);

        // check if the client is disconnected
        if ($data === false)
        {
            // remove client for $clients array
            $key = array_search($read_sock, $clients);
            unset($clients[$key]);
            echo "client disconnected.\n";
            continue;
        }

        $data = trim($data);

        if (!empty($data))
        {
            echo " send {$data}\n";

            // do sth..

            // send some message to listening socket
            socket_write($read_sock, $send_data);

            // send this to all the clients in the $clients array (except the first one, which is a listening socket)
            foreach ($clients as $send_sock)
            {
                if ($send_sock == $sock)
                    continue;

                socket_write($send_sock, $data);

            } // end of broadcast foreach
        }

    } // end of reading foreach
}

// close the listening socket
socket_close($sock);

继续阅读》

,

留名火星!

如无意外的话,NASA 将会在 2016 年的 3 月发射 InSight(Interior Exploration using Seismic Investigations, Geodesy and Heat Transport)探测器前往火星,以研究火星的深处构造是如何。虽然现在距离发射仍有超过半年的时间,不过大家「名留火星」的机会快将结束了。该计划早前开始让任何网民在官网提交自己的名字、所在国家和电邮等信息,以让 InSight 载着存储了上述信息的芯片(继续阅读的图中)一起前往火星做任务;假设大家在有生之年未能前往火星,也至少能在形式上参与相关任务吧。早前 Orion 任务收集到约 130 万个名字,而目前 InSight 只收到 60 多万个,大家不妨一起对这个数目推高啊。

我已经申请~~~ 下面是我的船票! 申请地址在 http://mars.nasa.gov/participate/send-your-name/insight/?linkId=16760252

继续阅读》

CentOS 6.5 PHP Update to 5.4 5.5 5.6

A best rpm:
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm

yum install php(54w/55w/56w) php55w-bcmath php55w-cli php55w-common php55w-devel php55w-fpm php55w-gd php55w-imap php55w-ldap php55w-mbstring php55w-mcrypt php55w-mysql php55w-odbc php55w-pdo php55w-pear php55w-pecl-igbinary php55w-xml php55w-xmlrpc php55w-opcache php55w-intl php55w-pecl-memcache

继续阅读》

Install / Setting vsftp in CentOS

1. Install
yum install vsftp

2. Config
vi vsftpd.conf


# turn off anonymous login
anonymous_enable=NO
# use userlist user for ftp
userlist_deny=NO
# open passive mode
pasv_enable=YES
pasv_min_port=10000
pasv_max_port=10010
# login banner
ftpd_banner=Welcome to Putike.cn

3. Create user for ftp
useradd –s /sbin/nologin –d /ftp/home/path username
passwd username

4. Add user to userlist
vi user_list

5. Virtual user if need
a. create virtual user list
touch vuser
eg:
username
password

b. build users db
db_load –T –t hash –f /etc/vsftpd/vuser /etc/vsftpd/vuser.db

c. edit /etc/pam.d/vsftpd, add lines:


auth required pam_userdb.so db=/etc/vsftpd/vuser
account required pam_userdb.so db=/etc/vsftpd/vuser

d. config vsftp


guest_enable=YES # enable guest
guest_username=vuser # user vuser db
pam_service_name=vsftpd # use pam

e. set virtual user config path
user_config_dir=/etc/vsftpd/vsftpd_user_conf

f. config the virtual user settings accoring to your needs.
touch username


anon_world_readable_only=NO # view ftp path
anon_upload_enable=YES # allow download
anon_mkdir_write_enable=YES # create and delete dir
anon_other_write_enable=YES # rename or delete file
local_root=/path/ # define user's root 

继续阅读》

, ,

福利已到~再谢白白Sama

shary

吾组福利丰厚!让组长代购的雪莉手办已经到他那里了,话说= = 好大的包装啊,里面会有啥!?
嘛~看着好爽~~~先上图感受一下 23333

追加:晚上的时候,原价7680就已经下线了,恢复原价了。。。赞赞赞!

shary2

继续阅读》

Ubuntu Install and Use Shadowsocks+privoxy

1. INSTALL Shadowsocks-qt5
The document and help on Github is here.
(Github 上有中文安装说明,但是要跳转几次。[传送门]


add-apt-repository ppa:hzwhuang/ss-qt5
apt-get update
apt-get install shadowsocks-qt5

2. Start Up
Setting server,port,username..(GUI). Not say more.

3. privoxy
INSTALL:
apt-get install privoxy
CONFIG:
vi /etc/privoxy/config
INSERT A NEW LINE
forward-socks5 / 127.0.0.1:1080 . (注意最后的点不是我失误
RESTART privoxy
service privoxy restart

继续阅读》

, , ,

Use proxychains to set up a proxy for some program in Linux

Install:
ubuntu apt-get install proxychains
Document: http://proxychains.sourceforge.net/howto.html

Settion:
Config files be loaded order by:
1) ./proxychains.conf
2) $(HOME)/.proxychains/proxychains.conf
3) /etc/proxychains.conf
So change the config in etc/ or create a new one in $home

Config:


## Order by list. auto skip the failed proxy
#dynamic_chain

## Order by list. not skip
strict_chain 

## Order by random
#random_chain

[ProxyList]
## Add the Proxy list, Eg: shadowsocks
socks5 127.0.0.1 1080

To use it:


proxychains ./sudio.sh 

继续阅读》

, ,

Ubuntu Mount the Windows’ partition

Sometime, we need share the folder for word, likes apache’s root path or any other project’s folder.
So we need mount the other partition on start-up Ubuntu.

Step:
1. CMD sudo blkid
View all the partition of disks.


/dev/sda1: LABEL="SYSTEM" UUID="6A4031254030F981" TYPE="ntfs" PARTUUID="c52dc52d-01"
/dev/sda5: LABEL="WORK" UUID="841C686D1C685C64" TYPE="ntfs" PARTUUID="c52dc52d-05"
/dev/sda6: LABEL="DOCUMENT" UUID="E604677704674A1F" TYPE="ntfs" PARTUUID="c52dc52d-06"
/dev/sda7: LABEL="OTHER" UUID="D2AC7ADBAC7ABA15" TYPE="ntfs" PARTUUID="c52dc52d-07"
/dev/sdb1: UUID="6efcb250-6c3c-4b2b-a9c5-3e3857d5c680" TYPE="ext4" PARTUUID="3051b101-01"
/dev/sdb5: UUID="6ec85b42-afc6-4e48-bee9-65c6028d514b" TYPE="swap" PARTUUID="3051b101-05"

2.Update config vim /etc/fstab
Each line need contain the following contents:
<file system> <mount point> <type> <options> <dump> <pass>
<file system> : Partition positioning, UUID or LABEL. Eg: /dev/sda2
<mount point> : Mount point, Eg: /media/C
<type> : Partition type. Eg: ntfs or fat32
<options> : default option is defaults
<dump> : Backup partition, default is 0
<pass> : Check partition, default is 0

Add your new partition:


# disk WORK
/dev/sda5 /media/nolan/WORK/ ntfs defaults 0 0

3. Test
sudo mount -a

4. Options
About the options

async / sync
(Yes/Not) Synchronous
Recommend “async” (better performance).

auto / noauto
(Yes/Not) Auto mount
Default is “auto”.

rw / ro
Read and Write / Read only

exec / noexec
(Yes/Not) Executable
This option will control the all files in partition can be execution or not.

user / nouser
(Yes/Not) Allow default user mount partition.
Generally, we choose not allow.

suid/nosuid
(Yes/Not) Have suid auth.
Allow SUID ?

defaults is same of “rw, suid, dev, exec, auto, nouser, async”.

继续阅读》

, ,

Ubuntu 14.10 mirrors

apt-get update ## update repos
apt-get dist-upgrade ## update package

sohu源:

deb http://mirrors.sohu.com/ubuntu/ utopic main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ utopic-security main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ utopic-updates main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ utopic-proposed main restricted universe multiverse
deb http://mirrors.sohu.com/ubuntu/ utopic-backports main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ utopic main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ utopic-security main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ utopic-updates main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ utopic-proposed main restricted universe multiverse
deb-src http://mirrors.sohu.com/ubuntu/ utopic-backports main restricted universe multiverse

aliyun源:

deb http://mirrors.aliyun.com/ubuntu/ utopic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ utopic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ utopic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ utopic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ utopic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ utopic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ utopic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ utopic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ utopic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ utopic-backports main restricted universe multiverse

163源:

deb http://mirrors.163.com/ubuntu/ utopic main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ utopic-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ utopic-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ utopic-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ utopic-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ utopic main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ utopic-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ utopic-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ utopic-proposed main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ utopic-backports main restricted universe multiverse

继续阅读》

, ,

HTML Character Code

Char Code Char Code Char Code Char Code Char Code
´ &acute; © &copy; > &gt; µ &micro; ® &reg;
\& & amp; ° &deg; ¡ &iexcl; &nbsp; » &raquo;
¦ &brvbar; ÷ &divide; ¿ &iquest; ¬ &not; § &sect;
&bull; ½ &frac12; « &laquo; &para; ¨ &uml;
¸ &cedil; ¼ &frac14; < &lt; ± &plusmn; × &times;
¢ &cent; ¾ &frac34; ¯ &macr; &quot; &trade;
&euro; £ &pound; ¥ &yen;
&bdquo; &hellip; · &middot; &rsaquo; ª &ordf;
ˆ &circ; &ldquo; &mdash; &rsquo; º &ordm;
&dagger; &lsaquo; &ndash; &sbquo; &rdquo;
&Dagger; &lsquo; &permil; &shy; ˜ &tilde;
&asymp; &frasl; &larr; &part; &spades;
&cap; &ge; &le; &Prime; &sum;
&clubs; &harr; &loz; &prime; &uarr;
&darr; &hearts; &minus; &prod; &zwj;
&diams; &infin; &ne; &radic; &zwnj;
&equiv; &int; &oline; &rarr;
α &alpha; η &eta; μ &mu; π &pi; θ &theta;
β &beta; γ &gamma; ν &nu; ψ &psi; υ &upsilon;
χ &chi; ι &iota; ω &omega; ρ &rho; ξ &xi;
δ &delta; κ &kappa; ο &omicron; σ &sigma; ζ &zeta;
ε &epsilon; λ &lambda; φ &phi; τ &tau;
Α &Alpha; Η &Eta; Μ &Mu; Π &Pi; Θ &Theta;
Β &Beta; Γ &Gamma; Ν &Nu; Ψ &Psi; Υ &Upsilon;
Χ &Chi; Ι &Iota; Ω &Omega; Ρ &Rho; Ξ &Xi;
Δ &Delta; Κ &Kappa; Ο &Omicron; Σ &Sigma; Ζ &Zeta;
Ε &Epsilon; Λ &Lambda; Φ &Phi; Τ &Tau; ς &sigmaf;

继续阅读》

以前的文章..

Powered by WordPress.org.