java复习笔记day16

本文深入讲解Java网络编程的基础知识,包括IP地址与InetAddress类的使用、端口号的概念、URL编程、TCP编程的具体实现,以及如何通过Java进行网络通信。

网络编程:
1.要想实现网络通信,需要解决的问题:
如何准确定位互联网上的一台或多台主机。
如何实现可靠而高效的数据通信。

2.网络通信的两个要素:
使用IP地址。
使用通信协议:TCP/IP 网络通信模型:应用层,传输层,网络层,物理+数据链路层。

3.java.net.InetAddress 类的使用
InetAdress类的一个对象,就代表一个IP地址。
一个IP地址,唯一地标识Internet上的计算机。
本地回路地址:127.0.0.1 域名:localhost

package inetAddressTest;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class InetAddressTest {
	public static void main(String[] args) {
		//实例化
		try {
//			1.实例化:getByName(String host)
			InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
			System.out.println(inetAddress);
			
			System.out.println(inetAddress.getHostAddress());
//			2.获取本地的ip
			InetAddress inetAddress2 = InetAddress.getLocalHost();
			System.out.println(inetAddress2);
			
			System.out.println(inetAddress2.getHostName());
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

4.端口号:
端口号标识正在计算机上运行的进程(程序)
5.URL编程
Uniform Resource Locator :统一资源定位符,它表示Internet上某一资源的地址。
协议名://主机名:端口号/文件名
public String getProtocol() 获取该URL的协议名
public String getHost() 获取该URL的主机名
public String getPort() 获取该URL的端口号
public String getPath() 获取该URL的路径
public String getFile() 获取该URL的文件名
public String getQuery() 获取该URL的查询名

6.TCP 编程

package inetAddressTest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import org.junit.Test;

public class URLTest {
	@Test
	public void test(){
		//实例化
		try {
			URL url = new URL("");
			System.out.println(url.getHost());
			//可以使用url的一系列方法
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//下载指定位置的资源
	@Test
	public void test1(){
		HttpURLConnection connection = null;
		InputStream is = null;
 		FileOutputStream fos = null;
		try {
			URL url = new URL("http://img.zcool.cn/community/0117e2571b8b246ac72538120dd8a4.jpg@1280w_1l_2o_100sh.jpg");
			connection = (HttpURLConnection) url.openConnection();
			is = connection.getInputStream();
			fos = new FileOutputStream("test.jpg");
			
			byte []  buffer = new byte[1024];
			int len ;
			while((len = is.read(buffer))!=1)
				fos.write(buffer,0,len); // 这一行会报数组越界错误,但不影响。
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			
			if(fos != null)
				try {
					fos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(is != null)
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(connection != null)
				connection.disconnect();
		}
		
		
	}
}

package inetAddressTest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

/**
 * TCP 的 网络通信举例1
 * 客户端发送数据给服务器端,服务器端获取数据,将数据显示在控制台
 * 
 */
/*****先运行服务端,再运行客户端***************/
public class TCPTest {
	
	//服务端
	@Test
	public void server(){
//		1.创建ServerSocket
		ServerSocket serverSocket = null ;
		Socket socket = null;
		ByteArrayOutputStream baos = null;
		try {
			serverSocket = new ServerSocket(8989);
			//2.得到Socket
			socket = serverSocket.accept();
			//3.获取输入流
			InputStream is = socket.getInputStream();
			//有乱码风险
//			byte[] buffer = new byte[10];
//			int len;
//			while((len = is.read(buffer))!=-1){
//				String s = new String(buffer,0,len);
//				System.out.println(s);
//			}
			baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[10];
			int len;
			while((len = is.read(buffer))!=-1){
				baos.write(buffer,0,len);
			}
			System.out.println(baos.toString());
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(baos != null)
				try {
					baos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(socket != null)
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(serverSocket != null)
				try {
					serverSocket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
	//客户端
		@Test
		public void client(){
			
			OutputStream os = null;
			Socket socket = null;
			try {
				//1.创建Socket,指明对方的IP地址和对方的端口号
				socket = new Socket(InetAddress.getByName("127.0.0.1"),8989);
				//2.获取一个输出流
				os = socket.getOutputStream();
				//3.输出数据
				os.write("你好,服务端,我是客户端".getBytes());
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				//关闭资源
				if (os != null){
					try {
						os.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if(socket != null){
					try {
						socket.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
		}
}

package inetAddressTest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.junit.Test;

/**
 * TCP 的 网络通信举例2
 * 客户端发送数据给服务器端,服务器端获取数据。并告诉客户端收到了,同时将客户端发来的数据显示在控制台。客户端将服务端发来
 * 的数据也打印出来。
 * 
 */
/*****先运行服务端,再运行客户端***************/
public class TCPTest2 {
	
	//服务端
	@Test
	public void server(){
//		1.创建ServerSocket
		ServerSocket serverSocket = null ;
		Socket socket = null;
		ByteArrayOutputStream baos = null;
		OutputStream os = null;
		try {
			serverSocket = new ServerSocket(8989);
			//2.得到Socket
			socket = serverSocket.accept();
			//3.获取输入流
			InputStream is = socket.getInputStream();
			
			baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[10];
			int len;
			while((len = is.read(buffer))!=-1){
				baos.write(buffer,0,len);
			}
			System.out.println(baos.toString());
			System.out.println("数据来自于:"+socket.getInetAddress().getHostAddress());
			socket.shutdownInput();
			//4.给客户端发送反馈信息。
			os = socket.getOutputStream();
			os.write("已收到".getBytes());
			//关闭输出过程
			socket.shutdownOutput(); 
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(baos != null)
				try {
					baos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(socket != null)
				try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(serverSocket != null)
				try {
					serverSocket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if (os != null){
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	//客户端
		@Test
		public void client(){
			
			OutputStream os = null;
			Socket socket = null;
			InputStream is = null;
			ByteArrayOutputStream baos = null;
			try {
				//1.创建Socket,指明对方的IP地址和对方的端口号
				socket = new Socket(InetAddress.getByName("127.0.0.1"),8989);
				//2.获取一个输出流
				os = socket.getOutputStream();
				//3.输出数据
				os.write("你好,服务端,我是客户端".getBytes());
				//关闭输出过程
				socket.shutdownOutput();
				//4.接收服务器端发送来的数据
				is = socket.getInputStream();
				baos = new ByteArrayOutputStream();
				byte[] buffer = new byte[10];
				int len;
				while((len = is.read(buffer))!=-1){
					baos.write(buffer,0,len);
				}
				System.out.println(baos.toString());
				socket.shutdownInput();
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				//关闭资源
				if(baos != null)
					try {
						baos.close();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				if (os != null){
					try {
						os.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) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
				
			}
		}
}

内容概要:本文系统介绍了算术优化算法(AOA)的基本原理、核心思想及Python实现方法,并通过图像分割的实际案例展示了其应用价值。AOA是一种基于种群的元启发式算法,其核心思想来源于四则运算,利用乘除运算进行全局勘探,加减运算进行局部开发,通过数学优化器加速函数(MOA)和数学优化概率(MOP)动态控制搜索过程,在全局探索与局部开发之间实现平衡。文章详细解析了算法的初始化、勘探与开发阶段的更新策略,并提供了完整的Python代码实现,结合Rastrigin函数进行测试验证。进一步地,以Flask框架搭建前后端分离系统,将AOA应用于图像分割任务,展示了其在实际工程中的可行性与高效性。最后,通过收敛速度、寻优精度等指标评估算法性能,并提出自适应参数调整、模型优化和并行计算等改进策略。; 适合人群:具备一定Python编程基础和优化算法基础知识的高校学生、科研人员及工程技术人员,尤其适合从事人工智能、图像处理、智能优化等领域的从业者;; 使用场景及目标:①理解元启发式算法的设计思想与实现机制;②掌握AOA在函数优化、图像分割等实际问题中的建模与求解方法;③学习如何将优化算法集成到Web系统中实现工程化应用;④为算法性能评估与改进提供实践参考; 阅读建议:建议读者结合代码逐行调试,深入理解算法流程中MOA与MOP的作用机制,尝试在不同测试函数上运行算法以观察性能差异,并可进一步扩展图像分割模块,引入更复杂的预处理或后处理技术以提升分割效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值