标签:soc 文件 计算机 输出流 while can ack bcd input
计算机网络:
计算机网络是指将地理位置不同的具有独立功能的多台计算机及其外部设备,通过通信线路连接起来,在网络操作系统,网络管理软件及网络通信协议的管理和协调下,实现资源共享和信息传递的计算机系统。
网络编程目的:
无限电台、传播交流信息、数据交换、通信......
想要达到这个效果需要什么:
概念B/S与C/:
javaweb: 网页编程 B/S
网络编程:TCP/IP C/S
如何实现网络的通信?
通信双方的地址:
规则:网络通信协议
TCP/IP参考模型:
小结:
ip地址:InetAdress
唯一定位一台网络上的计算机
127.0.0.1:本机地址 即localhost
ip地址的分类
ipv4/ipv6
IPV4:4个字节组成。约42亿,北美占了30亿,亚洲4亿,2011年已经用完
IPV6:12位。8个无符号整数,例如
1111:2222:3333:4444:5555:6666:aaaa:cccc
公网(互联网)和私网(局域网)
域名:记忆IP
测试代码:
package pers.mobian.tcpip;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class TestMyip {
public static void main(String[] args) throws UnknownHostException {
//查询本机地址
InetAddress inetAddress1 = InetAddress.getByName("127.0.0.1");
InetAddress inetAddress2 = InetAddress.getByName("localhost");
InetAddress inetAddress3 = InetAddress.getLocalHost();
System.out.println(inetAddress1);
System.out.println(inetAddress2);
System.out.println(inetAddress3);
//查询网络ip地址
InetAddress inetAddress4 = InetAddress.getByName("www.baidu.com");
System.out.println(inetAddress4);
//常用的方法
System.out.println(inetAddress4.getCanonicalHostName());//规范的名字
System.out.println(inetAddress4.getHostAddress());//ip
System.out.println(inetAddress4.getHostName());//域名或者自己电脑的名字
}
}
执行结果:
/127.0.0.1
localhost/127.0.0.1
DESKTOP-EGA979M/192.168.253.1
www.baidu.com/183.232.231.174
183.232.231.174
183.232.231.174
www.baidu.com
端口表示计算机上的一个程序的进程
不同得端口用来区分不同的软件
规定端口的范围:0~65535
TCP和UDP分别占有65535个,即电脑端口总数为65535*2
端口分类
公有端口 0~1023
程序注册端口:1024~49151,分配用户或者程序
动态、私有:49152~65535
netstat -ano //查看所有的端口
netstat -ano |findstr “端口号” //查看指定的端口号
tasklist|findstr "端口号" //查看指定端口号的进程
测试代码:
package pers.mobian.socket;
import java.net.InetSocketAddress;
public class TestSocket02 {
public static void main(String[] args) {
InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1",8080);
InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost",8080);
System.out.println(inetSocketAddress);
System.out.println(inetSocketAddress2);
System.out.println(inetSocketAddress.getAddress());//获取地址
System.out.println(inetSocketAddress.getHostName());//获取主机名字
System.out.println(inetSocketAddress.getPort());//获取端口
}
}
执行结果:
/127.0.0.1:8080
localhost/127.0.0.1:8080
/127.0.0.1
127.0.0.1
8080
网络通信协议:速率、传输码率、代码结构、传输控制......
TCP/IP协议簇:是一组协议
TCP与UDP的对比:
TCP:打电话
连接稳定
三次握手、四次挥手
//最少需要三次,保证稳定连接!<连接>
A:你瞅啥?
B:瞅你咋地?
A:干一场
//四次挥手 <断开>
A:我要走了
B:你真的要走了吗?
B:你真的真的要走了吗?
A:我真的要走了
客户端、服务器
传输完成、释放连接、效率低
UDP:发短信
客户端测试代码:
package pers.mobian.socket;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
//客户端
public class TestClientSocket {
public static void main(String[] args) {
Socket socket = null;
OutputStream os = null;
try {
//1、要知道服务器得地址、端口号
InetAddress serverIP = InetAddress.getByName("127.0.0.1");
int port = 9999;
//2、创建一个socket连接
socket = new Socket(serverIP, 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();
}
}
}
}
}
服务端测试代码:
package pers.mobian.socket;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;
//服务器端
public class TestServersSocket {
public static void main(String[] args) throws Exception {
//1、给自己新建一个端口号
ServerSocket serverSocket = new ServerSocket(9999);
//2、等待客户端进行连接
Socket socket = serverSocket.accept();
//3、读取客户端的消息
InputStream inputStream = socket.getInputStream();
//4、管道流
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer))!= -1){
baos.write(buffer,0,len);
}
System.out.println(baos.toString());
//5、关闭资源(后开先关)
baos.close();
inputStream.close();
socket.close();
serverSocket.close();
}
}
执行结果:
//服务器端显示
你好,默辨
测试代码:
//服务器
package pers.mobian.socket;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class TestServersSocket2 {
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("E:\\ideaWorkSpace\\pan\\day14_socket_01\\33.jpg"));
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1) {
fos.write(buffer,0,len);
}
//5、通知客户端,我接受完毕了
OutputStream os = socket.getOutputStream();
os.write("我接受完了".getBytes());
//6、关闭资源
fos.close();
is.close();
socket.close();
}
}
//客户端
package pers.mobian.socket;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
public class TestClientSocket2 {
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("E:\\ideaWorkSpace\\pan\\day14_socket_01\\3.jpg"));
//4、写出文件
byte[] buffer = new byte[1024];
int len;
while ((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//5、确定服务器接受完毕,才能断开连接
InputStream inputStream = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer2 = new byte[1024];
int len2;
while ((len2=inputStream.read(buffer2))!=-1){
baos.write(buffer2,0,len2);
}
System.out.println(baos.toString());
//6、关闭资源
fis.close();
os.close();
os.close();
socket.close();
}
}
服务端;
客户端:
UDP没有服务器和客户端得概念,只有发送端和接收端得概念
发送端测试代码:
package pers.mobian.socket;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
//发送端
public class TestClientSocket3 {
public static void main(String[] args) throws Exception {
//1、建立一个Socket
DatagramSocket socket = new DatagramSocket();
//2、建包
String msg = "你好";
InetAddress localhost = InetAddress.getByName("localhost");
int port = 9090;
//参数得列表分别是:数据,数据长度始,数据长度终,地址,端口号
DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,localhost,port);
//3、发送包
socket.send(packet);
//4、关闭资源
socket.close();
}
}
接收端测试代码:
package pers.mobian.socket;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
//接收端
public class TestServersSocket3 {
public static void main(String[] args) throws Exception {
//1、设置开放端口
DatagramSocket socket = new DatagramSocket(9090);
//2、准备接受数据包
byte[] buffer = new byte[1024];
DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length);
socket.receive(packet);
System.out.println(packet.getAddress().getHostAddress());
//3、将需要打印得数据进行格式转换
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
发送端测试代码:
package pers.mobian.udp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TestUDPSender {
public static void main(String[] args) throws Exception {
//设置开放端口
DatagramSocket socket = new DatagramSocket(8888);
while (true) {
//1、读取控制台的数据
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
//2、将控制台的一行语句赋值给data
String data = reader.readLine();
//3、将data包装成数据
byte[] datas = data.getBytes();
//4、将数据打包成package
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress("localhost", 6666));
//5、发送信息
socket.send(packet);
//判定的数据输入应为最初的输入数据,而非包装后的数据
if(data.equals("bye")){
break;
}
}
socket.close();
}
}
接受端测试代码:
package pers.mobian.udp;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TestUDPReceiver {
public static void main(String[] args) throws Exception {
//设置开放端口
DatagramSocket socket = new DatagramSocket(6666);
while (true) {
//1、准备接受包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
socket.receive(packet);
//2、打印接受到的数据
System.out.println(new String(packet.getData(),0,packet.getLength()));
//设置断开连接
byte[] data = packet.getData();
String receiverData = new String(data, 0, data.length);
if (receiverData.equals("bye")) {
break;
}
}
socket.close();
}
}
发送方测试代码:
package pers.mobian.udp;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
public class TestUDPSender2 implements Runnable {
private int fromPort;
private String toIP;
private int toPort;
DatagramSocket socket = null;
BufferedReader reader = null;
public TestUDPSender2(int fromPort, String toIP, int toPort) {
this.fromPort = fromPort;
this.toIP = toIP;
this.toPort = toPort;
try {
//设置开放端口
socket = new DatagramSocket(fromPort);
//1、读取控制台的数据
reader = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
try {
//2、将控制台的一行语句赋值给data
String data = reader.readLine();
//3、将data包装成数据
byte[] datas = data.getBytes();
//4、将数据打包成package
DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort));
//5、发送信息
socket.send(packet);
//判定的数据输入应为最初的输入数据,而非包装后的数据
if (data.equals("bye")) {
break;
}
} catch (Exception e) {
e.printStackTrace();
}
}
socket.close();
}
}
接收方测试代码:
package pers.mobian.udp;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
public class TestUDPReceiver2 implements Runnable {
DatagramSocket socket = null;
private int port;
private String msgFrom;
public TestUDPReceiver2(int port, String msgFrom) {
this.port = port;
this.msgFrom = msgFrom;
try {
//设置开放端口
socket = new DatagramSocket(port);
} catch (Exception e) {
e.printStackTrace();
System.out.println(88888);
}
}
@Override
public void run() {
while (true) {
//1、准备接受包裹
byte[] container = new byte[1024];
DatagramPacket packet = new DatagramPacket(container, 0, container.length);
try {
socket.receive(packet);//阻塞式接受
} catch (IOException e) {
e.printStackTrace();
System.out.println("111");
}
//2、打印接受到的数据
//设置断开连接
byte[] data = packet.getData();
String receiverData = new String(data, 0, data.length);
System.out.println(msgFrom + " : " + receiverData);
if (receiverData.equals("bye")) {
break;
}
}
socket.close();
}
}
测试代码:
package pers.mobian.udp;
public class TestUDPReceiver2Main1 {
public static void main(String[] args) {
new Thread(new TestUDPSender2(7777,"localhost",9999)).start();
new Thread(new TestUDPReceiver2(8888,"老师")).start();
}
}
测试代码:
package pers.mobian.udp;
public class TestUDPReceiver2Main2 {
public static void main(String[] args) {
new Thread(new TestUDPSender2(5555,"localhost",8888)).start();
new Thread(new TestUDPReceiver2(9999,"学生")).start();
}
}
执行结果:
//Main1
ni
学生 : jj
//Main2
老师 : ni
jj
URL类的使用:
测试代码:
package pers.mobian.udp;
import java.net.MalformedURLException;
import java.net.URL;
public class TestURL {
public static void main(String[] args) throws MalformedURLException {
URL url = new URL("http://localhost:8080/mobian/index.jsp?username=mobian&password=123");
System.out.println(url.getProtocol());//使用的协议
System.out.println(url.getHost());//主机IP
System.out.println(url.getPort());//使用的端口号
System.out.println(url.getPath());//文件路径
System.out.println(url.getFile());//文件的全路径
System.out.println(url.getQuery());//参数列表
}
}
执行结果:
http
localhost
8080
/mobian/index.jsp
/mobian/index.jsp?username=mobian&password=123
username=mobian&password=123
利用url路径下载网络资源测试代码:
package pers.mobian.udp;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class TestDownURL {
public static void main(String[] args) throws IOException {
//1、请求下载的地址
URL url = new URL("https://m701.music.126.net/20200121172558/e95dfac9e276e6d145b5f2638a036c2b/jdyyaac/035e/565f/055f/447bb91679a53ba93bc2258f72f57669.m4a");
//2、连接到这个资源HTTP
HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
FileOutputStream fos = new FileOutputStream("77.m4a");
byte[] buffer = new byte[1024];
int len;
while ((len = inputStream.read(buffer))!=-1){
fos.write(buffer,0,len);
}
fos.close();
inputStream.close();
urlConnection.disconnect();
}
}
标签:soc 文件 计算机 输出流 while can ack bcd input
原文地址:https://www.cnblogs.com/mobian/p/12222703.html