长沙北大青鸟作者:科泰校区久隆信息/张晓刚
1 #!/usr/local/bin/php -q
2
3 <?php
4 /*
5 * We don't want any time-limit for how the long can hang
6 * around, waiting for connections:
7 */
8 set_time_limit(0);
9
10 /* Create a new socket: */
11 if( ($sock = socket( AF_INET, SOCK_STREAM, 0 )) < 0 )
12 {
13 print strerror( $sock ) . "n";
14 exit(1);
15 }
16
17 /* Bind the socket to an address and a port: */
18 if( ($ret = bind( $sock, "10.31.172.77", 10000 )) < 0 )
19 {
20 print strerror( $ret ) . "n";
21 exit(1);
22 }
23
24 /*
25 * Listen for incoming connections on $sock.
26 * The '5' means that we allow 5 queued connections.
27 */
28 if( ($ret = listen( $sock, 5 )) < 0 )
29 {
30 print strerror( $ret ) . "n";
31 }
32
33 /* Accept incoming connections: */
34 if( ($msgsock = accept_connect( $sock )) < 0)
35 {
36 print strerror( $msgsock ) . "n";
37 exit(1);
38 }
39
40 /* Send the welcome-message: */
41 $message = "Welcome to my TCP-server!n";
42 if( ($ret = write( $msgsock, $message, strlen($message)) ) < 0 )
43 {
44 print strerror( $msgsock ) . "n";
45 exit(1);
46 }
47
48 /* Read/Receive some data from the client: */
49 $buf = '';
50 if( ($ret = read( $msgsock, $buf, 128 )) < 0 )
51 {
52 print strerror( $ret ) . "n";
53 exit(1);
54 }
55
56 /* Echo the received data back to the client: */
57 if( ($ret = write( $msgsock, "You said: $buf{CONTENT}n", strlen("You said: $buf{CONTENT}n")) ) < 0 )
58 {
59 print strerror( $ret ) . "n";
60 exit(1);
61 }
62
63 /* Close the communication-socket: */
64 close( $msgsock );
65
66 /* Close the global socket: */
67 close( $sock );
68 ?>
$socket = socket( AF_INET,SOCK_STREAM, 0);
// 首先建立一个 socket, 族为 AF_INET, 类型为 SOCK_STREAM.
// AF_INET = ARPA Internet protocols 即使用 TCP/IP 协议族
// SOCK_STREAM 类型提供了顺序的, 可靠的, 基于字节流的全双工连接.
// 由于该协议族中只有一个协议, 因此第三个参数为 0
bind ($sock, $address, $port)
// 再将这个 socket 与某个地址进行绑定.
listen( sockfd, MAX_CLIENT)
// 地址绑定之后, server 进入监听状态.
// MAX_CLIENT 是可以同时建立连接的 client 总数.
server 进入 listen 状态后, 等待 client 建立连接。
Client端要建立连接首先也需要初始化连接:
$socket= socket( AF_INET,SOCK_STREAM,0))
// 同样的, client 也先建立一个 socket, 其参数与 server 相同.
connect ($socket, $address, $service_port)
// client 使用 connect 建立一个连接.
当 client 建立新连接的请求被送到Server端时, server 使用 accept 来接受该连接:
accept_connect($sock)
// accept 返回一个新的文件描述符.
<?php
$cfgServer = "news.php.net";
$cfgPort = 119;
$cfgTimeOut = 10;
// open a socket
if(!$cfgTimeOut)
// without timeout
$usenet_handle = fsockopen($cfgServer, $cfgPort);
else
// with timeout
$usenet_handle = fsockopen($cfgServer, $cfgPort, &$errno, &$errstr, $cfgTimeOut);
if(!$usenet_handle) {
echo "Connexion failedn";
exit();
}
else {
echo "Connectedn";
$tmp = fgets($usenet_handle, 1024);
}
?>
GROUP ggg
<?php
//$cfgUser = "xxxxxx";
//$cfgPasswd = "yyyyyy";
$cfgNewsGroup = "alt.php";
// identification required on private server
if($cfgUser) {
fputs($usenet_handle, "AUTHINFO USER ".$cfgUser."n");
$tmp = fgets($usenet_handle, 1024);
fputs($usenet_handle, "AUTHINFO PASS ".$cfgPasswd."n");
$tmp = fgets($usenet_handle, 1024);
// check error
if($tmp != "281 Okrn") {
echo "502 Authentication errorn";
exit();
}
}
// select newsgroup
fputs($usenet_handle, "GROUP ".$cfgNewsGroup."n");
$tmp = fgets($usenet_handle, 1024);
if($tmp == "480 Authentication required for commandrn") {
echo "$tmpn";
exit();
}
$info = split(" ", $tmp);
$first = $info[2];
$last = $info[3];
print "First : $firstn";
print "Last : $lastn";
?>