韩顺平Java学习(656-659)

 

 韩顺平老师说的计算机网络三大神书是哪三本呀

据韩顺平老师所说,计算机网络三大神书是:

1.《计算机网络:自顶向下方法》(Computer Networking: A Top-Down Approach):由James F. Kurose和Keith W. Ross所著,是一本广受欢迎的计算机网络教材,使用自顶向下的方法介绍了计算机网络的各个层次。

2.《TCP/IP详解 卷一:协议》(TCP/IP Illustrated, Volume 1: The Protocols):由W. Richard Stevens所著,介绍了TCP/IP协议族的详细内容,包括数据链路层、网络层、传输层和应用层等各个方面。

3.《UNIX网络编程 卷一:套接字联网API》(Unix Network Programming, Volume 1: The Sockets Networking API):由W. Richard Stevens所著,介绍了Unix系统下套接字编程的内容,包括套接字的创建、绑定、监听、连接、发送和接收等各个方面。

socket (电源插座)

 

 

 

package com.hspedu.api;


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

/**
 * @author abner
 * @version 1.0
 */
public class API_ {
    public static void main(String[] args) throws UnknownHostException {
        1.获取本机的InetAddress对象
        InetAddress localHost = InetAddress.getLocalHost();
        System.out.println(localHost);//LAPTOP-VSLBPDPF/192.168.130.84
//2。根据指定主机名获取InetAddress对象
        InetAddress host1 = InetAddress.getByName("LAPTOP-VSLBPDPF");
        System.out.println("host1=" +host1);
//3。根据域名返回InetAddress对象,比如www.baidu.com对应
        InetAddress host2 = InetAddress.getByName("www.baidu.com");
        System.out.println("host2= "+ host2);
        //服务器后台设置 服务区集群
        //百度后台
//4。通过InetAddress对象,获取对应的地址
        String hostAddress = host2.getHostAddress();//110.242.68.3
        System.out.println("host2 对应的ip = "+hostAddress);


        String hostName = host2.getHostName();
        System.out.println("host2对应的主机名/域名="+hostName);


    }
}

案例1:

 

 

 服务端

package com.hspedu.socket;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author abner
 * @version 1.0
 */
public class SocketTCP01Server {
    public static void main(String[] args) throws IOException {
//思路
//1。在本机的9999端口监听,等待连接
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务器等待中、、、、");
//2。当没有客户端连接9999端口时,程序会
//        11
        Socket socket = serverSocket.accept();
        System.out.println("服务端 socket = "+socket.getClass());

//        阻塞,等待连接
3.通过socket.getInputstream()读取
        InputStream inputStream = socket.getInputStream();
        
        
//        
//        客户端写入到数据通道的数据,
        byte[] buf = new byte[1024];
        int readLen = 0;
        while ((readLen = inputStream.read(buf)) != -1){
            System.out.println(new String(buf,0,readLen));
        }

        inputStream.close();
        socket.close();
        serverSocket.close();
//        显东
        
    }
}

客户端

package com.hspedu.socket;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

/**
 * @author abner
 * @version 1.0
 */
public class SocketTCP01Client {
    public static void main(String[] args) throws IOException {
//思路
//1。连接服第端(ip,端口)
        //解读:连接本机的9999端口,如果连接成功,返回Socket对象
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("客户端 socket返回=" +socket.getClass());

//2。连接上后,生成Socket,通过
//socket.getOutputstream()
      //  得到和socket对象关联的输出流对象
        OutputStream outputStream = socket.getOutputStream();
//3。通过输出流,写入数据到数据通道
        outputStream.write("hello ,server".getBytes(StandardCharsets.UTF_8));

        outputStream.close();
        socket.close();
        System.out.println("客户端退出");
    }
}

案例2:

服务端:

package com.hspedu.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @author abner
 * @version 1.0
 */
public class SocketTCP02Server {
    public static void main(String[] args) throws IOException {
//思路
//1。在本机的9999端口监听,等待连接
        ServerSocket serverSocket = new ServerSocket(9999);
        System.out.println("服务器等待中、、、、");
//2。当没有客户端连接9999端口时,程序会
//        11
        Socket socket = serverSocket.accept();
        System.out.println("服务端 socket = "+socket.getClass());

//        阻塞,等待连接
3.通过socket.getInputstream()读取
        InputStream inputStream = socket.getInputStream();


//
//        客户端写入到数据通道的数据,
        byte[] buf = new byte[1024];
        int readLen = 0;
        while ((readLen = inputStream.read(buf)) != -1){
            System.out.println(new String(buf,0,readLen));
        }

        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello, client".getBytes(StandardCharsets.UTF_8));
        socket.shutdownOutput();

        outputStream.close();
        inputStream.close();
        socket.close();
        serverSocket.close();
//        显东

    }
}

客户端:

package com.hspedu.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.charset.StandardCharsets;

/**
 * @author abner
 * @version 1.0
 */
public class SocketTCP02Client {
    public static void main(String[] args) throws IOException {
//思路
//1。连接服第端(ip,端口)
        //解读:连接本机的9999端口,如果连接成功,返回Socket对象
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("客户端 socket返回=" +socket.getClass());

//2。连接上后,生成Socket,通过
//socket.getOutputstream()
        //  得到和socket对象关联的输出流对象
        OutputStream outputStream = socket.getOutputStream();
//3。通过输出流,写入数据到数据通道
        outputStream.write("hello ,server".getBytes(StandardCharsets.UTF_8));
        socket.shutdownOutput();
        //4。获取和socket关联的输入流。读取数据(字节),并显示
        InputStream inputStream = socket.getInputStream();
        byte[] buf = new byte[1024];
        int readLen = 0;
        while ((readLen = inputStream.read(buf)) != -1){
            System.out.println(new String(buf,0,readLen));
        }

        inputStream.close();
        outputStream.close();
        socket.close();
        System.out.println("客户端退出");
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值