어디서 주워서 개조한 소켓 채팅. 정보
어디서 주워서 개조한 소켓 채팅.
본문
쉘로 켜셔야 됩니다. 웹페이지용이 아니라서 ㅎ.ㅎ;;;
server.php
#!/usr/bin/php
<?
//-----------------------
class Select{
var $fhs;
//-----------------------
function Select(&$fh){
$this->fhs = array($fh);
}
//-----------------------
function add(&$fh){
array_push($this->fhs, $fh);
}
//-----------------------
function remove(&$fh){
for($i = 0; $i < count($this->fhs); $i++){
if($this->fhs[$i] == $fh){
array_splice($this->fhs, $i, 1);
break;
}
}
}
//-----------------------
function can_read($limit = 5){
$read_fhs = $this->fhs;
socket_select($read_fhs, $write_fhs = null, $exception_fhs = null, $limit);
return $read_fhs;
}
//-----------------------
function can_write($limit = 0){
$write_fhs = $this->fhs;
socket_select($read_fhs = null, $write_fhs, $exception_fhs = null, $limit);
return $write_fhs;
}
}
//-----------------------
class Guest{
var $fh;
var $name;
function Guest($fh, $name){
$this->fh = $fh;
$this->name = $name;
}
}
//-----------------------
class Lobby{
var $fh;
var $sel;
var $guests;
//-----------------------
function Lobby(){
$this->guests = array();
}
//-----------------------
function open($addr, $port){
$this->fh = socket_create(AF_INET, SOCK_STREAM, 0)
or die("create error!!n");
socket_setopt($this->fh, SOL_SOCKET, SO_REUSEADDR, 1)
or die("setopt error!!n");
socket_bind($this->fh, $addr, $port) or die("bind error!!n");
socket_listen($this->fh, 5) or die("listen error!!n");
$this->sel = new Select($this->fh);
print "Wating...";
}
//-----------------------
function close($g){
socket_close($this->fh);
}
//-----------------------
function work(){
while(true){
foreach($this->sel->can_read() as $fh){
if($fh == $this->fh){
//print "Got connection.n";
$new = socket_accept($this->fh)
or die("accept error!!n");
$this->sel->add($new);
socket_write($new, "## 대화명: ##");
}
else{
$buf = socket_read($fh, 1024);
if($buf){
printf("\n".$g->name.": %s", $buf);
$g = $this->get_guest($fh);
if(! $g){
$this->add_guest(new Guest($fh, $buf));
continue;
}
$this->talk2all(sprintf("%s: ", $g->name, $buf));
}
else{
if($this->is_guest($fh))
{$this->del_guest($fh);}
$this->sel->remove($fh);
socket_close($fh);
print "A client has been removed.n";
}
}
}
}
}
//-----------------------
function talk2all($buf){
foreach($this->guests as $g){
socket_write($g->fh, $buf);
}
}
//-----------------------
function get_guest(&$fh){
for($i = 0; $i < count($this->guests); $i++){
if($fh == $this->guests[$i]->fh){return $this->guests[$i];}
}
return null;
}
//-----------------------
function is_guest(&$fh){
for($i = 0; $i < count($this->guests); $i++){
if($fh == $this->guests[$i]->fh){return true;}
}
return false;
}
//-----------------------
function add_guest(&$g){
array_push($this->guests, $g);
$this->talk2all("## " . $g->name . " has entered. ##n");
printf("count($this->guests) = %dn", count($this->guests));
}
//-----------------------
function del_guest(&$fh){
for($i = 0; $i < count($this->guests); $i++){
if($fh == $this->guests[$i]->fh){array_splice($this->guests, $i);}
}
printf("count($this->guests) = %dn", count($this->guests));
}
}
//-----------------------
//main
set_time_limit(0);
$lob = new Lobby();
$lob->open("10.0.0.109", 50017);
$lob->work();
?>
c.php
#!/usr/bin/php
<?
set_time_limit(0);
$fh = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)
or die("create error!!n");
socket_connect($fh, "10.0.0.109", 50017) or die("connect error!!n");
$pid = pcntl_fork();
if($pid == -1){die("could not fork");}
if($pid == 0){
while(true){
$in = fopen("php://stdin", "r");
$line = fgets($in, 255);
$line = trim($line);
if($line){socket_write($fh, $line);}
}
}
else{
while(true){
print socket_read($fh, 1024);
}
}
?>
0
댓글 7개






