黑马程序员 Java基础知识总结-网络编程

2、网络通讯要素

(1)IP地址(InetAddress已经封装成了对象)
1、网络中设备的标识
2、不易记忆,可用主机名
3、本地IP地址:127.0.0.1 主机名:Localhost。
(2)端口号(数字标识,没有必要封装成对象) 
1、用于标识进程的逻辑地址,不同进程的标识。
2、有效端口:0~65535,其中0~1024系统使用或保留端口。
(3)传输协议
1、通讯的规则。
2、常见协议:TCP,UDP。
3、网络通讯过程 
(1)找到对方 IP。 
(2)数据要发到对方指定的应用程序上,为了标识这些应用程序,所以给这些网络应用程序都用数字进行了标识。为了方便称呼这个数字,叫做端口。(逻辑端口)


了解:Ping 127.0.0.1(LocalHost)可以查看网卡是否异常。
(网络编程-IP地址) 
由于IP地址是一个复杂的事物,Java已经它封装成了对象,封装成了对象把复杂的事情简单化。 

下面是通过UDP协议实现的两台主机互相通信

客户端:UdpClient.java

import java.net.*;
public class UdpClient {
	private DatagramSocket ds;
	/*
	 *客户端负责发送数据 	
	 * */
	public static void main(String[] args) throws Exception {
		new UdpClient().send();
	}
	private void send() throws Exception {
		// 1、创建UDP socket服务
		ds = new DatagramSocket();
		// 2、定义数据包用于发送数据
		byte[] buf = new byte[1024];
		buf = "www.itheima.com".getBytes();
		InetAddress address = InetAddress.getByName("192.168.1.100");
		DatagramPacket dp = new DatagramPacket(buf, buf.length, address, 10000);
		// 3、通过DatagramSocket中的send方法发送通过DatagramPacket封装好的数据包
		ds.send(dp);
		// 4、关闭资源
		receive();
		ds.close();
	}
	private void receive() throws Exception {
		// 设置缓冲区
		byte[] buf = new byte[1024];
		DatagramPacket dp = new DatagramPacket(buf, buf.length);
		// 3、接收数据
		ds.receive(dp);
		String data = new String(dp.getData(), 0, dp.getLength()).trim();
		System.out.println(data);
	}
}
服务器端:UdpServer.java源代码
import java.net.*;
public class UdpServer {
	private String ip;
	private int port;
	private DatagramSocket ds;
	public static void main(String[] args) throws Exception {
		new UdpServer().receive();
	}
	public void receive() throws Exception {
		// 1、创建Socket服务
		ds = new DatagramSocket(10000);
		// 2、定义缓冲区用于存储数据
		byte[] buf = new byte[1024];
		DatagramPacket dp = new DatagramPacket(buf, buf.length);
		// 3、接收数据
		ds.receive(dp);
		buf = dp.getData();
		reverse(buf, dp.getLength());// 反转
		ip = dp.getAddress().getHostAddress(); // 获取ip地址
		port = dp.getPort();// 获取端口
		//System.out.println(data + "::" + ip + "::" + port);
		send(buf);
		ds.close();
	}
	// 发送数据到客户端
	public void send(byte[] buf) throws Exception {
		InetAddress address = InetAddress.getByName(ip);
		DatagramPacket dp = new DatagramPacket(buf, buf.length, address, port);
		// 3、通过DatagramSocket中的send方法发送通过DatagramPacket封装好的数据包
		ds.send(dp);
		// 4、关闭资源
	}
	public static byte[] reverse(byte[] buf, int length) {

		for (int i = 0; i < (length) / 2; i++) {
			byte temp = buf[i];
			buf[i] = buf[length - i - 1];
			buf[length - i - 1] = temp;
		}
		return buf;
	}
}
下面是通过TCP/IP协议实现媒体文件的上传:

import java.io.*;
import java.net.*;
public class UpLoadMediaClient {
	public static void main(String[] args) throws Exception {
		Socket s = new Socket("192.168.1.100",10005);
		//读媒体文件对象 
		BufferedInputStream buffr = 
				new BufferedInputStream(new FileInputStream("IO.jpg"));
		//向服务端发送数据
		PrintStream out = 
				new PrintStream(s.getOutputStream(),true);
		//接收服务器返回信息
		BufferedReader buffIn = new BufferedReader(new InputStreamReader(s.getInputStream())) ;
		//设置缓冲区
		byte[] buf = new byte[1024];
		int len = -1;
		while((len = buffr.read(buf, 0, buf.length))!=-1){
			out.write(buf, 0, len);
			out.flush();
		}
		s.shutdownOutput();
		String result = buffIn.readLine();
		System.out.println(result);
		buffr.close();
		s.close();		
	}
}
服务器端:

import java.io.*;
import java.net.*;
public class UpLoadMediaServer {
	public static void main(String[] args) throws Exception {
		ServerSocket ss = new ServerSocket(10005);
		Socket s = ss.accept() ;
		//创建接受客户端数据对象
		BufferedInputStream buffIn = 
				new BufferedInputStream(s.getInputStream());
		//写文件对象
		BufferedOutputStream out = 
				new BufferedOutputStream(new FileOutputStream("server.jpg",true));
		//返回信息到客户端对象
		PrintWriter pw = new PrintWriter(s.getOutputStream());
		//设置缓冲区
		byte[] buf = new byte[1024];
		int len = 0 ;
		while((len = buffIn.read(buf, 0, buf.length))!=-1){
			out.write(buf, 0, len);
			out.flush();
		}		
		pw.write("上传成功");
		pw.flush();
		out.close();
		s.close();
		ss.close();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值