Java学习笔记之网络编程
1、要想实现网络传输,需要考虑哪些问题?
1.1 如何才能准确的定位网络上的一台主机?
1.2 如何才能进行可靠的、高效的数据传输?
2、Java如何实现的网络通信
2.1 使用IP地址定位一台主机 使用端口号定位一个应用-------->IPAddress类。
>如何创建一个InetAddress的对象?getByName("");比如InetAddress inet = InetAddress.getByName("127.0.0.1");
>如何获取本机的一个InetAddress的对象?getLocalHost()
>域名:getHostName(); IP:getHostAddress();
2.2 对应有协议
对于传输层而言,分为TCP、UDP
TCP编程例子: Socket、ServerSocket
- /**
- * TCP编程例一:
- * 客户端给服务端发送信息,服务端输出此信息到控制台上
- * 网络编程实际上就是Socket编程
- */
- public class TestTCP1 {
- @Test
- 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) {
- e.printStackTrace();
- } finally {
- // 4.关闭相应的流和Socket对象。
- if (os != null) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (socket != null) {
- try {
- socket.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- @Test
- public void server() {
- ServerSocket ss = null;
- Socket s = null;
- InputStream is = null;
- try {
- // 1.创建ServerSocket对象,通过构造器指明自身的端口号。
- ss = new ServerSocket(9090);
- // 2.调用其accept()方法,返回一个socket的对象
- s = ss.accept();
- // 3. 调用Socket对象的getInputStream()获取一个从客户端发送来的输入流。
- is = s.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) {
- e.printStackTrace();
- } finally {
- // 5.关闭相应的流以及Socket、ServerSocket对象。
- if (is != null) {
- try {
- is.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (s != null) {
- try {
- s.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (ss != null) {
- try {
- ss.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
UDP编程例子:DatagramSocket、DatagramPacket
URL编程:统一资源定位符 Uniform Resource Locator
- public class TestUDP1 {
- /**
- * 发送端
- */
- @Test
- public void send() {
- DatagramSocket ds = null;
- try {
- ds = new DatagramSocket();
- byte[] b = "你好,我是要发送的数据!".getBytes();
- // 创建一个数据报:每一个数据报不能大于64K,都记录着数据信息以及发送端的IP、端口号以及要发送到的
- // 接收端的IP、端口号。
- DatagramPacket packet = new DatagramPacket(b, 0, b.length, InetAddress.getByName("127.0.0.1"), 9090);
- ds.send(packet);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (ds != null) {
- ds.close();
- }
- }
- }
- /**
- * 接收端
- */
- @Test
- public void receive() {
- DatagramSocket ds = null;
- try {
- ds = new DatagramSocket(9090);
- byte[] b = new byte[1024];
- DatagramPacket packet = new DatagramPacket(b, 0, b.length);
- ds.receive(packet);
- String str = new String(packet.getData(), 0, packet.getLength());
- System.out.print(str);
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (ds != null) {
- ds.close();
- }
- }
- }
- }
例子:
- /**
- * URL:统一资源定位符,一个URL对象对应着互联网上一个资源。
- * 我们可以通过URL的对象调用其相应的方法,将此资源读取(“下载”)
- */
- public class TestURL {
- public static void main(String[] args) throws Exception {
- // 1.创建一个URL对象
- URL url = new URL("http://127.0.0.1:8080/examples/helloworld.txt");
- // 2.常用的方法:
- // System.out.println(url.getProtocol());
- // System.out.println(url.getHost());
- // System.out.println(url.getPort());
- // System.out.println(url.getFile());
- // System.out.println(url.getRef());
- // System.out.println(url.getQuery());
- // 如何将服务端的资源读取进来?
- InputStream is = url.openStream();
- byte[] b = new byte[20];
- int len;
- while ((len = is.read(b)) != -1) {
- String str = new String(b, 0, len);
- System.out.println(str);
- }
- //如果既有数据的输入,也有输出,则须使用URLConnection
- URLConnection urlConn = url.openConnection();
- InputStream is1 = urlConn.getInputStream();
- FileOutputStream fos = new FileOutputStream(new File("abc.txt"));
- byte[] b1 = new byte[20];
- int len1;
- while ((len1 = is1.read(b1)) != -1) {
- fos.write(b1, 0, len1);
- }
- is.close();
- is1.close();
- fos.close();
- }
- }