day25----及其简单的网络编程基础

网络编程

概述

网络协议

需要共同遵守的游戏规则

TCP

保证数据的可达,数据可靠的网络协议。实时性不强

UDP

无法保证数据的可达,数据不可靠。但是实时性强

IP

能够在网络中找到一台电脑

端口号

能够找到这台电脑的某个软件

8.140.130.145

InetAddress

能够表示网络中的一个地址

public class InetAddressPractice {
    public static void main(String[] args) throws UnknownHostException {
        InetAddress localhost = InetAddress.getLocalHost();
        System.out.println(localhost.getHostAddress());
        System.out.println(localhost.getHostName());
    }
}

ServerSocket

能够依赖此对象,创建出一台服务器

public class SimulateServer {
    public static void main(String[] args) throws IOException {
        // 创建套接字对象
        ServerSocket socket = new ServerSocket(8088);

        while(true) {
            // 只要没有节点连接此服务器,那么accept就会一直等
            // 在等待的过程中,accept下面的代码,不能被执行
            // 所以此行代表,会造成当前线程的阻塞
            // 当等到了一连接,得到的连接对象就是accept
            Socket accept = socket.accept();

            // accept的输入流,代表的是节点给服务器发送的是什么
            // accept的输出流,代表的是此服务器往回返回的是什么
            OutputStream os = accept.getOutputStream();

            os.write(("HTTP/1.1 200 OK\r\n" +
                    "Content-Type: text/html; charset=utf-8;\r\n" +
                    "\r\n" +
                    "hello!").getBytes());
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值