十八章:网络编程"/>
第十八章:网络编程
一、InetAddress类
(1)方法:
- 获取本机地址InetAddress对象 getLocalHost
- 根据指定主机名/域名获取ip地址对象 getByName
- 获取InetAddress对象的主机名 getHostName
- 获取InetAddress对象的地址 getHostAddress
public static void main(String[] args) throws UnknownHostException {//获取本机的InetAddress对象InetAddress localHost = InetAddress.getLocalHost();System.out.println(localHost); //LAPTOP-N88N9SVM/192.168.137.1//根据指定的主机名 获取 InetAddress对象InetAddress host1 = InetAddress.getByName("LAPTOP-N88N9SVM");System.out.println(host1);//LAPTOP-N88N9SVM/192.168.137.1//根据域名返回 InetAddress对象,比如www.baidu 对应InetAddress host2 = InetAddress.getByName("www.baidu");System.out.println(host2); //www.baidu/183.232.231.174//通过 InetAddress 对象 获取对应的地址String hostAddress = host2.getHostAddress(); //IPSystem.out.println(hostAddress);//183.232.231.174//通过 InetAddress 对象 获取对应的域名String hostName = host2.getHostName();System.out.println(hostName); //www.baidu}
二、TCP网络通信编程
(1)客户端
public class Client {public static void main(String[] args) throws IOException {//1.连接服务器(ip,端口)//连接本机的9999,如果连接成功返回Socket对象Socket socket = new Socket(InetAddress.getLocalHost(), 9999);System.out.println("客户端: "+socket.getClass());//2.连接成功后,生成Socket对象通过socket.getOutputStreamOutputStream outputStream = socket.getOutputStream();//3.通过写入数据到数据通道outputStream.write("hello,serve".getBytes());//4.关闭流对象与socket,必须关闭outputStream.close();socket.close();System.out.println("客户端退出。。。。");}
}
(2)服务端:
public class Serve {public static void main(String[] args) throws IOException {//1.在本机的 9999端口监听,等待连接ServerSocket serverSocket = new ServerSocket(9999);System.out.println("服务端,在9999端口监听,等待连接。。");//2.当没有客户端连接9999端口时,程序会阻塞等待连接//如果有客户端连接,则会返回Socket对象Socket socket = serverSocket.accept();System.out.println("服务端:" + socket.getClass());//3.通过socket.getInputStream() 读取客户端写入数据通道的数据,InputStream inputStream = socket.getInputStream();//4.IO读取byte[] buf = new byte[8];int readLen = 0;while ((readLen = inputStream.read(buf)) != -1) {System.out.print(new String(buf, 0, readLen));}//5.关闭serverSocket.close();inputStream.close();socket.close();}
双向数据通知需要使用关闭结束标识 shoutdowmOutput
(1)客户端:
public class Client {public static void main(String[] args) throws IOException {//1.连接服务器(ip,端口)//连接本机的9999,如果连接成功返回Socket对象Socket socket = new Socket(InetAddress.getLocalHost(), 9999);System.out.println("客户端: "+socket.getClass());//2.连接成功后,生成Socket对象通过socket.getOutputStreamOutputStream outputStream = socket.getOutputStream();//3.通过写入数据到数据通道outputStream.write("hello,serve".getBytes());//设置结束标识socket.shutdownOutput();//4.获取和Socket相关联的输入流,读取数据(字节),并显示InputStream inputStream = socket.getInputStream();int readLen = 0;byte[] buf = new byte[1024];while ((readLen = inputStream.read(buf)) != -1){System.out.println(new String(buf,0,readLen));}//5.关闭流对象与socket,必须关闭inputStream.close();outputStream.close();socket.close();System.out.println("客户端退出。。。。");}
}
(2)服务端
public class Serve {public static void main(String[] args) throws IOException {//1.在本机的 9999端口监听,等待连接ServerSocket serverSocket = new ServerSocket(9999);System.out.println("服务端,在9999端口监听,等待连接。。");//2.当没有客户端连接9999端口时,程序会阻塞等待连接//如果有客户端连接,则会返回Socket对象Socket socket = serverSocket.accept();System.out.println("服务端:" + socket.getClass());//3.通过socket.getInputStream() 读取客户端写入数据通道的数据,InputStream inputStream = socket.getInputStream();//4.IO读取byte[] buf = new byte[8];int readLen = 0;while ((readLen = inputStream.read(buf)) != -1) {System.out.print(new String(buf, 0, readLen));}//5.获取socket相关联的输出流OutputStream outputStream = socket.getOutputStream();outputStream.write("hello,Client".getBytes());//设置结束标识socket.shutdownOutput();//6.关闭serverSocket.close();inputStream.close();outputStream.close();socket.close();}
}
字符流写入读取一体
(1)客户端
public class Client {public static void main(String[] args) throws IOException {//1.连接服务器(ip,端口)//连接本机的9999,如果连接成功返回Socket对象Socket socket = new Socket(InetAddress.getLocalHost(), 9999);System.out.println("客户端: "+socket.getClass());//2.连接成功后,生成Socket对象通过socket.getOutputStreamOutputStream outputStream = socket.getOutputStream();//3.通过写入数据到数据通道BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));bufferedWriter.write("hello,serve 字符流");bufferedWriter.newLine();//插入一个换行符,表示写入的内容结束bufferedWriter.flush();//如果使用字符流,需要手动刷新,否则数据不会写入通道//设置结束标识socket.shutdownOutput();//4.获取和Socket相关联的输入流,读取数据(字节),并显示InputStream inputStream = socket.getInputStream();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));String s = bufferedReader.readLine();System.out.println(s);//5.关闭流对象与socket,必须关闭bufferedReader.close();bufferedWriter.close();socket.close();System.out.println("客户端退出。。。。");}
}
(2)服务端
public class Hello {public static void main(String[] args) throws IOException {//1.在本机的 9999端口监听,等待连接ServerSocket serverSocket = new ServerSocket(9999);System.out.println("服务端,在9999端口监听,等待连接。。");//2.当没有客户端连接9999端口时,程序会阻塞等待连接//如果有客户端连接,则会返回Socket对象Socket socket = serverSocket.accept();System.out.println("服务端:" + socket.getClass());//3.通过socket.getInputStream() 读取客户端写入数据通道的数据,InputStream inputStream = socket.getInputStream();//4.IO读取BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));String s = bufferedReader.readLine();System.out.println(s);//5.获取socket相关联的输出流OutputStream outputStream = socket.getOutputStream();BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));bufferedWriter.write("helloClient");bufferedWriter.newLine();//表示写的内容已经结束bufferedWriter.flush(); //需要手动刷新//设置结束标识socket.shutdownOutput();//6.关闭serverSocket.close();bufferedReader.close();bufferedWriter.close();socket.close();}
}
(案例)
(1)服务端
public class Hello {public static void main(String[] args) throws IOException {//1.在本机的 9999端口监听,等待连接ServerSocket serverSocket = new ServerSocket(9999);System.out.println("服务端,在9999端口监听,等待连接。。");//2.当没有客户端连接9999端口时,程序会阻塞等待连接//如果有客户端连接,则会返回Socket对象Socket socket = serverSocket.accept();InputStream inputStream = socket.getInputStream();byte[] buf = new byte[1024];int readLen = 0;String filePath = "src/qie.png";FileOutputStream fileOutputStream = new FileOutputStream(filePath);while ((readLen = inputStream.read(buf)) != -1){fileOutputStream.write(buf,0,readLen);}OutputStream outputStream = socket.getOutputStream();outputStream.write("收到图片".getBytes());//6.关闭serverSocket.close();outputStream.close();inputStream.close();socket.close();}
}
(2)客户端
public class Client {public static void main(String[] args) throws IOException {//1.连接服务器(ip,端口)//连接本机的9999,如果连接成功返回Socket对象Socket socket = new Socket(InetAddress.getLocalHost(), 9999);//2.连接成功后,生成Socket对象通过socket.getOutputStreamOutputStream outputStream = socket.getOutputStream();//3.通过写入数据到数据通道String filePath = "d:\\qie.png";byte[] buf = new byte[1024];FileInputStream fileInputStream = new FileInputStream(filePath);while (fileInputStream.read(buf) != -1){outputStream.write(buf);}//设置结束标识socket.shutdownOutput();InputStream inputStream = socket.getInputStream();byte[] buf1 =new byte[1024];inputStream.read(buf1);System.out.println(new String(buf1,0, buf1.length));socket.close();inputStream.close();outputStream.close();}
}
三、netstat指令
netstat查看当前主机网络情况,包括端口监听和网络连接情况
netstat -an查看所有 netstat -an | more 分页显示
Listening表示在监听
四、UDP网络编程
(1)接收端:
public class Client {public static void main(String[] args) throws IOException {//1.创建一个 DatagramSocket 对象,准备在9999接收数据DatagramSocket socket = new DatagramSocket(9999);//2.构建一个 DatagramPacket 对象准备接收数据//在前面讲过UDP 协议时说过一个数据包最大 64kbyte[] bytes = new byte[1024];DatagramPacket packet = new DatagramPacket(bytes, bytes.length);//3.调用接收方法socket.receive(packet); //没有数据的话就会阻塞//4.可以把packet进行拆包取出数据,并显示int length = packet.getLength();//实际获取到的数据字节长度byte[] data = packet.getData();//接收到的数据String s = new String(data, 0, length);System.out.println(s);//5.关闭资源socket.close();}
}
(2)发送端
public class Hello {public static void main(String[] args) throws IOException {//1.创建DatagramSocket对象,准备在9998发送和接收数据 发送端也可以接受数据DatagramSocket socket = new DatagramSocket(9998);//2.将需要发送的数据,封装到byte[] data = "hello 明天去吃火锅".getBytes();//说明:封装的 DatagramPacket对象 data 内容字节数组 ,data.length ,主机ip,端口DatagramPacket packet = new DatagramPacket(data,data.length, InetAddress.getByName("127.0.0.1"),9999);socket.send(packet);//关闭资源socket.close();}
}
五、网络编程课后作业
(1)要求TCP协议
(2)要求使用UDP协议
(3)要求TCP协议
更多推荐
第十八章:网络编程
发布评论