IP地址
-
IP地址:inet Address
-
唯一定位一台网络上的计算机
-
127.0.0.1:本机local hostipv
-
IP地址的分类:
-
ipv4:127.2.2.1,四个字节组成,0-255,大概有42亿;30亿在北美,亚洲4亿。
-
IPv6: fe80::588c:5b23:d5c5:eed6%13,128位。
-
公网(互联网)——私网(局域网)
public class testInetAddress {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
System.out.println(inetAddress);
InetAddress inetAddress3 = InetAddress.getByName("localhost");
System.out.println(inetAddress3);
InetAddress inetAddress4 = InetAddress.getLocalHost();
System.out.println(inetAddress4);
//查询网站IP地址
InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress2);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
端口Port
端口表示计算机上的一个程序的进程
- 不同的进程有不同的端口号,用来区分软件
- 被规定0~65535
- TCP,UDP:65535*2 单个协议下,端口号是不能冲突的
- 端口分类
- 公有端口0~1023
- HTTP:80
- HTTPS:443
- FTP:21
- Telent:23
- 程序注册端口:1024~49151,分配用户或者程序
- Tomcat:8080
- MySql:3306
- Oracle:1521
- 动态,私有,
public class testSocketAddress {
public static void main(String[] args) {
InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1",8080);
InetSocketAddress socketAddress2 = new InetSocketAddress("localhost",8080);
System.out.println(socketAddress);
System.out.println(socketAddress2);
System.out.println(socketAddress.getAddress());
System.out.println(socketAddress.getHostName());
System.out.println(socketAddress.getPort());
}
运行结果:
通信协议
- 协议:就是约定,就好比我们现在所说的是普通话
- 网络通信协议:速率,传输码率,代码结构,传输控制等等
- TCP/IP协议簇:实际上就是一组协议,比较重要的是TCP和UDP,其中TCP是用户传输协议,UDP是用户数据报协议。比较出名的协议是TCP和IP,TCP是传输控制协议,IP是网络互联协议
TCP\IP 参考模型
TCP与UDP对比:
TCP和UDP协议是TCP/IP协议的核心。 TCP 传输协议:TCP 协议是一TCP (Transmission Control Protocol)和UDP(User Datagram Protocol)协议属于传输层协议。其中TCP提供IP环境下的数据可靠传输,它提供的服务包括数据流传送、可靠性、有效流控、全双工操作和多路复用。通过面向连接、端到端和可靠的数据包发送。通俗说,它是事先为所发送的数据开辟出连接好的通道,然后再进行数据发送;而UDP则不为IP提供可靠性、流控或差错恢复功能。一般来说,TCP对应的是可靠性要求高的应用,而UDP对应的则是可靠性要求低、传输经济的应用。
简单来说就是:
TCP相当与打电话,连接比较稳定,三次握手,四次挥手,客户端,服务端,传输完成,释放连接,效率较低。
UDP类似与发短信:不连接,不稳定,客户端服务端没有明确的界限。不管有没有准备好,都可以发给你
TCP
客户端
1.知道服务器的地址和端口号
2.连接服务器Socket
2.发送消息IO流
//客户端
public class TcpClictDemo1 {
public static void main(String[] args) {
Socket socket =null;
OutputStream os =null;
try {
//1.要知道服务器的地址和端口号
InetAddress byName = InetAddress.getByName("127.0.0.1");
int port=9999;
//2.创建一个socket连接
socket = new Socket(byName,port);
//3.发送消息,IO流
os = socket.getOutputStream();
os.write("你好".getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally {
if (os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
服务器
1.建立服务的端口ServerSocket
2.等待用户的连接accept
3.接收消息
//服务端
public class TcpServerDemo1 {
public static void main(String[] args) {
ServerSocket serverSocket =null;
Socket socket=null;
InputStream is=null;
ByteArrayOutputStream baos =null;
try {
//1.需要有一个地址
serverSocket = new ServerSocket(9999);
//2.等待客户端连接
socket=serverSocket.accept();
//3.读取客户端的信息
is=socket.getInputStream();
//4.管道流
baos = new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len;
while ((len=is.read())!=-1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
if (serverSocket!=null)
{
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (socket!=null)
{
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is!=null)
{
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (baos!=null)
{
try {
baos.close();
} catch (IOException e) {
e.printStackTrace();
}
TCP之文件图片的传输
//服务端
public class TcpServerDemo2 {
public static void main(String[] args) throws Exception {
//1.创建服务
ServerSocket serverSocket = new ServerSocket(9000);
//2.监听客户端的选择
Socket socket = serverSocket.accept();//阻塞式监听,会一直等待客户端的连接
//3.获取输入流
InputStream is = socket.getInputStream();
//4.文件输出
FileOutputStream fos = new FileOutputStream(new File("receive.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len=is.read(buffer))!=-1){
fos.write(buffer,0,len);
}
//字符串
//通知客户端接收完毕
OutputStream os = socket.getOutputStream();
os.write("已经接收完毕,可以断开了".getBytes());
//5.关闭
fos.close();
is.close();
socket.close();
serverSocket.close();
}
}
//客户端
public static void main(String[] args) throws Exception {
//1.创建一个Socket连接
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9000);
//2.创建一个输入流
OutputStream os = socket.getOutputStream();
//3.读取文件
FileInputStream fis = new FileInputStream(new File("picture (10).jpg"));
//4。写出文件
byte[] bufffer=new byte[1024];
int len;
while ((len=fis.read(bufffer))!=-1){
os.write(bufffer,0,len);
}
//通知服务器,我已经结束了
socket.shutdownOutput();//我已经传输完毕了
//字符串
//确定接收完毕,才能断开连接
InputStream inputStream = socket.getInputStream();
//对应服务端的String byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2=new byte[1024];
int len1;
while ((len1=inputStream.read(buffer2))!=-1){
baos.write(buffer2,0,len1);
}
System.out.println(baos.toString());
//5.关闭资源
baos.close();
fis.close();
os.close();
socket.close();
}
}
运行结果:
UDP
没有真正的服务端和客户端的连接,客户端相当于发快递,服务端相当于接收快递,父母知道我们的地址,给我们邮寄快递,我们在这个地方时刻准备着接收快递。
UDP消息发送
发送端
public static void main(String[] args) throws Exception {
//1.建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2.创建一个包
String msg="你好,服务器!";
//3.发送给谁
InetAddress localhost=InetAddress.getByName("localhost");
int port=9090;
//发送数据:五个参数——>数据,数据的其实长度,数据的结束长度,要发给谁,端口号
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
//4.发送包
socket.send(packet);
socket.close();
}
}
接收端
public static void main(String[] args) throws Exception {
//开放端口
DatagramSocket socket = new DatagramSocket(9090);
//接收数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
//输出
System.out.println(packet.getAddress().getHostAddress());
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
运行结果:
UDP基本聊天实现
发送方:
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(8888);
//准备数据,控制台读取数据System.in
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true){
String data = reader.readLine();
byte [] datas=data.getBytes();
DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",6666));
socket.send(packet);
if (datas.equals("bye")){
break;
}
}
socket.close();
}
}
接收方:
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket(6666);
while (true){
//接收包裹
byte [] container=new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);//阻塞式接收
//断开连接 bye
byte[] data = packet.getData();
String receiveData =new String(data,0,data.length);
System.out.println(receiveData);
if (receiveData.equals("bye"))
{
break;
}
}
socket.close();
运行结果:
UDP多线程在线咨询
发送方:
public class TalkSend implements Runnable {
DatagramSocket socket = null;
BufferedReader reader = null;
private int fromPort;
private String toIP;
private int toPort;
public TalkSend(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
socket = new DatagramSocket(fromPort);
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
//准备数据,控制台读取数据System.in
while (true) {
try {
String data = reader.readLine();
byte[] datas = data.getBytes();
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, toPort));
socket.send(packet);
if (datas.equals("bye")) {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
socket.close();
}
}
接收方:
DatagramSocket socket=null;
private int port;
private String msgFrom;
public TalkReceiveDemo(int port,String msgFrom) {
this.port = port;
this.msgFrom=msgFrom;
try {
socket=new DatagramSocket(port);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true){
try{
//接收包裹
byte [] container=new byte[1024];
DatagramPacket packet = new DatagramPacket(container,0,container.length);
socket.receive(packet);//阻塞式接收
//断开连接 bye
byte[] data = packet.getData();
String receiveData =new String(data,0,data.length);
System.out.println(msgFrom+":"+receiveData);
if (receiveData.equals("bye"))
{
break;
}
}catch (Exception e){
e.printStackTrace();
}
}
socket.close();
}
线程开启
//学生
public class TalkStudent {
public static void main(String[] args) {
new Thread(new TalkSend(7777,"localhost",9999)).start();
new Thread(new TalkReceiveDemo(8888,"老师")).start();
}
}
//老师
public class TalkTeacher {
public static void main(String[] args) {
new Thread(new TalkSend(5555,"localhost",8888)).start();
new Thread(new TalkReceiveDemo(9999,"学生")).start();
}
}
运行结果:
URL
URL的认识:
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/helloword/index.jsp?username=shmily&password=123");
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());//文件
System.out.println(url.getQuery());//参数
}
运行结果:
下载网络资源:
public static void main(String[] args) throws Exception {
//1.下载地址
URL url = new URL("网址地址");
//2.连接到这个资源的HTTP
HttpURLConnection urlConnection =(HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("下载后名字");
byte[] buffer = new byte[1024];
int len;
while ((len=inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();//断开连接
}
}