详解Java 网络编程


一、网络编程概述

1.1 网络编程的目的:直接或间接地通过网络协议与其他计算机实现数据交互,进行通讯

1.2 如何实现网络中的主机互相通信:

  • 通信双方的地址:IP、端口号
  • 一定的规则(即网络通信协议,有两套参考模型):①OSI参考模型:模型过于理想化,未能在英特网上进行广泛推广 ②TCP/IP协议:事实上的国际标准
    在这里插入图片描述

二、通信要素1:IP和端口号

2.1 IP

  1. IP:唯一的标识Internet上的计算机(通信实体)
  2. 在Java中使用InetAddress类代表IP
  3. IP分类:IPv4 和 IPv6 ;万维网 和 局域网
  4. 本地回路地址:127.0.0.1 对应着:localhost
  5. 如何实例化InetAddress:两个方法:getByName(String host)、getLocalHost
  6. 两个常用方法:getHostName()/getHostAddress()
import org.omg.CORBA.ARG_OUT;

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

public class InetAddressTest {
    public static void main(String[] args) {
        try {
            InetAddress inetAddress = InetAddress.getByName("192.168.1.104");
            System.out.println(inetAddress);

            InetAddress inetAddress2 = InetAddress.getByName("www.baidu.com");
            System.out.println(inetAddress2);

            InetAddress inetAddress3 = InetAddress.getByName("127.0.0.1");
            System.out.println(inetAddress3);

            //获取本地IP
            InetAddress inetAddress4 = InetAddress.getLocalHost();
            System.out.println(inetAddress4);
            //getHostName():获取域名
            System.out.println(inetAddress2.getHostName());
            //getHostAddress():获取主机的地址
            System.out.println(inetAddress2.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

输出结果:
在这里插入图片描述

2.2 端口号

在这里插入图片描述

三、通信要素2:网络通信协议

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
例子1:客户端发送信息给服务端,服务器将数据显示在控制台上

我们要先运行服务器server测试类,再运行客户client测试类

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class TCPTest {

    //充当客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream outputStream = null;
        try {
            //1.创建Socket对象,指明服务器端的ip和端口号
            //要的是对方服务器端的IP,但是服务器也在我们主机上,所以IP地址为127.0.0.1
            InetAddress inetAddress = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inetAddress,8899);
            //2.获取一个输出流,用于输出数据
            outputStream = socket.getOutputStream();
            //3.写出数据的操作
            outputStream.write("你好".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.资源的关闭
            if (outputStream!=null){
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //充当服务端
    @Test
    public void server(){
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            //1.创建服务器端的ServerSocket,指明自己的端口号
            serverSocket = new ServerSocket(8899);
            //2.调用accept()表示接收来自于客户端的socket
            socket = serverSocket.accept();
            //3.获取输入流
            inputStream = socket.getInputStream();
            //不建议这样写,可能会有乱码
//        byte[] buffer = new byte[200];
//        int len;
//        while ((len = inputStream.read(buffer))!= -1){
//            String str = new String(buffer,0,len);
//            System.out.print(str);
//        }
            //4.读取输入流中的数据
            byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[5];
            int len;
            while ((len = inputStream.read(buffer))!=-1){
                byteArrayOutputStream.write(buffer,0,len);
            }
            System.out.println(byteArrayOutputStream.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5.资源关闭
            if (byteArrayOutputStream!=null){
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

四、URL网络编程

  1. URL:统一资源定位符,对应着互联网的某一资源地址
  2. 格式:
    http://localhost:8080/examples/beauty.jpg?username=Tom
    协议-主机名----端口号-资源地址---------------参数列表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值