Java基础:网络编程

网络编程

  • 网络编程中有两个主要的问题:

    • 1.如何准确地定位网络上-台或多台主机;定位主机上的特定的应用
    • 2.找到主机后如何可靠高效地进行数据传输
  • 网络编程中的两个要素:

    • 1.对应问题一: IP和端口号
    • 2.对应问题二:提供网络通信协议: TCP/IP参考模型(应用层、传输层、网络层、物理+数据链路层)
  • 通信要素一: IP和端口号

    • IP:唯一的标识Internet. 上的计算机(通信实体)

    • 2.在Java中使用InetAddress类代表IP

    • 3.IP分类: IPv4和IPv6 ;万维网和局城网

    • 4.域名:www.baidu.com、www.jd.com

    • 5.本地回路地址:127.0.0.1 对应:localhost

  • 端口号:正在计算机上运行的进程。**
    - 要求:不同的进程有不同的端口号
    - 范围:被规定为一个16位的整数0-65535

端口号与IP地址的组合得出一个网络套接字:Socket

实例化InetAddress

  • 实例化InetAddress:两个方法:getByName(String host)getLocalHost()

  • 两个常用方法:getHostName()获取域名,getHostAddress()获取IP地址

public class IpTest {

    @Test
    public void test() throws Exception{

        System.out.println(InetAddress.getByName("38.143.11.116"));
        InetAddress inet1 = InetAddress.getByName("www.wanmeikk.me");
        System.out.println(inet1);
        //获取本地ip
        InetAddress inet2 = InetAddress.getLocalHost();
        System.out.println(inet2);

        //getHostName()获取域名
        String hostName = inet1.getHostName();
        System.out.println(hostName);
        //getHostAddress()获取IP地址
        String hostAddress = inet1.getHostAddress();
        System.out.println(hostAddress);
    }
}

TCP编程

消息发送

/**
 * Created by KingsLanding on 2022/7/4 20:38
 */
public class TCPTest {

    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        try {
            //1.创建Socket对象,指明服务器端的ip和端口号
            InetAddress inet = InetAddress.getByName("192.168.222.1");
            socket = new Socket(inet, 6699);

        /*
        返回此套接字的输出流。
        如果此套接字具有相关联的通道,则生成的输出流将其所有操作委派给通道。
        如果通道处于非阻塞模式,则输出流的write操作将抛出IllegalBlockingModeException 。
        */
            //2.获取一个输出流,用于输出数据
            os = socket.getOutputStream();
            //3.写出数据的操作
            os.write("你好,这是客户端".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭资源
            try {
                if(os!=null)
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(socket!=null)
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

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

文件发送

客户端向服务器发送文件,服务端保存文件到本地

/**
 * Created by KingsLanding on 2022/7/5 11:30
 *
 * 客户端向服务器发送文件,服务端保存文件到本地
 */
public class TCPTest2 {

    //客户端
    @Test
    public void client(){
        Socket socket = null;
        OutputStream os = null;
        FileInputStream fis = null;
        try {
            //1.IP地址
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            //2.Socket
            socket = new Socket(inet, 6699);
            //3.获取输出流
            os = socket.getOutputStream();
            //4.获取要发送的文件
            File file = new File("zmj.jpg");
            //5.文件输入流
            fis = new FileInputStream(file);

            //6.写入到socket输出流中
            byte[] bytes = new byte[1024];
            int len;
            while ((len=fis.read(bytes))!=-1){
                os.write(bytes,0,len);
            }
            System.out.println("发送成功");
            //7.关闭数据的输出
            socket.shutdownOutput();
            //8.接收来自服务器端的数据
            InputStream is = socket.getInputStream();
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] bytes1 = new byte[5];
            int len1;
            while ((len1=is.read(bytes1))!=-1){
                baos.write(bytes1,0,len1);
            }
            System.out.println(baos.toString());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //9.关闭资源
                if (fis!=null)
                fis.close();
                if (os!=null)
                os.close();
                if (socket!=null)
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //服务器端
    @Test
    public void server(){
        ServerSocket serverSocket = null;
        Socket socket = null;
        InputStream is = null;
        FileOutputStream fos = null;
        try {
            //1.定义服务器socket
            serverSocket = new ServerSocket(6699);
            //2.允许接收
            socket = serverSocket.accept();
            //3.获取输入流
            is = socket.getInputStream();
            //4.输出流(写出到--)
            fos = new FileOutputStream(new File("TCPTest.jpg"));
            //5.输出操作
            byte[] bytes = new byte[1024];
            int len;
            while ((len=is.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
            System.out.println("接收成功");

            //6.向客户端反馈
            OutputStream os = socket.getOutputStream();
            os.write("服务器已收到!".getBytes());

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //7.资源关闭
                if (fos!=null)
                fos.close();
                if (is!=null)
                is.close();
                if (socket!=null)
                socket.close();
                if (serverSocket!=null)
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

King'sLanding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值