0
点赞
收藏
分享

微信扫一扫

第十二章 屏幕后处理效果

1kesou 19小时前 阅读 0

Day38 网络编程(了解即可)

1. 计算机网络

在这里插入图片描述

2. 网络编程

3. 网络模型

在这里插入图片描述

4. 网络编程三要素

在这里插入图片描述

4.1. IP地址

4.2. 端口号

4.3. 网络协议

5. InetAddress类

public class Test01 {
	public static void main(String[] args) throws UnknownHostException {
		
		//获取本机的IP地址
//		InetAddress localHost = InetAddress.getLocalHost();
//		System.out.println(localHost);
		
		//获取域名对应的服务器地址
//		InetAddress byName = InetAddress.getByName("www.baidu.com");
//		System.out.println(byName);
		
		//获取域名对应的所有的服务器地址
		InetAddress[] allByName = InetAddress.getAllByName("www.baidu.com");
		for (InetAddress inetAddress : allByName) {
			System.out.println(inetAddress);
		}
        
	}
}

6. Socket

7. TCP编程

7.1. 简单的TCP通信

在这里插入图片描述

//服务端
public class Server {

	public static void main(String[] args) throws IOException {
		
		//大堂经理
		ServerSocket server = new ServerSocket(8080);
		
		//18号技师
		//注意:accept()是线程阻塞的方法,该方法会等待客户端的连接成功后才生成一个Socket对象与之交互
		Socket socket = server.accept();
		
		//2.接受来自客户端的数据
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
		String readLine = br.readLine();
		System.out.println(readLine);
		
		
		//3.向客户端发送数据
		PrintStream ps = new PrintStream(socket.getOutputStream());
		ps.println("18号技师:今年18岁");
		
		br.close();
		ps.close();
		server.close();
	}
}
//客户端
public class Client {

	public static void main(String[] args) throws UnknownHostException, IOException {
		
		//注意:关闭流相当于关闭Socket!!!
		
		//巴得伟
		Socket socket = new Socket("127.0.0.1", 8080);
		
		//1.向服务端发送数据
		PrintStream ps = new PrintStream(socket.getOutputStream());
		ps.println("巴得伟:小妹妹,你多大了?");
		
		
		//4.接受来自服务端的数据
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
		String readLine = br.readLine();
		System.out.println(readLine);
		
		ps.close();
		br.close();
		socket.close();
		
		
	}
}

7.2 TCP案例

7.2.1 传输文件
public class Server {
	public static void main(String[] args) throws IOException {
		
		ServerSocket server = new ServerSocket(8080);//端口号要对应
		
		Socket socket = server.accept();
		
		BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.mp4"));
		
		byte[] bs = new byte[1024];
		int len;
		while((len = bis.read(bs)) != -1){
			bos.write(bs, 0, len);
		}
		
		bis.close();
		bos.close();
		server.close();
	}
}
public class Client {
	public static void main(String[] args) throws UnknownHostException, IOException {
		
		Socket socket = new Socket("127.0.0.1", 8080);
		
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("视频.mp4"));
		BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
		
		byte[] bs = new byte[1024];
		int len;
		while((len = bis.read(bs)) != -1){
			bos.write(bs, 0, len);
		}
		
		bis.close();
		bos.close();
		socket.close();
	}
}
7.2.2 单聊
public class Server {
	public static void main(String[] args) throws IOException {
		
		ServerSocket server = new ServerSocket(8080);
		
		Socket socket = server.accept();
		
		new ReceiveThread(socket).start();
		
		Scanner scan = new Scanner(System.in);
		PrintStream ps = new PrintStream(socket.getOutputStream());
		while(true){
			ps.println("18号技师:" + scan.next());
		}
		
	}
}
public class Client {
	public static void main(String[] args) throws UnknownHostException, IOException {
		
		Socket socket = new Socket("127.0.0.1", 8080);
		
		new ReceiveThread(socket).start();
		
		Scanner scan = new Scanner(System.in);
		PrintStream ps = new PrintStream(socket.getOutputStream());
		while(true){
			ps.println("巴得伟:" + scan.next());
		}
		
	}
}
public class ReceiveThread extends Thread{
	
	private Socket socket;
	
	public ReceiveThread(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
			while(true){
				String readLine = br.readLine();
				System.out.println(readLine);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}		
		
	}
}
7.2.3 群聊

在这里插入图片描述

public class Server {
	
	public static final ConcurrentHashMap<String, PrintStream> map = new ConcurrentHashMap<>();

	public static void main(String[] args) throws IOException {
		
		ServerSocket server = new ServerSocket(8080);
		
		while(true){
			
			Socket socket = server.accept();
			
			String ip = socket.getInetAddress().toString();
			PrintStream ps = new PrintStream(socket.getOutputStream());
			map.put(ip, ps);
			
			new ServerThread(socket).start();
		}
		
	}
}
public class Client {
	public static void main(String[] args) throws UnknownHostException, IOException {
		
		Socket socket = new Socket("127.0.0.1", 8080);
		
		new ReceiveThread(socket).start();
		
		Scanner scan = new Scanner(System.in);
		PrintStream ps = new PrintStream(socket.getOutputStream());
		while(true){
			ps.println("巴得伟:" + scan.next());
		}
		
	}
}
public class ReceiveThread extends Thread{
	
	private Socket socket;
	
	public ReceiveThread(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
			while(true){
				String readLine = br.readLine();
				System.out.println(readLine);
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}
}
public class ServerThread extends Thread{
	
	private Socket socket;
	
	public ServerThread(Socket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		
		try {
			//接受当前客户端的消息
			BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "GBK"));
			while(true){
				String readLine = br.readLine();
				System.out.println(readLine);
				
				//发送给其他客户端消息
				Set<Entry<String,PrintStream>> entrySet = Server.map.entrySet();
				for (Entry<String, PrintStream> entry : entrySet) {
					String ip = entry.getKey();
					PrintStream ps = entry.getValue();
					
					if(!socket.etInetAddress().toString().equals(ip)){
						ps.println(readLine);
					}
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}				
		
	}
}

8.TCP三次握手 和 四次挥手

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

9. UDP编程

9.1 简单的UDP通信

public class Client01 {
	public static void main(String[] args) throws IOException {

		DatagramSocket socket = new DatagramSocket(7070);
		
		//1.向客户端2发送数据
		byte[] buf = "用良心做教育".getBytes();
		DatagramPacket p = new DatagramPacket(buf , 0, buf.length, InetAddress.getByName("127.0.0.1"), 8080);
		socket.send(p);
		
		//4.接受来自客户端1的数据
		buf = new byte[1024];
		p = new DatagramPacket(buf , buf.length);
		socket.receive(p);
		System.out.println(new String(buf).trim());
		
		socket.close();
	}
}
public class Client02 {
	public static void main(String[] args) throws IOException {
		
		DatagramSocket socket = new DatagramSocket(8080);
		
		//2.接受来自客户端1的数据
		byte[] buf = new byte[1024];
		DatagramPacket p = new DatagramPacket(buf , buf.length);
		socket.receive(p);
		System.out.println(new String(buf).trim());
		
		//3.向客户端2发送数据
		buf = "做真实的自己".getBytes();
		p = new DatagramPacket(buf , 0, buf.length, InetAddress.getByName("127.0.0.1"), 7070);
		socket.send(p);
		
		socket.close();
	}
}

9.2 UDP案例 之 单聊

public class Client01 {
	public static void main(String[] args) throws SocketException {
		
		DatagramSocket socket = new DatagramSocket(7070);
		
		new ReceiveThread(socket).start();
		new SendThread("巴得伟", "127.0.0.1", 8080, socket).start();
	}
}
public class Client02 {
	public static void main(String[] args) throws SocketException {
		
		DatagramSocket socket = new DatagramSocket(8080);
		
		new ReceiveThread(socket).start();
		new SendThread("王益升", "127.0.0.1", 7070, socket).start();
	}
}
public class ReceiveThread extends Thread{
	
	private DatagramSocket socket;
	
	public ReceiveThread(DatagramSocket socket) {
		this.socket = socket;
	}

	@Override
	public void run() {
		while(true){
			byte[] buf = new byte[1024];
			DatagramPacket p = new DatagramPacket(buf , buf.length);
			try {
				socket.receive(p);
				System.out.println(new String(buf).trim());
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
}
public class SendThread extends Thread{
	
	private String nickName;
	private String ip;
	private int port;
	private DatagramSocket socket;
	
	public SendThread(String nickName, String ip, int port, DatagramSocket socket) {
		this.nickName = nickName;
		this.ip = ip;
		this.port = port;
		this.socket = socket;
	}
	
	@Override
	public void run() {
		Scanner scan = new Scanner(System.in);
		while(true){
			byte[] buf = (nickName + ":" + scan.next()).getBytes();
			try {
				DatagramPacket p = new DatagramPacket(buf, buf.length, InetAddress.getByName(ip), port);
				socket.send(p);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}

9.3 TCP VS UDP

比较TCPUDP
是否连接面向连接无面向连接
传输可靠性可靠不可靠
应用场合传输大量数据少量数据
速度

10.HTTP

10.1 需求:获取淘宝商品周边类别

public class Test01 {
	public static void main(String[] args) throws IOException {
		
		String path = "https://suggest.taobao.com/sug?code=utf-8&q=%E8%80%90%E5%85%8B&callback=cb";
		
		//创建链接对象
		URL url = new URL(path);
		//获取连接对象
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		
		//设置参数
		connection.setConnectTimeout(5000);//设置连接超时时间
		connection.setReadTimeout(5000);//设置读取数据超时时间
		connection.setDoInput(true);//设置是否允许使用输入流
		connection.setDoOutput(true);//设置是否允许使用输出流
		
		//获取响应状态码
		int code = connection.getResponseCode();
		if(code == HttpURLConnection.HTTP_OK){
			
			BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
			char[] cs = new char[1024];
			int len;
			while((len = br.read(cs)) != -1){
				System.out.println(new String(cs, 0, len));
			}
			
			
		}else if(code == HttpURLConnection.HTTP_NOT_FOUND){
			System.out.println("页面未找到");
		}				
		
	}
}

10.2 需求:下载图片

public class Test02 {
	public static void main(String[] args) throws IOException {
		
		String path = "https://wx2.sinaimg.cn/mw690/e2438f6cly1hoo3qpm7vrj21111jk4mn.jpg";
		
		//创建链接对象
		URL url = new URL(path);
		//获取连接对象
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		
		//设置参数
		connection.setConnectTimeout(5000);//设置连接超时时间
		connection.setReadTimeout(5000);//设置读取数据超时时间
		connection.setDoInput(true);//设置是否允许使用输入流
		connection.setDoOutput(true);//设置是否允许使用输出流
		
		//获取响应状态码
		int code = connection.getResponseCode();
		if(code == HttpURLConnection.HTTP_OK){
			
			BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("金智媛.jpg"));
			byte[] bs = new byte[1024];
			int len;
			while((len = bis.read(bs)) != -1){
				bos.write(bs, 0, len);
			}
			
			bis.close();
			bos.close();
			
		}else if(code == HttpURLConnection.HTTP_NOT_FOUND){
			System.out.println("页面未找到");
		}				
		
	}
}
举报

相关推荐

0 条评论