本文共 15070 字,大约阅读时间需要 50 分钟。
计算机网络是指将位置不同的具有独立功能的多台及其外部设备,通过通信线路连接起来,在,及的管理和协调下,实现和信息传递的计算机系统。
传播交流信息,数据交换,通信。
package com.qing.demo01;import java.net.InetAddress;import java.net.UnknownHostException;/** * 测试IP */public class TestInetAddress { public static void main(String[] args) { try { InetAddress address1 = InetAddress.getLocalHost(); System.out.println(address1); InetAddress address2 = InetAddress.getByName("127.0.0.1"); System.out.println(address2); InetAddress address3 = InetAddress.getByName("localhost"); System.out.println(address3); InetAddress address4 = InetAddress.getByName("www.baidu.com"); System.out.println(address4); InetAddress address5 = InetAddress.getByName("www.ali.com"); System.out.println(address5); } catch (UnknownHostException e) { e.printStackTrace(); } }}
L87Y12K91TH8M2R/192.168.75.1/127.0.0.1localhost/127.0.0.1www.baidu.com/182.61.200.7www.ali.com/72.52.10.14
#查看所有端口netstat -ano#查看指定端口netstat -ano | findstr "3306"
package com.qing.demo01;import java.net.InetSocketAddress;public class TestInetSocketAddress { public static void main(String[] args) { InetSocketAddress socketAddress = new InetSocketAddress("127.0.0.1", 8080); InetSocketAddress socketAddress1 = new InetSocketAddress("localhost", 8080); System.out.println(socketAddress); System.out.println(socketAddress1); System.out.println(socketAddress1.getAddress()); System.out.println(socketAddress1.getHostName()); System.out.println(socketAddress1.getPort()); }}
/127.0.0.1:8080localhost/127.0.0.1:8080localhost/127.0.0.1localhost8080
package com.qing.demo02;import java.io.IOException;import java.io.OutputStream;import java.net.InetAddress;import java.net.Socket;import java.net.UnknownHostException;import java.util.Scanner;/** * 客户端 */public class TestClient01 { public static void main(String[] args) { Socket socket = null; OutputStream os = null; Scanner scanner = null; try { //1.声明服务端的IP和端口号 InetAddress serverIP = InetAddress.getByName("127.0.0.1"); int port = 9999; //2.创建socket连接 socket = new Socket(serverIP,port); //3.发送消息 os = socket.getOutputStream(); os.write("欢迎进入武侠世界!".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if (os != null) { try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if (socket != null) { try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
package com.qing.demo02;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.ServerSocket;import java.net.Socket;/** * 服务端 */public class TestServer01 { public static void main(String[] args) { ServerSocket serverSocket = null; Socket client = null; InputStream is = null; ByteArrayOutputStream baos = null; try { //1.设置端口 serverSocket = new ServerSocket(9999); while (true) { //2.等待客户端连接 client = serverSocket.accept(); System.out.println(client.getInetAddress()); System.out.println(client.getPort()); //3.读取客户端的消息 is = client.getInputStream(); //管道流 baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len; while ((len=is.read(buffer)) != -1) { baos.write(buffer,0,len); } System.out.println(baos.toString()); } } catch (IOException e) { e.printStackTrace(); } finally { if (baos != null) { try { baos.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (client != null) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } if (serverSocket != null) { try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } } } }}
欢迎进入武侠世界!/127.0.0.113650欢迎进入武侠世界!/127.0.0.113666欢迎进入武侠世界!
package com.qing.demo02;import java.io.*;import java.net.InetAddress;import java.net.Socket;/** * 客户端 */public class TestClient02 { public static void main(String[] args) throws IOException { //1.创建socket连接 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999); //2.创建输出流 OutputStream os = socket.getOutputStream(); //3.读取文件 FileInputStream fis = new FileInputStream("D:\\code\\JavaSE\\net\\qing.jpg"); //4.写出文件 byte[] buffer = new byte[1024]; int len; while ((len=fis.read(buffer)) != -1) { os.write(buffer,0,len); } //通知服务端,已经传输完毕 socket.shutdownOutput(); //5.确认服务端接受完毕,才能断开连接 InputStream is = socket.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer2 = new byte[1024]; int len2; while ((len2=is.read(buffer2)) != -1) { baos.write(buffer2,0,len2); } System.out.println(baos.toString()); //6.关闭资源 fis.close(); os.close(); socket.close(); }}
package com.qing.demo02;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.ServerSocket;import java.net.Socket;/** * 服务端 */public class TestServer02 { public static void main(String[] args) throws IOException { //1.创建服务 ServerSocket serverSocket = new ServerSocket(9999); //2.监听客户端的连接 Socket client = serverSocket.accept();//阻塞式监听,会一直等待客户端连接 //3.获取输入流 InputStream is = client.getInputStream(); //4.文件输出 FileOutputStream fos = new FileOutputStream("D:\\code\\JavaSE\\net\\receive.jpg"); byte[] buffer = new byte[1024]; int len; while ((len=is.read(buffer)) != -1) { fos.write(buffer,0,len); } //5.通知客户端,已接受完毕 OutputStream os = client.getOutputStream(); os.write("服务端已接受完毕,可以断开连接".getBytes()); //6.关闭资源 fos.close(); is.close(); client.close(); serverSocket.close(); }}
package com.qing.demo03;import java.io.IOException;import java.net.*;/** * udp发送端,udp不需要连接服务端 */public class UdpSend01 { public static void main(String[] args) throws IOException { //1.创建socket DatagramSocket socket = new DatagramSocket(); //2.创建数据报包 String msg = "秋风扫落叶"; DatagramPacket packet = new DatagramPacket(msg.getBytes(),0,msg.getBytes().length,InetAddress.getByName("localhost"),9999); //3.发送数据报包 socket.send(packet); //4.关闭资源 socket.close(); }}
package com.qing.demo03;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;/** * udp接收端,udp没有服务端 */public class UdpReceive01 { public static void main(String[] args) throws IOException { //1.开放端口 DatagramSocket socket = new DatagramSocket(9999); //2.接收数据报包 byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length); socket.receive(packet);//阻塞接收 System.out.println(packet.getAddress().getHostAddress()); System.out.println(packet.getPort()); System.out.println(new String(packet.getData())); //关闭资源 socket.close(); }}
127.0.0.164144秋风扫落叶
package com.qing.chat;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetSocketAddress;import java.net.SocketException;public class UdpSender01 { public static void main(String[] args) throws IOException { //1.开放端口 DatagramSocket socket = new DatagramSocket(8888); //2.读取控制台输入 BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while (true) { String data = reader.readLine(); byte[] datas = data.getBytes(); DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress("localhost",9999)); //3.发送数据报包 socket.send(packet); if ("bye".equals(data)) { break; } } //4.关闭资源 socket.close(); }}
左一拳右一拳上一拳下一拳bye
package com.qing.chat;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;public class UdpReceive01 { public static void main(String[] args) throws IOException { //1.开放端口 DatagramSocket socket = new DatagramSocket(9999); //2.接收数据报包 while (true) { byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length); socket.receive(packet);//阻塞式接收 byte[] datas = packet.getData(); String data = new String(datas).trim(); System.out.println(data); if ("bye".equals(data)) { break; } } //3.关闭资源 socket.close(); }}
左一拳右一拳上一拳下一拳bye
package com.qing.chat;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetSocketAddress;import java.net.SocketException;public class TalkSend implements Runnable { private String toIP; private int toPort; private DatagramSocket socket = null; private BufferedReader reader = null; public TalkSend(String toIP, int toPort) { this.toIP = toIP; this.toPort = toPort; reader = new BufferedReader(new InputStreamReader(System.in)); try { socket = new DatagramSocket(); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run() { while (true) { String data = null; try { data = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } byte[] datas = data.getBytes(); DatagramPacket packet = new DatagramPacket(datas,0,datas.length,new InetSocketAddress(toIP,toPort)); try { socket.send(packet); } catch (IOException e) { e.printStackTrace(); } if ("bye".equals(data)) { break; } } socket.close(); }}
package com.qing.chat;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.SocketException;public class TalkReceive implements Runnable { private int port; private String msgFrom; private DatagramSocket socket = null; public TalkReceive(int port,String msgFrom) { this.port = port; this.msgFrom = msgFrom; try { socket = new DatagramSocket(port); } catch (SocketException e) { e.printStackTrace(); } } @Override public void run() { while (true) { byte[] buffer = new byte[1024]; DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length); try { socket.receive(packet);//阻塞式接收 } catch (IOException e) { e.printStackTrace(); } byte[] datas = packet.getData(); String data = new String(datas).trim(); System.out.println(msgFrom + "->" + data); if ("bye".equals(data)) { break; } } //3.关闭资源 socket.close(); }}
package com.qing.chat;public class TalkStudent { public static void main(String[] args) { //开启两个线程 new Thread(new TalkSend("localhost",9999)).start(); new Thread(new TalkReceive(8888,"老师")).start(); }}
package com.qing.chat;public class TalkTeacher { public static void main(String[] args) { //开启两个线程 new Thread(new TalkSend("localhost",8888)).start(); new Thread(new TalkReceive(9999,"学生")).start(); }}
老师好老师->同学好老师辛苦了老师->为学生服务
学生->老师好同学好学生->老师辛苦了为学生服务
https://192.168.10.217:80/demo/index.html协议://IP:端口/项目名/资源
package com.qing.demo04;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;/** * URL下载网络资源 */public class UrlDown { public static void main(String[] args) throws IOException { //1.下载地址 URL url = new URL("https://dss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/topnav/zhidao@2x-e9b427ecc4.png"); //2.连接资源 HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection(); InputStream is = httpURLConnection.getInputStream(); FileOutputStream fos = new FileOutputStream("urlDown.png"); byte[] buffer = new byte[1024]; int len; while ((len=is.read(buffer)) != -1) { fos.write(buffer,0,len); } fos.close(); is.close(); httpURLConnection.disconnect();//断开连接 }}
转载地址:http://pjfbz.baihongyu.com/