B站视频指路:尚硅谷Java入门视频教程(在线答疑+Java面试真题)_哔哩哔哩_bilibili
写在前面:马上秋招,打算从0开始再学一遍Java,开个知识点记录贴,就当做课堂笔记吧.
网络编程概述:
·Java是Internet上的语言 它从语言级上提供了对网络应用程序的支持 程序员很容易开发常见的网络应用程序
·Java提供的网络类库,可以实现无痛的网络连接,联网的底层细节被隐藏在Java的本机安装系统里,由JVM进行控制.并且Java实现了一个跨平台的网络库,程序员面对的是一个统一的网络编程环境.
网络基础
·计算机网络:
·网络编程的目的:
直接或间接地通过网络协议与其它计算机实现数据交换 进行通讯
·网络编程中有两个主要的问题
·如何准确地定位网络上一台或多台主机 定位主机上的特定的应用
·找到主机后如何可靠高效的进行数据传输
如何实现网络中的主机互相通信
·通信双方地址
IP、端口号
·一定的规则(即:网络通信协议 有两套参考模型)
OSI参考模型:过于理想化 未能在Internet上进行广泛推广
TCP/IP参考模型(也称TCP/IP协议):事实上的国际标准
应用层、传输层、网络层、物理和数据链路层
网络编程中的两个要素
①对应问题1:IP号和端口号
②对应问题2:网路通信协议
网络通信协议
| OSI参考模型 | TCP/IP参考模型 | TCP/IP参考模型各层对应协议 |
|---|---|---|
| 应用层 | 应用层 | HTTP、FTP、Telnet、DNS... |
| 表示层 | ||
| 会话层 | ||
| 传输层 | 传输层 | TCP、UDP... |
| 网络层 | 网络层 | IP、ICMP、ARP... |
| 数据链路层 | 物理+数据链路层 | link |
| 物理层 |
数据传输的例子
通信要素一:IP和端口号
端口号与IP地址的组合得出一个网络套接字:Socket
IP地址:
1.IP:唯一的标识Internet上的计算机(通信实体)
2.在Java中使用InetAddress类代表IP
3.IP分类: IPv4、IPv6; 万维网和局域网
4.域名:www.baidu.com ...
5.本地回路地址:127.0.0.1 对应着:localhost
6.如何实例化InetAddress:①getByName(String host)、getLocalHost()
常用方法:①getHostName()②getHostAddress()
package ipyoyoyo;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Inet {
public static void main(String[] args) {
try {
InetAddress byName = InetAddress.getByName("192.168.0.12");
System.out.println(byName);
InetAddress byName2 = InetAddress.getByName("www.atguigu.com");
System.out.println(byName2);
InetAddress byName3 = InetAddress.getByName("127.0.0.1");
System.out.println(byName3);
//获取本机ip
InetAddress localHost = InetAddress.getLocalHost();
System.out.println(localHost);
//getHostName
String hostName = byName2.getHostName();
System.out.println(hostName);
//getHostAddress
String hostAddress = byName2.getHostAddress();
System.out.println(hostAddress);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
端口号:
·不同进程有不同的端口号
·范围:被规定为一个16位整数0~65535
访问流程:
通信要素二:网络协议
传输层协议中有两个非常重要的协议:
①传输控制协议TCP(Transmission Control Protocol)
②用户数据报协议UDP(User Datagram Protocol)
·TCP/IP 以其两个主要协议:传输控制协议TCP和网络互连协议IP而得名,实际上是一组协议,包括多个具有不同功能且互为关联的协议
·IP(Internet Protocol)协议是网络层的主要协议,支持网间互连的数据通信
·TCP/IP协议模型从更实用的角度出发,形成了高效的四层体系结构,即物理链路层、IP层、传输层和应用层
TCP和UDP
·TCP协议
①使用TCP协议前,须先建立TCP连接,形成传输数据通道
②传输前采用"三次握手"方式,点对点通信,是可靠的
③TCP协议进行通信的两个应用进程:客户端、服务器
④在连接中进行大数据量的传输
⑤传输完毕,需释放已建立的链接,效率低
·UDP协议
①将数据、源、目的封装成数据包,不需要建立连接
②每个数据报的大小限制在64K内
③发送不管对方是否准备好,接收方收到也不确认,故是不可靠的
④可以广播发送
⑤发送数据结束时无需释放资源,开销小,速度快
实现TCP的网络编程:
例题:
1.客户端:
①创建Socket对象 指明服务器端的ip和端口号
②获取一个输出流 用于输出数据
③写出数据的操作
④资源的关闭
服务端:
①创建服务器端的SercerSocket,指明自己的端口号
②调用accept()表示接收来自于客服端的socket
③获取输入流
④读取输入流中的数据
⑤资源的关闭
package ipyoyoyo;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPTst {
@Test
//客户端
public void client() {
Socket socket = null;
OutputStream os = null;
try {
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
socket = new Socket(inetAddress,2211);
os = socket.getOutputStream();
os.write("hello".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os!=null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
//服务端 属于被动的 它一启动 就等客户端去链接了 所以先启动服务端
public void server(){
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try {
serverSocket = new ServerSocket(2211);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
//不建议这么写 可能会乱码 因为字节流 汉字可能乱码
// int len;
// byte[] buff = new byte[20];
// while ((len=inputStream.read(buff))!=-1){
// String s = new String(buff,0,len);
// System.out.println(s);
// }
byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buff = new byte[5];
int len;
while ((len=inputStream.read(buff))!=-1){
byteArrayOutputStream.write(buff,0,len);
}
System.out.println(byteArrayOutputStream);
System.out.println("收到了从:"+accept.getInetAddress().getHostAddress()+"发来的数据!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(accept!=null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(byteArrayOutputStream!=null)
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(inputStream!=null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(serverSocket!=null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.
package ipyoyoyo;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class lizier {
@Test
//客户端
public void client() {
FileInputStream fileInputStream = null;
Socket socket = null;
OutputStream os = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"),2210);
os = socket.getOutputStream();
fileInputStream = new FileInputStream("报名照片.jpg");
int len;
byte[] buf = new byte[1024];
while((len=fileInputStream.read(buf))!=-1){
os.write(buf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os!=null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fileInputStream!=null)
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
//服务端 属于被动的 它一启动 就等客户端去链接了 所以先启动服务端
public void server(){
ServerSocket serverSocket = null;
Socket accept = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
serverSocket = new ServerSocket(2210);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
fileOutputStream = new FileOutputStream("zhangkekeke.jpg");
//不建议这么写 可能会乱码 因为字节流 汉字可能乱码
// int len;
// byte[] buff = new byte[20];
// while ((len=inputStream.read(buff))!=-1){
// String s = new String(buff,0,len);
// System.out.println(s);
// }
byte[] buff = new byte[5];
int len;
while ((len=inputStream.read(buff))!=-1){
fileOutputStream.write(buff,0,len);
}
System.out.println("收到了从:"+accept.getInetAddress().getHostAddress()+"发来的数据!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(accept!=null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fileOutputStream!=null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(inputStream!=null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(serverSocket!=null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.
package ipyoyoyo;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class sdafsdf {
@Test
//客户端
public void client() {
FileInputStream fileInputStream = null;
Socket socket = null;
OutputStream os = null;
InputStream inputStream = null;
ByteArrayOutputStream bs = null;
try {
socket = new Socket(InetAddress.getByName("127.0.0.1"),2210);
os = socket.getOutputStream();
fileInputStream = new FileInputStream("报名照片.jpg");
int len;
byte[] buf = new byte[1024];
while((len=fileInputStream.read(buf))!=-1){
os.write(buf,0,len);
}
//关闭数据的输出 没这句话的话 服务端会一直在等 客户端传输
socket.shutdownOutput();
inputStream = socket.getInputStream();
bs = new ByteArrayOutputStream();
byte[]buff = new byte[20];
int len2;
while((len2=inputStream.read(buff))!=-1){
bs.write(buff,0,len2);
}
System.out.println(bs.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(os!=null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(socket!=null)
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fileInputStream!=null)
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(bs!=null)
bs.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(inputStream!=null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
//服务端 属于被动的 它一启动 就等客户端去链接了 所以先启动服务端
public void server(){
ServerSocket serverSocket = null;
Socket accept = null;
OutputStream outputStream = null;
InputStream inputStream = null;
FileOutputStream fileOutputStream = null;
try {
serverSocket = new ServerSocket(2210);
accept = serverSocket.accept();
inputStream = accept.getInputStream();
fileOutputStream = new FileOutputStream("z.jpg");
//不建议这么写 可能会乱码 因为字节流 汉字可能乱码
// int len;
// byte[] buff = new byte[20];
// while ((len=inputStream.read(buff))!=-1){
// String s = new String(buff,0,len);
// System.out.println(s);
// }
byte[] buff = new byte[5];
int len;
while ((len=inputStream.read(buff))!=-1){
fileOutputStream.write(buff,0,len);
}
System.out.println("收到了从:"+accept.getInetAddress().getHostAddress()+"发来的数据!");
//服务器端基于客户端反馈
outputStream = accept.getOutputStream();
outputStream.write("shoudaola!".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(accept!=null)
accept.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fileOutputStream!=null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(inputStream!=null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(serverSocket!=null)
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(outputStream!=null)
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}



