LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 667|回复: 0

关于socket 的udp 服务器的程序

[复制链接]
发表于 2005-5-13 08:46:20 | 显示全部楼层 |阅读模式
<?php
// Set time limit to indefinite execution
set_time_limit (0);

// Set the ip and port we will listen on
$address = '192.168.255.255' ;
$port = '9000';

// Create a TCP Stream socket
$sock = socket_create(AF_INET, SOCK_DGRAM, 0);
// Bind the socket to an address/port
socket_bind($sock, $address, $port) or die('Could not bind to address');
// Start listening for connections
//socket_listen($sock);

/* Accept incoming requests and handle them as child processes */
$client = socket_accept($sock);

// Read the input from the client – 1024 bytes
$input = socket_read($client, 1024);

// Strip all white spaces from input
$output = ereg_replace("[ \t\n\r]","",$input).chr(0);

// Display output back to client
socket_write($client, $output);

// Close the client (child) socket
socket_close($client);

// Close the master sockets
socket_close($sock);
?>

上面这个的udp socket服务器哪里错了 ! 但是我把$sock = socket_create(AF_INET, SOCK_DGRAM, 0); 改成 $sock = socket_create(AF_INET, SOCK_STREAM, 0) ; 和//socket_listen($sock);
的注释去掉 tcp的socket服务器就可以 !! 谢谢大家了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表