21天精通java基础之Day20网络编程(一)

本文介绍Java网络编程的基础概念,包括IP地址的使用、网络通信协议的选择,并通过具体示例讲解了TCP协议下客户端和服务端的交互流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Day20:网络编程(

  网络通信的要素一:IP地址,通过IP地址,唯一的定位互联网上一台主机。

     InetAddress:位于java.net包下

   1.InetAddress用于代表IP地址。一个InetAddress的对象就代表一个IP地址。

   2.getByName(String host):创建InetAddress的对象。

   3.getHostName():获取IP地址对应的域名,getHostAddress():获取IP地址。

  网络通信的要素二:网络通信协议

   计算机网络中实现通信必须有一些约定,即通信协议,对速率,传输代码、代码结构、传输控制步骤、出错控制等制定标准。

  传输层协议中有两个非常重要的协议:

   1.传输控制协议TCP

           ①使用TCP协议前,须建立TCP连接,形成传输通道。

           ②传输前,采用三次握手方式,是可靠的。

           ③TCP协议进行通信的两个应用进程:客户端、服务端。

           ④可进行大数据量的传输。

           ⑤传输完毕,须释放已建立的连接,效率低。

   2.用户数据报协议UDP

           ①将数据、源、目的封装成数据包里,不需要建立连接。

           ②每个数据报的大小限制在64k内。

           ③因无需连接,故是不可靠的。

           ④发送数据结束时无需释放资源,速度快。

  Socket:

     ①利用套接字开发网络应用程序早已被广泛的采用,以至于成为事实上的标准。

     ②通信的两端都要有Socket,是两台机器间通信的端点。

     ③网络通信其实就是Socket间的通信。

     ④Socket允许程序把网络连接当成一个流,数据在两个Socket间通过IO传输。

     ⑤一般主动发起通信的应用程序属于客户端,在等待通信请求的为服务端。

TCP编程例一:

   客户端给服务端发送消息,服务器输出此信息到控制台上。

客户端:

	public void client(){
		Socket socket = null;
		OutputStream os = null;
		try {
			//1.创建一个Socket的对象,通过构造器指明服务的IP地址,以及其接收程序的端口号
			socket = new Socket(InetAddress.getByName("127.0.0.1"), 9090);
			//2.getOutputStream(),发送数据,方法返回OutputStream的对象。
			os = socket.getOutputStream();
			//3.具体的输出过程
			os.write("我是客户端".getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//4.关闭相应的流和Socket对象
			if(os != null){
				try {
					os.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();
				}				
			}
		}		
	}
服务器端:

	public void server(){
		ServerSocket ss = null;
		Socket socket = null;
		InputStream is = null;
		try {
			//1.创建一个ServerSocket的对象,通过构造器指明自身的端口号
			ss = new ServerSocket(9090);
			//2.调用其accept()方法,返回一个Socket的对象
			socket = ss.accept();
			//3.调用Socket对象的getInputStream()获取从客户端发送过来的输入流
			is = socket.getInputStream();
			//4.对获取的输入流进行的操作
			byte[] b = new byte[20];			
			int len;
			while((len = is.read(b)) != -1){
				String str = new String(b, 0, len);
				System.out.println(str);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//5.关闭相应的流以及Socket、ServerSocket对象
			if(is != null){
				
				try {
					is.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(ss != null){
				try {
					ss.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}		
	}
TCP编程例二:

   客户端给服务端发送信息,服务端将信息打印到控制台上,同时发送已收到信息。

客户端:

public void client(){
		Socket socket = null;
		OutputStream os = null;
		InputStream is = null;
		try {
			socket = new Socket(InetAddress.getByName("127.0.0.1"), 8989);
			os = socket.getOutputStream();
			os.write("I am Client".getBytes());
			//关闭输出流,显式的告诉服务端我已发送完毕
			socket.shutdownOutput();
			
			is = socket.getInputStream();
			byte b[] = new byte[20];
			int len ;
			while((len = is.read(b)) != -1){
				String str = new String(b, 0, len);
				System.out.print(str);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(is != null){			
				try {
					is.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(os !=null){
				try {
					os.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();
				}				
			}
		}
		
	}

服务器端:

	public void Server(){
		ServerSocket ss = null;
		Socket socket = null;
		InputStream is = null;
		OutputStream os = null;
		try {
			ss = new ServerSocket(8989);
			socket = ss.accept();
			is = socket.getInputStream();
			
			byte b[] = new byte[20];
			int len;
			while((len = is.read(b)) != -1){
				String str = new String(b, 0, len);
				System.out.println(str);
			}
			//socket.shutdownInput();
			os = socket.getOutputStream();
			os.write("I am Server,I already get message!".getBytes());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(os != null){
				try {
					os.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(socket != null){
			try {
					socket.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if(ss != null){
				try {
					ss.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}				
			}
		}	
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值