📄 ch6.txt
字号:
/* 代码6-1
* Created on 2005-5-14
*/
import java.net.*;
java.io.*;
public class LowPortScanner {
public static void main(String[] args) {
String host = "localhost";//设定为本地用户
if (args.length > 0) {
host = args[0];
}
for (int i = 1; i < 1024; i++) {
try {
//获取套接字对象
Socket s = new Socket(host, i);
System.out.println("There is a server on port " + i + " of "
+ host);
}
catch (UnknownHostException e) {
System.err.println(e);//打印出错误信息
break;
}
catch (IOException e) {
//必须是没有被使用过的端口
}
}
}
}
/* 代码6-2
* Created on 2005-5-14
*/
import java.net.*;
import java.io.*;
public class HighPortScanner {
public static void main(String[] args) {
//可以通过设置的参数给出主机名
String host = "localhost";
if (args.length > 0) {
host = args[0];
}
try {
InetAddress theAddress = InetAddress.getByName(host);
for (int i = 1024; i < 65536; i++) {
try {
Socket theSocket = new Socket(theAddress, i);//设定套接字
System.out.println("There is a server on port "
+ i + " of " + host);
}
catch (IOException e) {
}
}
}
catch (UnknownHostException e) {
System.err.println(e);//打印出错误信息
}
}
}
/* 代码6-3
* Created on 2005-5-14
*/
import java.net.*;
import java.io.*;
public class SocketInfo {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
try {
//获取套接字对象
Socket theSocket = new Socket(args[i], 80);
System.out.println("Connected to " +
theSocket.getInetAddress( )
+ " on port " + theSocket.getPort( ) + " from port "
+ theSocket.getLocalPort( ) + " of "
+ theSocket.getLocalAddress( ));//输出地址信息
}
catch (UnknownHostException e) {
System.err.println("I can't find " + args[i]);//输出错误信息
}
catch (SocketException e) {
System.err.println("Could not connect to " + args[i]);//输出错误信息
}
catch (IOException e) {
System.err.println(e);
}
}
}
}
/* 代码6-4
* Created on 2005-5-14
*/
import java.net.*;
import java.io.*;
public class DaytimeClient {
public static void main(String[] args) {
String hostname;
if (args.length > 0) {
hostname = args[0];
}
else {
//此地址为获取精确时间的地址
hostname = "tock.usno.navy.mil";
}
try {
Socket theSocket = new Socket(hostname, 13);//获得套接字
InputStream timeStream = theSocket.getInputStream( );//得到输入流
StringBuffer time = new StringBuffer( );
int c;
while ((c = timeStream.read( )) != -1) time.append((char) c);
String timeString = time.toString().trim( );//得到时间信息
System.out.println("It is " + timeString + " at " + hostname);
}
catch (UnknownHostException e) {
System.err.println(e);
}
catch (IOException e) {
System.err.println(e);
}
}
}
/* 代码6-5
* Created on 2005-5-14
*/
import java.net.*;
import java.io.*;
public class EchoClient {
public static void main(String[] args) {
//设定本地的主机地址
String hostname = "localhost";
if (args.length > 0) {
hostname = args[0];//设定主机信息
}
PrintWriter out = null;
BufferedReader networkIn = null;
try {
Socket theSocket = new Socket(hostname, 7);//设置套接字信息
networkIn = new BufferedReader(
new InputStreamReader(theSocket.getInputStream( )));
BufferedReader userIn = new BufferedReader(
new InputStreamReader(System.in));//通过修饰类装饰输入流
out = new PrintWriter(theSocket.getOutputStream( ));//输出流
System.out.println("Connected to echo server");
while (true) {
String theLine = userIn.readLine( );//按行读入信息
if (theLine.equals(".")) break;
//输出用户输入的信息
out.println(theLine);
out.flush( );
System.out.println(networkIn.readLine( ));
}
}
catch (IOException e) {
System.err.println(e);//打印错误信息
}
finally {
try {
if (networkIn != null) networkIn.close( );
if (out != null) out.close( );
}
catch (IOException e) {}
}
}
}
/* 代码6-6
* Created on 2005-5-14
*/
import java.net.*;
import java.io.*;
public class PortScanner {
public static void main(String[] args) {
String host = "localhost";
if (args.length > 0) {
host = args[0];
}
Socket connection = null;
try {
//通过主机名获取IP地址
InetAddress theAddress = InetAddress.getByName(host);
for (int i = 1; i < 65536; i++) {
try {
connection = new Socket(host, i);
System.out.println("There is a server on port "
+ i + " of " + host);
}
catch (IOException e) {
}
}
} // try的结尾
catch (UnknownHostException e) {
System.err.println(e);
}
finally {
try {
//关闭连接
if (connection != null) connection.close( );
}
catch (IOException e) {}
}
}
}
/* 代码6-7
* Created on 2005-5-14
*/
DGSClient.java
// DGSClient.java
import java.io.*;
import java.net.*;
class DGSClient
{
public static void main (String [] args)
{
String host = "localhost";
// 如果用户给出了字符串,这个字符串表示主机名字
if (args.length == 1)
host = args [0];
DatagramSocket s = null;
try
{
// 在指定的端口上创建DatagramSocket对象
s = new DatagramSocket ();
//创建一个字节数组用来保存数据包中的信息,
byte [] buffer;
buffer = new String ("Send me a datagram").getBytes ();
// 将主机名字转变为InetAddress类对象
// 此对象存有IP 主机的IP地址被DatagramPacket类使用
InetAddress ia = InetAddress.getByName (host);
//创建DatagramPacket对象用来封装字节数组的指针以及目标地址的信息。
//此目标地址的信息包含IP地址信息和端口号
DatagramPacket dgp = new DatagramPacket (buffer,buffer.length,ia,10000);
// 通过套接字发送数据包
s.send (dgp);
// 创建字符数组来保存反馈的信息
byte [] buffer2 = new byte [100];
// 创建DatagramPacket对象来构建一个缓冲区
// 用来保存服务器程序的反馈
dgp = new DatagramPacket (buffer2,buffer.length,ia,10000);
//从套接字中获取数据包
s.receive (dgp);
//打印出返回的数据,并保存在数据包中
System.out.println (new String (dgp.getData ()));
}
catch (IOException e)
{
System.out.println (e.toString ());
}
finally
{
if (s != null)
s.close ();
}
}
}
/* 代码6-8
* Created on 2005-5-14
*/
// DGSServer.java
import java.io.*;
import java.net.*;
class DGSServer
{
public static void main (String [] args) throws IOException
{
System.out.println ("Server starting ...\n");
// 在端口10000上创建套接字.
//从用户程序发送的包到这个端口
DatagramSocket s = new DatagramSocket (10000);
//packet.创建字符数组来保存数据包
byte [] data = new byte [100];
//创建数据包对象来封装指针
//以及字符数组和目的地址信息
DatagramPacket dgp = new DatagramPacket (data, data.length);
//进入一个循环,按Ctrl+C为退出
while (true)
{
// 从用户程序接收数据
s.receive (dgp);
// 打印数据内容
System.out.println (new String (data));
//将同样内容发回
s.send (dgp);
}
}
}
/* 代码6-9
* Created on 2005-5-14
*/
// MCClient.java
import java.io.*;
import java.net.*;
class MCClient
{
public static void main (String [] args) throws IOException
{
// ll创建MulticastSocket在端口10000上
//所有的组播数据包都在此端口上接收
MulticastSocket s = new MulticastSocket (10000);
InetAddress group = InetAddress.getByName ("231.0.0.1");
//加入组播组,如此才可以收到数据包
s.joinGroup (group);
// 从服务器程序读取数据
for (int i = 0; i < 10; i++)
{
// 任何一行不可以超过256个字符
byte [] buffer = new byte [256];
//对象不需要地址信息,因为套接字包含了地址
DatagramPacket dgp = new DatagramPacket (buffer,
buffer.length);
//接收了数据包
s.receive (dgp);
byte [] buffer2 = new byte [dgp.getLength ()];
//将数据复制到字节数组中
System.arraycopy (dgp.getData (),0,buffer2,0,dgp.getLength ());
//打印出来字节数组中的数据
System.out.println (new String (buffer2));
}
//离开组播组
s.leaveGroup (group);
//关闭套接字
s.close ();
}
}
/* 代码6-10
* Created on 2005-5-14
*/
// MCServer.java
import java.io.*;
import java.net.*;
class MCServer
{
public static void main (String[] args) throws IOException
{
System.out.println ("Server starting...\n");
//创建一个组播套接字,并不绑定到任何的端口上
MulticastSocket s = new MulticastSocket ();
//由于MulticastSocket是DatagramSocket的子类
// 可以用DatagramSocket s = new DatagramSocket ();
// 来代替以下的一行
// MulticastSocket s = new MulticastSocket ();
InetAddress group = InetAddress.getByName ("231.0.0.1");
//创建字节数组
byte [] dummy = new byte [0];
DatagramPacket dgp = new DatagramPacket (dummy,0,group,10000);
// 发送3000个字符串到这个端口
for (int i = 0; i < 30000; i++)
{
//从一个字符串上创建字节数组
byte [] buffer = ("Video line " + i).getBytes ();
//将这个字符串作为数据包的缓冲
dgp.setData (buffer);
//将字符数组的长度作为数据包的缓冲的长度
dgp.setLength (buffer.length);
//将数据包发送到整个组播组上
s.send (dgp);
}
//关闭这个套接字
s.close ();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -