通信双方地址
ip地址
端口号
准确定位到网络一台或者多台主机上的程序,需要网络ip和端口号。
协议:网络通信的规则
OSI七层网络模型,(理想化模型)
TCP/IP参考模型,(实际应用模型)
ip地址:Internet Protocol Address。
唯一定义一台网络上的计算机
127.0.0.1 :本机localhost (ipconfig查看本机网络地址)
ip的分类
ipv4/ipv6
ipv4 由四个字节组成,0-255之间,大概有42亿个,其中30亿在北美,亚洲只有4亿,2011用尽;
ipv6 128位,8个无符号整数用冒号隔开,基本用不完;
公网(互联网) - 私网(局域网)
IPV4地址中分为ABCDE五类地址,其中D类地址用于多播,E类地址保留为以后使用。
其中公网为公开的可在网络寻找的地址,不可重复,私网为局域网中的私有地址。
A类地址:第一字节为网络号,后三字节为主机号,1.0.0.1-126.155.255.254;
私有地址:10.X.X.X是私有地址
保留地址:127.X.X.X是保留地址,用于循环测试;
B类地址:前两个字节为网络号,后两个字节为主机号,128.0.0.1-191.255.255.254;
私有地址:172.16.0.0-172.31.255.255
保留地址:169.254.X.X,若IP地址是自动获取ip地址且网络上没有找到DHCP服务器就会得到其中一个ip。
C类地址:前三个字节为网络号,后一个字节为主机号,192.0.0.1-223.255.255.254
私有地址:192.168.X.X
端口表示计算机上一个程序的进程;
不同的进程有不同的端口号,用来区分软件。
TCP和UDP使用的端口号不冲突,范围都为0~65535;
端口分类:
公有端口 0~1023
FTP: 21
TELNET: 23
SMTP: 25
DNS: 53
TFTP: 69
HTTP: 80
HTTPS: 443
程序注册端口 1024~49151,分配给用户或者程序
Tomca:8080
MySQL:3306
Oracle:1521
动态、私有端口:49152~65535
#一些关于端口的dos命令 netstat -ano #查看所有端口号 netstat -ano|findstr "端口号/pid" #查看指定的端口或者进程的信息 tasklist|findstr "pid/进程名称" #查看指定的进程信息
协议:约定,即规则
TCP/IP协议簇:实际上是一组协议
TCP:用户传输协议
面向连接,稳定的
客户端与服务端之间
三次握手,四次挥手
传输完成才释放连接,效率低
UDP:用户数据协议
不连接不稳定
客户端与服务器间没有明确的界限
不需要双方确定准备就能发送
DDOS:洪水攻击(饱和攻击),发送多余数据包堵塞线路
IP:网络互联协议
客户端
1 . 连接服务器Socket
2 . 发送消息
package 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) { Socket socket = null; OutputStream os = null; try { //获取服务器的地址端口号 InetAddress serverIp = InetAddress.getByName("127.0.0.1"); int port =9999; //创建socket连接 socket = new Socket(serverIp, port); //发送消息 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 . 接受用户信息
package tcp; import java.io.ByteArrayOutputStream; 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 socket = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //创建一个服务器的地址 serverSocket = new ServerSocket(9999); while(true){ //等待客户端的连接 socket = serverSocket.accept(); //接受客户端发送的消息 is = socket.getInputStream(); //管道流 baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while((len=is.read(buffer))!=-1){ baos.write(buffer,0,len); } System.out.println(baos.toString()); } } catch (IOException e) { e.printStackTrace(); }finally{ if(baos!=null){ try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.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(); } } } } }
客户端
package tcp; import java.io.*; import java.net.InetAddress; import java.net.Socket; public class ClientDemo2 { public static void main(String[] args) throws Exception { //1创建一个socket链接 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9999); //2创建一个输出流 OutputStream os = socket.getOutputStream(); //3读取文件 FileInputStream fis = new FileInputStream(new File("1.jpg")); //4写出文件 byte[] bytes = new byte[1024]; int len; while((len=fis.read(bytes))!=-1){ os.write(bytes,0,len); } //通知服务器接收完毕 socket.shutdownOutput(); //5接受服务器的通知准备关闭连接 InputStream inputStream = socket.getInputStream(); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byte[] bytes1 = new byte[1024]; int len1; while((len1=inputStream.read(bytes1))!=-1){ byteArrayOutputStream.write(bytes1,0,len1); } System.out.println(byteArrayOutputStream.toString()); //6关闭资源 byteArrayOutputStream.close(); inputStream.close(); fis.close(); os.close(); socket.close(); } }
服务器
package tcp; import java.io.*; import java.net.ServerSocket; import java.net.Socket; public class ServerDemo2 { public static void main(String[] args) throws Exception { //1创建服务器 ServerSocket serverSocket = new ServerSocket(9999); //2监听客户端的连接 Socket socket = serverSocket.accept(); //3获取输入流 InputStream is = socket.getInputStream(); //4文件输出 FileOutputStream fos = new FileOutputStream(new File("receive.jpg")); byte[] bytes = new byte[1024]; int len; while((len=is.read(bytes))!=-1){ fos.write(bytes,0,len); } //5通知客户端接受完成可以关闭连接 OutputStream outputStream = socket.getOutputStream(); outputStream.write("接受完毕,可以关闭连接".getBytes()); //6关闭资源 outputStream.close(); fos.close(); is.close(); socket.close(); serverSocket.close(); } }