一、网络编程中有两个主要的问题
1、如何准确的定位一台或多台主机;定位主机上的特定应用。
2、找到主机后如何可靠高效的进行数据传输。
二、网络编程中的两个要素。
1、对应上方问题1:IP和端口号。
2、对应上方问题2:提供网络通信协议:TCP/IP参考模型(应用层,传输层,网络层,物理+数据链路层)。
三、通信要素一:IP和端口号
1、IP:唯一表示Internet上的计算机(通信实体)。
2、在Java中使用InetAddress类代表IP。
3、IP分类:IPV4 和 IPV6;万维网 和 局域网。
4、域名:www.baidu.com(代替了其IP)
5、本地回路地址:127.0.0.1 对应 localhost
6、InetAddress类的使用:
实例化:两个方法:public static InetAddress getByName(String 域名或IP) 、public static InetAddress getLocalHost()(返回本机IP相关信息);
两个常用方法:public String getHostAddress()(获取IP地址)、public String getHostName()(获取域名)。
@Test
public void test() throws UnknownHostException {
//实例化方式1
InetAddress inet1 = InetAddress.getByName("www.baidu.com");
System.out.println(inet1);//www.baidu.com/110.242.68.3
//实例化方式2
InetAddress inet2 = InetAddress.getByName("110.242.68.3");
System.out.println(inet2);///110.242.68.3
InetAddress inet3 = InetAddress.getLocalHost();
System.out.println(inet3);//DESKTOP-IS216FL/192.168.40.1
//获取IP地址
System.out.println(inet1.getHostAddress());//110.242.68.4
//获取域名
System.out.println(inet1.getHostName());//www.baidu.com
}
7、端口号:正在计算机上运行的进程。
- 要求:不同进程有不同端口号。
- 范围:0~65535.
8、端口号与IP地址的组合得出一个网路套接字:Socket
四、实现TCP的网络编程
1、客户端发送信息给服务端
//客户端
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
try {
//1、创建Socket,指明服务端IP和端口号
socket = new Socket(InetAddress.getByName("127.0.0.1"), 8899);
//2、获取输出流,用于输出数据
outputStream = socket.getOutputStream();
//3、写出数据
outputStream.write("你好!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//4、关闭资源
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//服务端
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
//1、创建服务器端的ServerSocket,指明端口号
serverSocket = new ServerSocket(8899);
//2、调用accept()方法接收来自于客户端的socket
socket = serverSocket.accept();
//3、通过获取到的客户端socket获取输入流
inputStream = socket.getInputStream();
//4、读取输入流中的内容
//可能出现乱码
/*byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read()) != -1) {
System.out.println(new String(bytes,0,len));
}*/
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1) {
byteArrayOutputStream.write(bytes, 0, len);
}
System.out.println(byteArrayOutputStream.toString());
System.out.println(socket.getInetAddress().getHostName());
} catch (IOException e) {
e.printStackTrace();
} finally {
//5、资源关闭
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2、客户端发送文件给服务端,并提供反馈
@Test
public void client() {
Socket socket = null;
OutputStream outputStream = null;
BufferedInputStream inputStream = null;
InputStream socketInputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
//1、创建socket,指明服务端IP和端口
socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999);
//2、获取输出流
outputStream = socket.getOutputStream();
//3、创建输入流
inputStream = new BufferedInputStream(new FileInputStream(new File("360wallpaper.jpg")));
//4、读取文件
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1) {
//5、输出读取到的内容
outputStream.write(bytes, 0, len);
}
//6、结束文件输出
socket.shutdownOutput();
//7、接收来自服务端的反馈信息。
socketInputStream = socket.getInputStream();
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] bytes1 = new byte[1024];
int length;
while ((length = socketInputStream.read(bytes1)) != -1) {
byteArrayOutputStream.write(bytes1, 0, length);
}
System.out.println(byteArrayOutputStream.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
//8、关闭资源
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socketInputStream != null) {
try {
socketInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void server() {
ServerSocket serverSocket = null;
Socket socket = null;
InputStream inputStream = null;
BufferedOutputStream outputStream = null;
OutputStream socketOutputStream = null;
try {
//1、创建服务器socket,设置端口号
serverSocket = new ServerSocket(9999);
//2、获取客户端socket
socket = serverSocket.accept();
//3、获取输入流
inputStream = socket.getInputStream();
//4、创建输出流。用于文件写入磁盘
outputStream = new BufferedOutputStream(new FileOutputStream(new File("360wallpaper1.jpg")));
//5、读取传输内容
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1) {
//6、将读取到的数据写入磁盘
outputStream.write(bytes, 0, len);
outputStream.flush();
}
//7、给客户端反馈信息
socketOutputStream = socket.getOutputStream();
socketOutputStream.write("传输完成!!!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//8、关闭资源
if (socketOutputStream != null) {
try {
socketOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
五、UDP网络编程
@Test
public void sender(){
DatagramSocket socket = null;
try {
socket = new DatagramSocket();
String str = "UDP方式发送";
byte[] bytes = str.getBytes();
InetAddress inetAddress = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length,inetAddress,9999);
socket.send(packet);
} catch (IOException e) {
e.printStackTrace();
} finally {
socket.close();
}
}
@Test
public void receiver(){
DatagramSocket socket = null;
try {
socket = new DatagramSocket(9999);
byte[] bytes = new byte[1024];
DatagramPacket packet = new DatagramPacket(bytes,0,bytes.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
} catch (IOException e) {
e.printStackTrace();
} finally {
socket.close();
}
}
六、URL网络编程
1、URL:统一资源定位符,对应着互联网的某一资源地址。
2、格式:
http://localhost:8080 /test/login ? userName=tom&password=123456
协议 主机名 端口号 资源地址 参数列表
@Test
public void urlTest(){
try {
//实例化
URL url = new URL("https://i.cnblogs.com/posts/edit;postId=14826332");
//常用方法
//获取协议名
System.out.println(url.getProtocol());
//获取主机名
System.out.println(url.getHost());
//获取端口号
System.out.println(url.getPort());
//获取文件路径
System.out.println(url.getPath());
//获取文件名
System.out.println(url.getFile());
//获取url查询名
System.out.println(url.getQuery());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
//通过URL下载文件
@Test
public void downloadTest() {
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
URL url = new URL("https://i.cnblogs.com/posts/edit;postId=14826332");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
inputStream = urlConnection.getInputStream();
fileOutputStream = new FileOutputStream("test.txt");
byte[] bytes = new byte[1024];
int len;
while ((len = inputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes,0,len);
}
System.out.println("OVER");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileOutputStream!=null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream!=null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
最新2021整理收集的一些高频面试题(都整理成文档),有很多干货,包含mysql,netty,spring,线程, spring cloud、jvm、源码、算法等详细讲解,也有详细的学习规划图,面试题整理等,需要获取这些内容的朋友请加Q君样:753912071