socketserver.class.php
//如果接收的数据比128大 则会分多次进行传输 参考readFromClient 中用while循环来接受数据,如果接收的数据小于这里设置的大小,
//则readFromClient中就无需使用循环,可使用if,如果不确定数据大小的话还是使用whileprotected $bufsize=128; protected $endString='/0'; protected $run_mode ; protected $clientInfo; static $socketServer; private function __construct(){} public static function getInstance(){ if(is_null(self::$socketServer)){ self::$socketServer = new SocketServer(); } return self::$socketServer; } public function changeRunMode($mode=self::_REUSEADDR){ $this->run_mode = $mode; } public function changeBuffSize($size=128){ $this->bufsize = $size; } public function changeEndString($endString='\0'){ $this->endString = $endString; } private function initProperties($host,$port,$common_protocol,$type,$protocol,$timeout=60,$max_connection_limit=1){ set_time_limit($timeout); $this -> host = $host; $this -> port = $port; $this -> common_protocol = $common_protocol; $this -> type = $type; $this -> protocol = $protocol; $this -> timeout = $timeout; $this -> max_connection_limit = $max_connection_limit; $this -> clientcount = 0; $this -> socketclients = array(); $this -> run_mode = self::_REUSEADDR; } /** * @param $host * @param $port * @param $protocol * @param $type * @param $common_protocol * @param $timeout * @param $connection_count * @return resource */ public function createSocketServer($host,$port,$protocol,$type,$common_protocol,$timeout,$max_connection_limit){ $common_protocol = getprotobyname($common_protocol); $this->initProperties($host,$port,$protocol,$type,$common_protocol,$timeout,$max_connection_limit); $this->socket = socket_create($protocol,$type,$common_protocol);return $this->socket; } public function run(){ error_log('deamon is running ~'); socket_setopt($this->socket,SOL_SOCKET,$this->run_mode,1); socket_bind($this->socket,$this->host,$this->port); socket_listen($this->socket,$this->max_connection_limit);
while(1){ $readFD = array($this->socket); for($i=0;$i<$this->clientcount ; $i++){ if(isset($this->socketclients[$i])) $readFD[] = $this->socketclients[$i]; } //select 多路复用 在没有任何客户端数据的情况下 应该利用阻塞停止CPU资源的消耗 if(FALSE === @socket_select($readFD,$this->null,$this->null,0))$this->shutdown(); //select 多路复用 在没有任何客户端数据的情况下 应该利用阻塞停止CPU资源的消耗 else if(@socket_select($readFD,$this->null,$this->null,0)<1) continue; if(in_array($this->socket,$readFD)){ //检查是否有新连接 $this -> accept_connection(); //接收新连接 } //遍历所有连接客户端 判断是否有数据 for($i= 0 ; $i<$this->clientcount;$i++){ if(!isset($this->socketclients[$i])) continue; if(in_array($this->socketclients[$i],$readFD)){ $data = $this->readFromClient($i); if(!$data){ //如果没有数据 就关闭连接 if($this->run_mode != SO_KEEPALIVE) $this->closeConnection($i); }else{
error_log(strval($this->socketclients[$i]).' data :'.$data); } } } } $this -> shutdown(); } private function readFromClient($clientIndex){ $cur_client = $this->socketclients[$clientIndex]; $data = ''; while($buf = socket_read($cur_client,$this->bufsize)){ $endString = substr($buf,-strlen($this->endString)); if($endString == $this->endString || $buf == NULL){ $data .= substr($buf,0,strlen($buf)-strlen($this->endString)); break; }else{ $data .= $buf; } } if($buf === FALSE){ error_log( socket_last_error($cur_client) ); } // error_log(socket_read($cur_client,$this->bufsize)); return $data; } private function accept_connection(){ for($i = 0 ; $i <= $this->clientcount;$i++){ if(!isset($this->socketclients[$i]) || $this->socketclients[$i] == null){ //如果不存在就创建 //检查是否超过最大连接 if($this->clientcount+1 > $this->max_connection_limit){ error_log('超过最大连接数:'.$this->max_connection_limit); return FALSE; break; } $this->socketclients[$i] = socket_accept($this->socket); socket_setopt( $this->socketclients[$i], SOL_SOCKET, $this->run_mode, 1 ); $this->clientcount++; $peer_host = ""; $peer_port = ""; socket_getpeername( $this->socketclients[$i], $peer_host, $peer_port ); $this->clientInfo[$i] = array( "host" => $peer_host, "port" => $peer_port, "connectOn" => time() ); error_log(strval($this->socketclients[$i]).' is connected client Index :'.$i); return $i; } } return FALSE; } private function closeConnection($clientIndex){ if(isset($this->socketclients[$clientIndex])){ socket_close($this->socketclients[$clientIndex]); unset($this->socketclients[$clientIndex]); unset($this->clientInfo[$clientIndex]); $this->clientcount--; } } private function shutdown(){ for($i = 0 ; $i<$this->clientcount;$i++){ $this->closeConnection($i); } socket_close($this->socket); }}
socketbase.class.php
守护进程deamon.php
#!/usr/bin/php createSocketServer($host,$port,SocketServer::_AF_INET,SocketServer::_SOCK_STREAM,SocketServer::_TCP,0,SOMAXCONN);$socket -> changeBuffSize(128);//设置接收字符大小$socket -> changeEndString('/0');//设置结束字符$socket -> changeRunMode($socket::_REUSEADDR);$socket -> run();?>
linux下启动守护进程
nohup ./deamon.php
客户端测试OC代码 //socket.m
//// Socket.m// NSStream//// Created by 卜 峘 on 13-7-26.// Copyright (c) 2013年 卜 峘. All rights reserved.//#import "Socket.h"@implementation Socket@synthesize input,output,host,port;-(id)init{ [super init]; port = 0; return self;}+(Socket *)instance{ static Socket *instance; if(nil == instance){ instance = [[self alloc]init]; } return instance;}-(void)connect{ if(nil == host || 0 == port)assert("error"); CFReadStreamRef readStream; CFWriteStreamRef writeStream; CFStreamCreatePairWithSocketToHost(nil, (CFStringRef)host, port, &readStream, &writeStream); input = CFBridgingRelease(readStream);//把管理权 移交给oc output = CFBridgingRelease(writeStream); [input setDelegate:self]; [output setDelegate:self]; [input scheduleInRunLoop:[NSRunLoop currentRunLoop]forMode:NSDefaultRunLoopMode]; [output scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [input open];//打开流 [output open]; [[NSRunLoop currentRunLoop] run];//启动socket}-(void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{ if(aStream == input){ //input [self inputStreamHandler:aStream handleEvent:eventCode]; }else{ //output [self outputStreamHandler:aStream handleEvent:eventCode]; }}-(void)inputStreamHandler:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{ NSString *event; switch (eventCode) { case NSStreamEventNone://没有事件发生 event = @"EventNone"; break; case NSStreamEventErrorOccurred://有错误发生 event = @"EventErrorOccurred"; NSLog(@"can not connected to %@",self.host); break; case NSStreamEventEndEncountered://到达流的末尾 event = @"EventEndEncounted"; /** 当NSInputStream对象到达steam的末尾的时候,它会向stream:handleEvent:函数发送一个NSStreamEventEndEncountered事件类型常量,delegate函数应该做出与准备使用流对象相反的操作,也就是说,需要关闭流对象,从run loop中移除,最终释放流对象 **/ [aStream close]; [aStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [aStream release]; aStream = nil; break; case NSStreamEventOpenCompleted://打开流成功 event = @"EventOpenCompleted"; break; case NSStreamEventHasSpaceAvailable://可以向流中写入数据 event = @"EventHasSpaceAvailable"; break; case NSStreamEventHasBytesAvailable://可以读取流中的数据 event = @"EventHasBytesAvailable"; NSMutableData *data = [[[NSMutableData alloc]autorelease ]init]; uint8_t buffer[1024]; int len; while([input hasBytesAvailable]){ len = (int)[input read:buffer maxLength:sizeof(buffer)]; if(len > 0){ [data appendBytes:buffer length:len]; } } NSString *result = [[[NSString alloc]autorelease ]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"%@",result); break; default: event=@"default"; break; } // NSLog(@"%@",event);}-(void)outputStreamHandler:(NSStream *)aStream handleEvent:(NSStreamEvent)eventCode{ NSString *event; switch (eventCode) { case NSStreamEventNone://没有事件发生 event = @"EventNone"; break; case NSStreamEventErrorOccurred://有错误发生 event = @"EventErrorOccurred"; NSLog(@"can not connected to %@",self.host); return; break; case NSStreamEventEndEncountered://到达流的末尾 event = @"EventEndEncounted"; /** 当NSInputStream对象到达steam的末尾的时候,它会向stream:handleEvent:函数发送一个NSStreamEventEndEncountered事件类型常量,delegate函数应该做出与准备使用流对象相反的操作,也就是说,需要关闭流对象,从run loop中移除,最终释放流对象 **/ [aStream close]; [aStream removeFromRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; [aStream release]; aStream = nil; break; case NSStreamEventOpenCompleted://打开流成功 event = @"EventOpenCompleted"; break; case NSStreamEventHasSpaceAvailable://可以向流中写入数据 event = @"EventHasSpaceAvailable"; if([output hasSpaceAvailable]){ // NSString *info = [NSString stringWithFormat:@"%d",rand()]; // uint8_t *buff = (uint8_t *)[info UTF8String]; uint8_t buff[] = "aaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdaaaaaaasssdasdsadasdadasdadasdasdadasdtest info /0"; [output write:buff maxLength:strlen((char*)buff)]; [output close];//关闭输出流 不然会不停的发送消息 } break; case NSStreamEventHasBytesAvailable://可以读取流中的数据 event = @"EventHasBytesAvailable"; break; default: event=@"default"; break; } // NSLog(@"%@",event);}@end
oc main 函数
//// main.m// NSStream//// Created by 卜 峘 on 13-7-26.// Copyright (c) 2013年 卜 峘. All rights reserved.//#import#import "Socket.h"int main(int argc, const char * argv[]){ @autoreleasepool { Socket *socket = [Socket instance]; socket.host = @"127.0.0.1"; socket.port = 5443; [socket connect]; } return 0;}