网络层实验
实验1:获取主机IP地址判断是否可达
import java.net.InetAddress;
public class client {
public static void main(String[] args) throws Exception {
//输出主机的IP名/地址和百度的主机名/ip地址
InetAddress localAddress=InetAddress.getLocalHost();
InetAddress baiduAddress=InetAddress.getByName("www.baidu.com");
System.out.println("local address:"+localAddress);
System.out.println("baidu address:"+baiduAddress);
//输出主机IP地址和百度IP地址
System.out.println("local address:"+localAddress.getHostAddress());
System.out.println("baidu address:"+baiduAddress.getHostAddress());
//输出3000表内是否可以到达百度
System.out.println("is possible to reach in 30000 seconds?"+baiduAddress.isReachable(3000));
//输出百度的主机名
System.out.println("baidu's host name:"+baiduAddress.getHostName());
}
}
实验2 UDP通信
java中的DatagramPacket类就相当于一个集装箱,用来封装UDP通信中发送或者接收的数据
关于DatagramPacket的构造方法与常用方法
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class client {
public static void main(String[] args) throws Exception {
//DatagramPocket的构造方法
InetAddress adr=InetAddress.getLocalHost();
String buf="new byte[1024]";
//1.指定封装字节数组和数据的大小,也就是缓冲区的大小,只能用于接收端,因为没有IP地址的端口号
DatagramPacket demo1 =new DatagramPacket(buf.getBytes(StandardCharsets.UTF_8) ,buf.length());
//2.指定封装字节数和数据大小(缓冲区的大小),数据包的目的IP地址和源端口号,常用于发送端
DatagramPacket demo2 =new DatagramPacket(buf.getBytes(StandardCharsets.UTF_8) ,buf.length(),adr,80);
//DatagramPocket类中常用的方法
System.out.println(demo2.getAddress());
System.out.println(demo2.getPort());
System.out.println(Arrays.toString(demo2.getData()));
System.out.println(demo2.getLength());
}
}
java中的DatagramSocket类用于数据传输
DatagramSocket类的构造方法
import javax.xml.crypto.Data;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class client {
public static void main(String[] args) throws Exception {
//DatagramSocket的构造方法
//该构造方法用创建发送端的DatagramPacket对象,没有指定端口号,系统会自己分配一个端口号
DatagramSocket demo1=new DatagramSocket();
//该构造方法用于创建发送端或者接收端的端口号的DatagramPacket对象,指定端口号
DatagramSocket demo2=new DatagramSocket(8001);
//指定端口号和IP地址
DatagramSocket demo3=new DatagramSocket(8001,InetAddress.getLocalHost());
//DatagramSocket的常用方法
//接收数据
byte[] buf=new byte[1024]; //定义接收数据的数组
DatagramPacket datagramPacket=new DatagramPacket(buf,buf.length);//封装数据缓冲区
DatagramSocket datagramSocket=new DatagramSocket(8080);//定义接收的数据,端口为8080
datagramSocket.receive(datagramPacket);//等待接收数据,如果没有数据就会被堵塞
System.out.println(Arrays.toString(datagramPacket.getData()) +"from"+ datagramPacket.getAddress().getHostAddress());
datagramSocket.close();//关闭当前Socket,通知驱动程序释放为这个socket保留的资源
}
}
socket实现udp通信
服务器端
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.Arrays;
public class server {
public static void main(String[] args) throws Exception {
//定义接收数据的socket
DatagramSocket datagramSocket=new DatagramSocket(3000);
//定义接收数据的数组
byte[] buf=new byte[1024];
//定于接收数据的包
DatagramPacket datagramPacket=new DatagramPacket(buf,buf.length);
//等待接收数据,如果没有数据就会被堵塞
datagramSocket.receive(datagramPacket);
//输出接收到的数据
System.out.println("receive...");
System.out.println(new String(datagramPacket.getData(),0,datagramPacket.getLength()) +" from "+ datagramPacket.getAddress().getHostAddress());
//关闭当前Socket,通知驱动程序释放为这个socket保留的资源
datagramSocket.close();
}
}
客户端
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
public class client {
public static void main(String[] args) throws Exception {
//定义发送数据的socket
DatagramSocket datagramSocket=new DatagramSocket();
//定义发送的数据
String str="hello";
//数据装包
DatagramPacket datagramPacket=new DatagramPacket(str.getBytes(StandardCharsets.UTF_8),str.length(),InetAddress.getByName("localhost"),3000);
//发送信息
datagramSocket.send(datagramPacket);
//关闭当前Socket,通知驱动程序释放为这个socket保留的资源
datagramSocket.close();
}
}
实验3:TCP通信
- 服务器端的ServerSocket构造函数
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
public class Server {
public static void main(String[] args) throws IOException {
//ServerSocket构造方法
//1.没有绑定端口号,这样的对象创建的服务器没有监听任何端口
ServerSocket serverSocket1=new ServerSocket();
//2.让服务器监听一个指定的端口号
ServerSocket serverSocket2=new ServerSocket(80);
//3.第二个参数指定在服务器繁忙时,可以和服务器保持连接请求的等待客户数量
ServerSocket serverSocket3=new ServerSocket(80,20);
//4.第三个参数明确规定socket在哪块网卡(哪个IP地址)上等待客户的连接请求
ServerSocket serverSocket4=new ServerSocket(80,20, InetAddress.getByName("localhost"));
}
}
- 客户端的Socket构造函数
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
public class Client {
public static void main(String[] args) throws IOException {
//socket构造函数
//1.指定对象,没有连接服务器,构造方法创建之后还需要调用connect来完成与指定服务器的连接
Socket socket=new Socket();
//2.会根据指定的ip和端口号去连接服务器
Socket socket2=new Socket(InetAddress.getLocalHost(),80);
}
}
- TCP网络程序
server.java
public class Server {
public static void main(String[] args) throws Exception {
new TCPServer().listen();//创建TCPServer对象调用listen方法
}
}
TCPServer.java
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class TCPServer {
public static final int PORT=7788;//设定一个端口号
public void listen() throws Exception{
ServerSocket serverSocket=new ServerSocket(PORT);//创建服务器套接字
Socket client=serverSocket.accept();//接收数据放在client里
OutputStream os=client.getOutputStream();//获取客户端的数据流
System.out.println("connect successfully!");
os.write("data...".getBytes(StandardCharsets.UTF_8));
Thread.sleep(5000);
System.out.println("end");
os.close();
client.close();
}
}
Client.java
public class Client {
public static void main(String[] args) throws Exception {
new TCPClient().connect();
}
}
TCPClient.java
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
public class TCPClient {
public static final int PORT=7788;
public void connect() throws Exception{
Socket client=new Socket(InetAddress.getLocalHost(),PORT);//创建一个套接字并连接到给出的地址和端口号的计算机
InputStream is=client.getInputStream();//得到接收的数据流
byte[] buf=new byte[1024];
int len=is.read(buf); //把数据读到缓冲区中
System.out.println(new String(buf,0,len));//读出数据
client.close();
}
}