1.网络编程有两个主要的问题:
2.网络编程的要素
cmd->ipconfig:查看本机IP地址
1 package com.KingJ.lesson1; 2 3 import java.net.Inet4Address; 4 import java.net.InetAddress; 5 import java.net.UnknownHostException; 6 7 public class TestInetAddress { 8 public static void main(String[] args) { 9 try { 10 //查询本机地址 11 InetAddress inetaddress1 = InetAddress.getByName("127.0.0.1"); 12 System.out.println(inetaddress1); 13 InetAddress inetaddress2 = InetAddress.getByName("localhost"); 14 System.out.println(inetaddress2); 15 InetAddress inetaddress3 = InetAddress.getLocalHost(); 16 System.out.println(inetaddress3); 17 18 //查询网站地址 19 InetAddress inetaddress4 = InetAddress.getByName("www.baidu.com"); 20 System.out.println(inetaddress4); 21 22 //常用方法 23 System.out.println(inetaddress4.getAddress()); 24 System.out.println(inetaddress4.getCanonicalHostName());//获取规范的名字 25 System.out.println(inetaddress4.getHostAddress());//IP 26 System.out.println(inetaddress4.getHostName());//域名,或自己电脑的名字 27 28 29 } catch (UnknownHostException e) { 30 e.printStackTrace(); 31 } 32 } 33 }
常见DOS命令:
1 package com.KingJ.lesson1; 2 3 import java.net.InetSocketAddress; 4 5 public class TestInetSocketAddress { 6 public static void main(String[] args) { 7 InetSocketAddress inetSocketAddress1 = new InetSocketAddress("127.168.0.1", 8080); 8 System.out.println(inetSocketAddress1); 9 InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost", 8080); 10 System.out.println(inetSocketAddress2); 11 12 System.out.println(inetSocketAddress1.getAddress()); 13 System.out.println(inetSocketAddress1.getHostName());//地址 14 System.out.println(inetSocketAddress1.getPort());//端口 15 } 16 }
客户端:
1 package com.KingJ.lesson2; 2 3 import java.io.IOException; 4 import java.io.OutputStream; 5 import java.net.InetAddress; 6 import java.net.Socket; 7 import java.net.UnknownHostException; 8 import java.nio.charset.StandardCharsets; 9 10 public class TestClientdemo01 { 11 public static void main(String[] args) { 12 Socket socket = null; 13 OutputStream os = null; 14 //1.要知道服务器的地址,端口号 15 try { 16 //1.要知道服务器的地址,端口号 17 InetAddress serverIP = InetAddress.getByName("127.0.0.1"); 18 int port = 9999; 19 //2.创建一个socket连接 20 socket = new Socket(serverIP, port); 21 //3.发送消息 I/O流 22 os = socket.getOutputStream(); 23 os.write("你好,请滚出去".getBytes()); 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } finally { 27 if (os != null) { 28 try { 29 os.close(); 30 } catch (IOException e) { 31 e.printStackTrace(); 32 } 33 } 34 if (socket != null) { 35 try { 36 socket.close(); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 42 } 43 } 44 45 }
服务器端:
1 package com.KingJ.lesson2; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 9 public class TestServerdemo02 { 10 11 public static void main(String[] args) { 12 ServerSocket serverSocket = null; 13 Socket socket = null; 14 InputStream is = null; 15 ByteArrayOutputStream baos = null; 16 17 try { 18 //1.我得有一个地址 19 serverSocket = new ServerSocket(9999); 20 //2.等待客户端连接过来 21 socket = serverSocket.accept(); 22 //3.读取客户端发来的消息 23 is = socket.getInputStream(); 24 25 //管道流 26 baos = new ByteArrayOutputStream(); 27 byte[] buffer = new byte[1024]; 28 int len; 29 while ((len = is.read(buffer)) != -1) { 30 baos.write(buffer, 0, len); 31 } 32 System.out.println(baos.toString()); 33 34 35 } catch (IOException e) { 36 e.printStackTrace(); 37 } finally { 38 //关闭资源 39 if (baos != null) { 40 try { 41 baos.close(); 42 } catch (IOException e) { 43 e.printStackTrace(); 44 } 45 } 46 if (is != null) { 47 try { 48 is.close(); 49 } catch (IOException e) { 50 e.printStackTrace(); 51 } 52 } 53 if (socket != null) { 54 try { 55 socket.close(); 56 } catch (IOException e) { 57 e.printStackTrace(); 58 } 59 } 60 if (serverSocket != null) { 61 try { 62 serverSocket.close(); 63 } catch (IOException e) { 64 e.printStackTrace(); 65 } 66 } 67 68 69 } 70 } 71 }
服务器端:
1 package com.KingJ.lesson2.TestFile; 2 3 import java.io.FileOutputStream; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.net.ServerSocket; 7 import java.net.Socket; 8 9 public class TestServer1 { 10 public static void main(String[] args) throws Exception { 11 //1.创建服务 12 ServerSocket serverSocket = new ServerSocket(9999); 13 //2.监听客户端的连接 14 Socket socket = serverSocket.accept(); 15 //3.获取输入流 16 InputStream is = socket.getInputStream(); 17 18 //4.文件输出 19 FileOutputStream fos = new FileOutputStream("receive.PNG"); 20 byte[] buffer = new byte[1024]; 21 int len; 22 while ((len = is.read(buffer)) != -1) { 23 fos.write(buffer, 0, len); 24 } 25 26 //通知客户端接收完毕 27 OutputStream os = socket.getOutputStream(); 28 os.write("接收完毕".getBytes()); 29 30 //关闭资源 31 os.close(); 32 fos.close(); 33 is.close(); 34 socket.close(); 35 serverSocket.close(); 36 } 37 }
客户端
1 package com.KingJ.lesson2.TestFile; 2 3 import java.io.*; 4 import java.net.InetAddress; 5 import java.net.Socket; 6 7 public class TestClient1 { 8 public static void main(String[] args) throws Exception { 9 //1.创建一个socket连接 10 Socket socket = new Socket(InetAddress.getByName("127.0.0.1"), 9999); 11 //2.创建一个输出流 12 OutputStream os = socket.getOutputStream(); 13 //3.读取文件 14 FileInputStream fis = new FileInputStream(new File("HTML.PNG")); 15 //4.写出文件 16 byte[] buffer = new byte[1024]; 17 int len; 18 while ((len = fis.read(buffer)) != -1) { 19 os.write(buffer, 0, len); 20 } 21 //通知服务器我已经发送完了 22 socket.shutdownOutput(); 23 //确定服务器接收完毕 24 InputStream inputStream = socket.getInputStream(); 25 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 26 byte[] buffer2 = new byte[1024]; 27 int len2; 28 while ((len = inputStream.read(buffer2)) != -1) { 29 baos.write(buffer, 0, len); 30 } 31 System.out.println(baos.toString()); 32 33 34 //5.关闭资源 35 baos.close(); 36 inputStream.close(); 37 fis.close(); 38 os.close(); 39 socket.close(); 40 41 42 } 43 }
发送方:
1 package com.KingJ.lesson1.lesson4; 2 3 import java.net.DatagramPacket; 4 import java.net.DatagramSocket; 5 import java.net.InetAddress; 6 import java.net.SocketException; 7 8 public class UdpClient1 { 9 public static void main(String[] args) throws Exception { 10 //1.建立一个socket 11 DatagramSocket socket = new DatagramSocket(); 12 //要发送的消息 13 String msg = "你好,请滚出去!"; 14 //发送给谁 15 InetAddress localhost = InetAddress.getByName("localhost"); 16 int port = 9999; 17 //2.建个包 18 DatagramPacket packet = new DatagramPacket(msg.getBytes(), 0, msg.getBytes().length, localhost, port); 19 20 //3.发送包 21 socket.send(packet); 22 23 //关闭流 24 socket.close(); 25 26 } 27 }
接收方:
1 package com.KingJ.lesson1.lesson4; 2 3 import java.net.DatagramPacket; 4 import java.net.DatagramSocket; 5 import java.net.SocketException; 6 7 public class UdpServer1 { 8 public static void main(String[] args) throws Exception { 9 //开放端口 10 DatagramSocket socket = new DatagramSocket(9999); 11 //接收数据包 12 byte[] buffer = new byte[1024]; 13 DatagramPacket packet = new DatagramPacket(buffer, 0, buffer.length); 14 15 socket.receive(packet); 16 System.out.println(new String(packet.getData(), 0, packet.getData().length)); 17 18 //关闭连接 19 socket.close(); 20 21 } 22 }
在线咨询实现:
1 package com.KingJ.chat; 2 3 import java.io.BufferedReader; 4 import java.io.InputStreamReader; 5 import java.net.DatagramPacket; 6 import java.net.DatagramSocket; 7 import java.net.InetSocketAddress; 8 9 public class TalkSend implements Runnable { 10 DatagramSocket socket = null; 11 BufferedReader reader = null; 12 13 private int fromPort;//自己的端口 14 private String toIP;//对方的IP 15 private int toPort;//对方的端口号 16 17 18 public TalkSend(int fromPort, String toIP, int toPort) { 19 this.fromPort = fromPort; 20 this.toIP = toIP; 21 this.toPort = toPort; 22 try { 23 socket = new DatagramSocket(fromPort); 24 reader = new BufferedReader(new InputStreamReader(System.in)); 25 } catch (Exception e) { 26 e.printStackTrace(); 27 } 28 29 } 30 31 @Override 32 public void run() { 33 34 while (true) { 35 try { 36 String data = reader.readLine(); 37 byte[] datas = data.getBytes();//数据转换为字节 38 DatagramPacket packet = new DatagramPacket(datas, 0, datas.length, new InetSocketAddress(this.toIP, this.toPort)); 39 socket.send(packet); 40 if (data.equals("bye")) { 41 break; 42 } 43 44 } catch (Exception e) { 45 e.printStackTrace(); 46 } 47 48 } 49 50 socket.close(); 51 } 52 }
1 package com.KingJ.chat; 2 3 import java.io.IOException; 4 import java.net.DatagramPacket; 5 import java.net.DatagramSocket; 6 import java.net.SocketException; 7 8 public class TalkReceive implements Runnable { 9 DatagramSocket socket = null; 10 private int port; 11 private String msgFrom; 12 13 public TalkReceive(int port, String msgFrom) { 14 this.port = port; 15 this.msgFrom = msgFrom; 16 try { 17 socket = new DatagramSocket(port); 18 } catch (SocketException e) { 19 e.printStackTrace(); 20 } 21 22 } 23 24 @Override 25 public void run() { 26 //准备接收包裹 27 byte[] container = new byte[1024]; 28 while (true) { 29 30 try { 31 DatagramPacket packet = new DatagramPacket(container, 0, container.length); 32 socket.receive(packet); 33 34 byte[] data = packet.getData(); 35 String receiveDatas = new String(data, 0, packet.getLength()); 36 System.out.println(msgFrom + ":" + receiveDatas); 37 38 if (receiveDatas.equals(("bye"))) { 39 break; 40 } 41 } catch (IOException e) { 42 e.printStackTrace(); 43 } 44 45 46 } 47 socket.close(); 48 49 } 50 }
1 package com.KingJ.chat; 2 3 public class TalkStudent { 4 public static void main(String[] args) { 5 new Thread(new TalkSend(7777, "localhost", 9999)).start(); 6 new Thread(new TalkReceive(8888, "老师")).start(); 7 } 8 }
1 package com.KingJ.chat; 2 3 public class TalkTeacher { 4 public static void main(String[] args) { 5 new Thread(new TalkSend(5555, "localhost", 8888)).start(); 6 new Thread(new TalkReceive(9999, "老师")).start(); 7 } 8 }