< linux网络编程工具>>配套源码

源代码在线查看: 程嶏清单 17-4.txt

软件大小: 1772 K
上传用户: myhpgnl
关键词: linux gt lt 网络
下载地址: 免注册下载 普通下载 VIP

相关代码

				程序清单17-4:client.pl
				#!/usr/bin/perl
				 #
				 # define constants for talking to server
				 #
				 $PACKIT = 'S n C4 x8';
				 $AF_INET = 2;
				 $SOCK_STR = 1;     # STREAMS
				 $DEF_PROTO = 0;    #default protocol of IP
				 $port = 6668 unless $port;
				    
				 #
				 # Get the name of the server
				 #
				 ($name, $aliases, $addrtype, $len, @addrs) = gethostbyname("www.ikra.com"); 
				 ($a,$b,$c,$d) = unpack('C4',$addrs[0]);
				 print "Server Name=$name, Server Address= $a.$b.$c.$d\n"; 
				 $that = pack($PACKIT,$AF_INET,$port,$addrs[0]);
				  
				 #
				 # Confine yourself to the local host machine address of 127.0.0.1 
				 #
				 $this = pack($PACKIT,$AF_INET,$port,127,0,0,1);
				    
				 #
				 # Disable buffering on this socket.
				 #
				 select(CLIENT_SOCKET);
				 $| = 1;
				 select(stdout);
				  
				  
				 socket(CLIENT_SOCKET,$AF_INET,$SOCK_STR,$DEF_PROTO) 
				or  die "$0: Cannot open socket\n";
				 print "Created socket\n";
				
				 #
				 # Attempt to connect to server.
				 #
				 do
				 {
				  sleep(1);
				   $result = connect(CLIENT_SOCKET,$that);
				   i f (result != 1) {
				       print "Sleeping\n"; 
				      }
				
				 } while ($result != 1);
				
				 sleep(5);
				 print "After connection\n";
				  
				 #
				 # send data to server
				 #
				 # write(CLIENT_SOCKET,"hello",5);
				
				 read(CLIENT_SOCKET,$buf,100);
				
				 print "[" . $buf . "]\n";
				 close(CLIENT_SOCKET);
				# end of client program
							

相关资源