网络编程

如何定位到网络上的一台主机

tcp

  • 稳定

  • 三次握手
    最少需要三次

  • 四次挥手
    二次确认

UDP

  • 不连接不稳定
  • 不考虑接收者的情况
  • DDOS

TCP实现聊天

  1. package com.example.demo.utils;
  2. import org.apache.catalina.connector.OutputBuffer;
  3. import java.io.*;
  4. import java.net.InetAddress;
  5. import java.net.ServerSocket;
  6. import java.net.Socket;
  7. //接收
  8. public class ScoketDemo {
  9. public static void main(String[] args) {
  10. ServerSocket serverSocket = null;
  11. Socket socket = null;
  12. BufferedReader read = null;
  13. try {
  14. //创建socket服务
  15. int port = 9999;
  16. serverSocket = new ServerSocket(port);
  17. while(true) {
  18. //接受连接,形成传输通道
  19. socket = serverSocket.accept();
  20. //从socket写到内存中,对于内存是输入所以是输入流
  21. read = new BufferedReader(new InputStreamReader(socket.getInputStream()));
  22. System.out.println(read.readLine());
  23. }
  24. } catch (IOException e) {
  25. e.printStackTrace();
  26. } finally {
  27. if(read != null) {
  28. try {
  29. read.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. if(serverSocket != null) {
  42. try {
  43. serverSocket.close();
  44. } catch (IOException e) {
  45. e.printStackTrace();
  46. }
  47. }
  48. }
  49. }
  50. }
  51. //发送
  52. class client{
  53. public static void main(String[] args) {
  54. Socket socket = null;
  55. PrintWriter writer = null;
  56. try {
  57. //连接到socket服务器
  58. socket = new Socket("127.0.0.1", 9999);
  59. //写数据到socket
  60. writer = new PrintWriter(socket.getOutputStream(), true);
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. } finally {
  64. if (writer != null) {
  65. writer.close();
  66. }
  67. if (socket != null) {
  68. try {
  69. socket.close();
  70. } catch (IOException e) {
  71. e.printStackTrace();
  72. }
  73. }
  74. }
  75. }
  76. }

UDP实现消息发送

  1. class UdpClient {
  2. public static void main(String[] args) throws IOException {
  3. //创建socket
  4. DatagramSocket socket = new DatagramSocket();
  5. //创建数据包
  6. DatagramPacket packet = new DatagramPacket("msg".getBytes(), 0, "msg".getBytes().length, InetAddress.getByName("localhost"), 9999);
  7. //发送数据包
  8. socket.send(packet);
  9. }
  10. }
  11. class UdpServer {
  12. public static void main(String[] args) throws IOException {
  13. //开放端口
  14. DatagramSocket server = new DatagramSocket(9999);
  15. //定义数据包
  16. DatagramPacket datagramPacket = new DatagramPacket(new byte[1024],0,new byte[1024].length);
  17. //阻塞接受
  18. server.receive(datagramPacket);
  19. System.out.println(datagramPacket.getAddress().getHostAddress());
  20. System.out.println(datagramPacket.getData());
  21. }
  22. }

使用udp实现连天功能

发送者

  1. import java.io.IOException;
  2. import java.net.*;
  3. import java.util.Scanner;
  4. public class UdpSender {
  5. public static void main(String[] args) throws IOException {
  6. DatagramSocket socket = new DatagramSocket(9998);
  7. while(true) {
  8. Scanner sc = new Scanner(System.in);
  9. String msg = sc.nextLine();
  10. InetAddress inetAddress = InetAddress.getByName("localhost");
  11. DatagramPacket datagramPacket = new DatagramPacket(msg.getBytes(), 0, msg.length(), inetAddress, 9999);
  12. socket.send(datagramPacket);
  13. }
  14. }
  15. }