韩顺平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.3System.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端口时,程序会
// 11Socket 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端口时,程序会
// 11Socket 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("客户端退出");}
}
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
