网络编程
IP
InetAdress
//获取本机的InetAddress对象
InetAddress LocalHost = InetAddress.getLocalHost();
System.out.println(LocalHost);//LAPTOP-INPK0LQ4/192.168.1.3 主机名+ip
//根据主机名获取InetAdress对象
InetAddress host1 = InetAddress.getByName("LAPTOP-INPK0LQ4");
System.out.println(host1);//LAPTOP-INPK0LQ4/192.168.1.3
//根据域名获取InetAdress对象
InetAddress host2 = InetAddress.getByName("gaodebo.top");
System.out.println(host2);//gaodebo.top/39.106.90.162
//根据Inetadress 对象获取ip地址
String hostAddress = host1.getHostAddress();//192.168.1.3
//根据Inetadress 对象获取主机名/或者域名
String hostName = host1.getHostName();//LAPTOP-INPK0LQ4
String hostName2 = host2.getHostName();//
System.out.println(hostName2);//gaodebo.top
TCP
Socket
一个接口有两个流,输入流和输出流
服务器与客户端之间的通信
服务端
package net.learn;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class TCPServer {
public static void main(String[] args) throws IOException {
//在本机的9999端监听,当作服务器,要求9999端口没有被占用,serverSocket可以返回多个socket
ServerSocket serverSocket=new ServerSocket(9999);
System.out.println("服务器重在监听9999端口。。。");
//当没有客户端连接的时候,程序会阻塞,等待连接
//创建socket
Socket socket = serverSocket.accept();
//创建字节输入流,以读取来自客户端的程序
InputStream inputStream = socket.getInputStream();
byte[ ]buf=new byte[1024];
int len=0;
// while ((len=inputStream.read(buf))!=-1){
// System.out.println(new String(buf,0,len));
// }
//把字符流转换成字节流,方法更多,效率更高
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
//用readline读取的时候,不要用while readline不会返回null
String read=bufferedReader.readLine();
System.out.println(read);
//
OutputStream outputStream = socket.getOutputStream();
// outputStream.write("hello.client".getBytes(StandardCharsets.UTF_8));
// socket.shutdownOutput();//客户端读取结束的标志
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello,clinet\r");
bufferedWriter.newLine();//结束标记,插入一个换行符,要求对方使用newLine来读
bufferedWriter.flush();//如果使用字符流,需要手动刷新
bufferedReader.close();
bufferedWriter.close();
outputStream.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
客户端
package net.learn;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;
public class TCPClinet {
public static void main(String[] args) throws Exception {
//1.根据主机和端口,链接服务器
Socket socket = new Socket(InetAddress.getLocalHost(),9999);
//客户端发送“hello"给服务器
//socket 获得数据流
OutputStream outputStream = socket.getOutputStream();
//向数据流中写入东西
// outputStream.write("hello,server".getBytes(StandardCharsets.UTF_8));
// socket.shutdownOutput();//
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
bufferedWriter.write("hello,server\r");
bufferedWriter.newLine();//结束标记,插入一个换行符,要求对方使用newLine来读
bufferedWriter.flush();//如果使用字符流,需要手动刷新
InputStream inputStream = socket.getInputStream();
// byte[ ]buf=new byte[1024];
// int len=0;
// System.out.println("等待读取。。");
// while ((len=inputStream.read(buf))!=-1){
// System.out.println(new String(buf,0,len));
// }
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
// String read="";
// while((read=bufferedReader.readLine())!=null){
// System.out.println(read);
// }
String read=bufferedReader.readLine();
System.out.println(read);
//关闭流和socket
bufferedReader.close();
bufferedWriter.close();
outputStream.close();
inputStream.close();
socket.close();
}
}
UDP
了解即可