网络层实验
实验1:获取主机IP地址判断是否可达
import java.net.InetAddress;
public class client {
public static void main(String[] args) throws Exception {
InetAddress localAddress=InetAddress.getLocalHost();
InetAddress baiduAddress=InetAddress.getByName("www.baidu.com");
System.out.println("local address:"+localAddress);
System.out.println("baidu address:"+baiduAddress);
System.out.println("local address:"+localAddress.getHostAddress());
System.out.println("baidu address:"+baiduAddress.getHostAddress());
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 {
InetAddress adr=InetAddress.getLocalHost();
String buf="new byte[1024]";
DatagramPacket demo1 =new DatagramPacket(buf.getBytes(StandardCharsets.UTF_8) ,buf.length());
DatagramPacket demo2 =new DatagramPacket(buf.getBytes(StandardCharsets.UTF_8) ,buf.length(),adr,80);
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 demo1=new DatagramSocket();
DatagramSocket demo2=new DatagramSocket(8001);
DatagramSocket demo3=new DatagramSocket(8001,InetAddress.getLocalHost());
byte[] buf=new byte[1024];
DatagramPacket datagramPacket=new DatagramPacket(buf,buf.length);
DatagramSocket datagramSocket=new DatagramSocket(8080);
datagramSocket.receive(datagramPacket);
System.out.println(Arrays.toString(datagramPacket.getData()) +"from"+ datagramPacket.getAddress().getHostAddress());
datagramSocket.close();
}
}
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 {
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());
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 {
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);
datagramSocket.close();
}
}
实验3:TCP通信
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket1=new ServerSocket();
ServerSocket serverSocket2=new ServerSocket(80);
ServerSocket serverSocket3=new ServerSocket(80,20);
ServerSocket serverSocket4=new ServerSocket(80,20, InetAddress.getByName("localhost"));
}
}
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 socket=new Socket();
Socket socket2=new Socket(InetAddress.getLocalHost(),80);
}
}
server.java
public class Server {
public static void main(String[] args) throws Exception {
new 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();
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();
}
}
|