计算机网络是指将 地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信的管理和协调下,实现资源共享和信息传递的计算机系统
网络编程的目的转播交流信息、数据交互、通信
网络通信的要素 如何实现网络通信通信双方地址:
- ip端口号
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vasQjZhb-1648100202516)(F:学习资料学习图片OSI协议.jpg)]
IP
唯一定位一台网络上的计算机
127.0.0.1: 本机localhost
ip地址的分类
ipv4/ipv6
ipv4 127.0.0.1四个字节组成 0255,42亿;30亿都在北美,亚洲4亿。2011年就用尽ipv6 128位 8个无符号整数 公网(互联网) 私网(局域网)
ABCD类地址192.168.xxx.xxx专门给组织内部使用的
域名 记忆IP问题
代码package com.qiu.ip;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class InetAddressDemo1 {
public static void main(String[] args) {
try {
//查询本机地址
InetAddress ip = InetAddress.getByName("127.0.0.1");
InetAddress ip1 = InetAddress.getByName("localhost");
InetAddress ip2 = InetAddress.getLocalHost();
System.out.println("本地:"+ip);
System.out.println("本地:"+ip1);
System.out.println("本地:"+ip2);
//查询百度
InetAddress bai1 = InetAddress.getByName("www.baidu.com");
System.out.println("百度"+bai1);
System.out.println(bai1.getCanonicalHostName());//获取规范主机名
System.out.println(bai1.getHostAddress());//获取主机地址
System.out.println(bai1.getHostName());//获取主机域名
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
端口号
端口表示计算机上的一个程序的进程不同的进程有不同的端口号!用来区分软件被规定0~65535TCP/UDP:65535*2 tcp:80,udp80,单个协议下,端口号不能冲突端口分类
公有端口0~1023
HTTP:80HTTPS:443FTP:21Telent:23 程序注册端口1024~49
netstat -ano#查看所以的端口号 netstat -ano|findstr "端口号"#查看指定端口号 tasklist|findstr "端口号"#查看指定端口的进程 ctrl+shift+esc#任务管理器快捷键代码
package com.qiu.prot;
import java.net.InetSocketAddress;
public class InetSocketAddressDemo1 {
public static void main(String[] args) {
//创建端口对象
InetSocketAddress prot1 = new InetSocketAddress("127.0.0.1",1111);
InetSocketAddress prot2 = new InetSocketAddress("localhost",2222);
System.out.println(prot1);
System.out.println(prot2);
System.out.println(prot1.getAddress());//获取地址
System.out.println(prot1.getHostName());//获取主机名
System.out.println(prot1.getPort());//获取端口
}
}
通信协议
协议:相当于约定
**网络通信协议:**速率,传输码率,传输控制
TCP/IP协议簇: 实际上是一组协议
TCP:用户传输协议
UDP:用户数据报协议
TCP UDP对比
TCP 打电话
连接,稳定三次握手 四次挥手客户端、服务端传输完成,释放连接,效率低 UDP 发短信
不连接、不稳定客户端、服务端没有明确的界限不管有没有准备好,都可以发给你导弹DDOS:洪水攻击(饱和攻击) TCP 客户端
1.连接服务器
2.发送消息
package com.qiu.tcp;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
public class clientDemo1 {
public static void main(String[] args) {
//获取服务端的ip和端口号
InetAddress localhost =null;
int prot=9999;
//连接服务端
Socket socket=null;
//定义传输的数据
String con="连接服务端成功,连接成功,请指示";
//输出数据到服务端
OutputStream ps = null;
try {
localhost = InetAddress.getByName("localhost");
try {
socket = new Socket(localhost,prot);
} catch (IOException e) {
System.out.println("连接服务器失败");
}
ps=socket.getOutputStream();
ps.write(con.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流
if (ps!=null){
try {
ps.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(socket!=null){
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
服务端
1.创建服务器端口
2.等待客户端连接
3.接收数据
package com.qiu.tcp;
import com.sun.xml.internal.messaging.saaj.util.ByteOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class ServerDemo1 {
public static void main(String[] args) {
//定义一个地址
ServerSocket serverSocket=null;
//等待客户端连接
Socket accept = null;
//客户端的资源输入到程序
InputStream is =null;
//处理流
ByteOutputStream bos=null;
try {
serverSocket = new ServerSocket(9999);
accept = serverSocket.accept();
is = accept.getInputStream();
bos=new ByteOutputStream();
byte[] bytes=new byte[1024];
int len;
while((len = is.read(bytes)) != -1){
bos.write(bytes,0,len);
}
System.out.println(bos.toString());
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭资源
if (bos!=null){
try {
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (is!=null){
try {
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (accept!=null){
try {
accept.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (serverSocket!=null){
try {
serverSocket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}
文件的传输
客户端
package com.qiu.tcp;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
//客户端
public class ClientDemo2 {
public static void main(String[] args) throws Exception {
//连接服务器
Socket socket=new Socket(InetAddress.getByName("localhost"),3307);
//输入到服务端
OutputStream os = socket.getOutputStream();
//读取数据
FileInputStream fis=new FileInputStream(new File("ming.jpg"));
byte[] buss=new byte[1024];
int len;
while((len=fis.read(buss))!=-1){
//写入到服务端
os.write(buss,0,len);
}
//通知服务器我已经传输完毕
socket.shutdownOutput();
//服务端告诉客户端数据接收完毕
InputStream is = socket.getInputStream();
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] buss1=new byte[1024];
int len1;
while((len1=is.read(buss1))!=-1){
bos.write(buss1,0,len1);
}
//打印到控制台
System.out.println(bos);
//关闭资源
bos.close();
fis.close();
os.close();
socket.close();
}
}
服务端
package com.qiu.tcp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务端
public class ServerDemo2 {
public static void main(String[] args) throws Exception {
//创建服务器端口
ServerSocket serversocket=new ServerSocket(3307);
//等待客户端连接
Socket accept = serversocket.accept();
//接收客户端数据
InputStream is = accept.getInputStream();
//输出数据
FileOutputStream fos=new FileOutputStream(new File("zuo.jpg"));
byte[] buss;
buss = new byte[1024];
int len;
while((len=is.read(buss))!=-1){
//输出到程序
fos.write(buss,0,len);
}
//告诉客户端数据接收完毕
OutputStream os = accept.getOutputStream();
os.write("服务端接收数据完成".getBytes());
//关闭资源
os.close();
fos.close();
is.close();
accept.close();
serversocket.close();
}
}
UDP
发短信不用连接,需要知道对方的地址(和寄快递一样发件人需要知道收件人的地址已经姓名)
代码//发包
package com.qiu.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
public class ClientDemo1 {
public static void main(String[] args) throws Exception {
//创建一个Socket
DatagramSocket socket=new DatagramSocket();
//创建一个包
//创建发送的数据(相当于一个包裹)
String data="你好n我是中国移动t1008611n你已经欠费100元";
//创建ip和端口号(相当于地址和信息)
InetAddress loca=InetAddress.getByName("localhost");
int prot=2345;
//发送包
DatagramPacket packet=new DatagramPacket(data.getBytes(),0,data.getBytes().length,loca,prot);
//发送包
socket.send(packet);
//关闭资源
socket.close();
}
}
//收包
package com.qiu.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class ClientDemo2 {
public static void main(String[] args) throws Exception{
//创建Socket
DatagramSocket socket=new DatagramSocket(2345);
//创建读取的字节数组
byte[] buss=new byte[1024];
//读取数据
DatagramPacket packet=new DatagramPacket(buss,0,buss.length);
socket.receive(packet);//阻塞接收
//打印数据
System.out.println(new String(packet.getData(),0,packet.getLength()));
for (byte b : buss){
if(b!=0){
System.out.print(b);
}
}
//关闭资源
socket.close();
}
}
聊天功能实现
发送类
package com.qiu.udp1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketException;
//发送消息
public class Send implements Runnable{
//创建socket
DatagramSocket socket=null;
//创建输入流
BufferedReader ber=null;
//创建包
DatagramPacket data;
//自己的端口号
int ownProt;
//连接的ip
String toIP;
//连接的端口号
int toProt;
public Send(int ownProt, String toIP, int toProt) {
this.ownProt = ownProt;
this.toIP = toIP;
this.toProt = toProt;
try {
socket=new DatagramSocket(this.ownProt);
ber=new BufferedReader(new InputStreamReader(System.in));
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run(){
try {
while(true){
//读入输入的字符串
String information= ber.readLine();
//实例包
data=new DatagramPacket(information.getBytes(),0,information.getBytes().length,new InetSocketAddress(this.toIP,this.toProt));
socket.send(data);//发送包
if(information.equals("esc")){
return;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
接收类
package com.qiu.udp1;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;
public class Take implements Runnable{
//创建socket
DatagramSocket socket=null;
//创建接收包
DatagramPacket packet=null;
//自己的端口号
int ownProt;
//发件人
String name;
public Take(int ownProt,String name) {
this.ownProt = ownProt;
this.name=name;
try {
socket=new DatagramSocket(this.ownProt);
} catch (SocketException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
byte[] bus = new byte[1024];
//接收包
packet = new DatagramPacket(bus, 0, bus.length);
socket.receive(packet);//阻塞
String take = new String(packet.getData(), 0, packet.getLength());
System.out.println(name+":"+take);
if (take.equals("esc")) {
return;
}
} catch(Exception e){
e.printStackTrace();
}
}
}
}
学生类
package com.qiu.udp1;
public class Student {
public static void main(String[] args) {
new Thread(new Send(6666,"localhost",9999)).start();
new Thread(new Take(8888,"老师")).start();
}
}
老师类
package com.qiu.udp1;
public class Teacher {
public static void main(String[] args) {
new Thread(new Send(7777,"localhost",8888)).start( );
new Thread(new Take(9999,"学生")).start();
}
}
URL
代码
package com.qiu.url;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownLoad {
public static void main(String[] args) throws Exception {
//获取链接
URL url = new URL("https://p1.music.126.net/pj2t0wr1WdRF2KYcdtDlWg==/109951163788213977.jpg?param=50y50");
//打开连接
HttpURLConnection link = (HttpURLConnection) url.openConnection();
//把连接读入进来
InputStream inputStream = link.getInputStream();
//输出连接
FileOutputStream fos=new FileOutputStream("f.jpg");
byte[] bus=new byte[1024];
int len;
while((len=inputStream.read(bus))!=-1){
fos.write(bus,0,len);
}
System.out.println(url.getHost());
System.out.println(url.getPort());
System.out.println(url.getPath());
}
}



