真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網站制作重慶分公司

【java】網絡編程-創(chuàng)新互聯(lián)

文章目錄
  • 網絡編程概述
    • 基本概念
    • IP地址概念
    • InetAddress
    • 端口與協(xié)議概念
  • UDP通信編程
    • UDP發(fā)送數(shù)據(jù)
    • UDP接受數(shù)據(jù)
    • UDP通信程序練習
  • TCP通信編程
    • TCP發(fā)送數(shù)據(jù)
    • TCP接收數(shù)據(jù)
    • TCP通信程序練習

網站建設哪家好,找成都創(chuàng)新互聯(lián)!專注于網頁設計、網站建設、微信開發(fā)、小程序設計、集團企業(yè)網站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了錦屏免費建站歡迎大家使用!網絡編程概述 基本概念

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

IP地址概念

在這里插入圖片描述
終端檢查:
在這里插入圖片描述

InetAddress

在這里插入圖片描述

package heima.網絡編程;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class inetaddress {public static void main(String[] args) throws UnknownHostException {//public static InetAddress getByName(String host):確定主機名稱的IP地址,主機名稱可以是機器名稱,也可以是IP地址
//        InetAddress address = InetAddress.getByName("ludan");
        InetAddress address = InetAddress.getByName("192.168.3.55");

        //public String getHostName():獲取此IP地址的主機名。
        String name = address.getHostName();
        //public String getHostAddress():返回文本顯示中的IP地址字符串。
        String ip = address.getHostAddress();

        System.out.println("主機名:"+name);
        System.out.println("IP地址:"+ip);

    }
}
端口與協(xié)議概念

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

UDP通信編程 UDP發(fā)送數(shù)據(jù)

在這里插入圖片描述
在這里插入圖片描述

package heima.網絡編程;

import java.io.IOException;
import java.net.*;

public class udp {public static void main(String[] args) throws IOException {//創(chuàng)建發(fā)送端的Socket對象(DatagramSocket)
        //public DatagramSocket() 構造數(shù)據(jù)報套接字并將其綁定到本地主機上的任何可用端口。
        DatagramSocket ds = new DatagramSocket();

        //創(chuàng)建數(shù)據(jù),并把數(shù)據(jù)打包
        //DatagramPacket (byte[] buf,int length,InetAddress address,int part)
        //構造一個數(shù)據(jù)包,發(fā)送長度為length的數(shù)據(jù)包到指定主機上的指定端口號。
        byte[] bys = "hello,udp,我來了".getBytes();
        DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.3.55"),8080);
        //way2
//        int length = bys.length;
//        InetAddress address = InetAddress.getByName("192.168.3.55");
//        int part = 10086;
//        DatagramPacket dp = new DatagramPacket(bys,length,address,part);


        //調用DatagramSocket對象的方法發(fā)送數(shù)據(jù)
        //void send(DatagramSocket p) 從此套接字發(fā)送數(shù)據(jù)包
        ds.send(dp);

        //關閉發(fā)送端
        //void vlose() 關閉此數(shù)據(jù)報套接字
        ds.close();
    }
}
UDP接受數(shù)據(jù)

在這里插入圖片描述

package heima.網絡編程;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ReceiveUDP {public static void main(String[] args) throws IOException {   //參加接受端的Socket對象(DatagramSocket)
        //DatagramSocket (int part) 構造數(shù)據(jù)報套接字并將其綁定到本地主機上的指定端口
        DatagramSocket ds = new DatagramSocket(8080);

        //創(chuàng)建一個數(shù)據(jù)包,用于接收數(shù)據(jù)
        //DatagramSocket(byte[] buf,int length) 構造一個DatagramPacket用于接收長度為length數(shù)據(jù)
        byte[] bys = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bys, bys.length);

        //調用DatagramSocket對象的方法接收數(shù)據(jù)
        ds.receive(dp);

        //解析數(shù)據(jù)包,并把數(shù)在控制臺顯示
        //byte[] getData() 返回數(shù)據(jù)緩沖區(qū)
//        byte[] datas = dp.getData();
        //int getlength() 返回要發(fā)送的數(shù)據(jù)長度或接受到的數(shù)據(jù)的長度
//        int len = dp.getLength();
//        String dataString = new String(datas,0,len);
//        System.out.println("數(shù)據(jù)是:"+dataString);
        System.out.println("數(shù)據(jù)是:"+new String(dp.getData(),0,dp.getLength()));

        //關閉接收端
        ds.close();
    }
}

很奇怪,接受不到數(shù)據(jù)。。

UDP通信程序練習

在這里插入圖片描述
創(chuàng)建了一個item03的路徑,在此路徑下:
udp1.java

package heima.網絡編程.item03;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class udp1 {public static void main(String[] args) throws IOException {//創(chuàng)建發(fā)送端的Socket對象(DatagramSocket)
        DatagramSocket ds = new DatagramSocket();

        //自己封裝鍵盤錄入數(shù)據(jù)
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = br.readLine())!=null){//輸入的數(shù)據(jù)是886,發(fā)送數(shù)據(jù)結束
            if ("886".equals(line)){break;
            }

            //創(chuàng)建數(shù)據(jù),并把數(shù)據(jù)打包
            byte[] bys = line.getBytes();
            DatagramPacket dp = new DatagramPacket(bys, bys.length, InetAddress.getByName("192.168.3.55"),12345);

            //調用DatagramPacket對象的方法發(fā)送數(shù)據(jù)
            ds.send(dp);
        }

        //關閉發(fā)送源
        ds.close();
    }
}

Receive.java

package heima.網絡編程.item03;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receive1 {public static void main(String[] args) throws IOException {//創(chuàng)建接收端Socket對象(DatagramSocket)
        DatagramSocket ds = new DatagramSocket(12345);

        while (true){//創(chuàng)建一個數(shù)據(jù)包,用于接收數(shù)據(jù)
            byte[] bys = new byte[1024];
            DatagramPacket dp = new DatagramPacket(bys, bys.length);

            //調用DatagramSocket對象的方法接收數(shù)據(jù)
            ds.receive(dp);

            //解析數(shù)據(jù)包,并把數(shù)據(jù)在控制臺顯示
            System.out.println("數(shù)據(jù)是:"+new String(dp.getData(),0,dp.getLength()));
        }

//        //關閉接收端
//        ds.close();
        //因為要一直接收數(shù)據(jù),所以它也不用關了
    }
}
TCP通信編程 TCP發(fā)送數(shù)據(jù)

在這里插入圖片描述

package heima.網絡編程.item04;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class ClientDemo {public static void main(String[] args) throws IOException {//創(chuàng)建客戶端的Socket對象(Socket)
        //Socket(InetAddress address,int part) 創(chuàng)建流套接字并將其連接到指定的IP地址的指定端口號
//        Socket s = new Socket(InetAddress.getByName("192.168.3.55"),10000);
        //Socket(String host,int part)創(chuàng)建流套接字并將其連接到指定主機上的指定端口號
        Socket s = new Socket("192.168.3.55",10000);

        //獲取輸出流,寫數(shù)據(jù)
        //OutputStream getOutputStream() 返回此套接字的輸出流
        OutputStream os = s.getOutputStream();
        os.write("hello,tcp,我來了".getBytes());

        //釋放資源
        s.close();
    }
}

現(xiàn)在運行會報錯,是因為還缺少TCP數(shù)據(jù)的接收

TCP接收數(shù)據(jù)

在這里插入圖片描述

package heima.網絡編程.item04;

import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class ClientDemo {public static void main(String[] args) throws IOException {//創(chuàng)建客戶端的Socket對象(Socket)
        //Socket(InetAddress address,int part) 創(chuàng)建流套接字并將其連接到指定的IP地址的指定端口號
//        Socket s = new Socket(InetAddress.getByName("192.168.3.55"),10000);
        //Socket(String host,int part)創(chuàng)建流套接字并將其連接到指定主機上的指定端口號
        Socket s = new Socket("192.168.3.55",12345);

        //獲取輸出流,寫數(shù)據(jù)
        //OutputStream getOutputStream() 返回此套接字的輸出流
        OutputStream os = s.getOutputStream();
        os.write("hello,tcp,我來了".getBytes());

        //釋放資源
        s.close();
    }
}

在這里插入圖片描述

TCP通信程序練習
  • 練習1 --服務器給出反饋
    在這里插入圖片描述
    在這里插入圖片描述
package heima.網絡編程.P349;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class ClientDemo {public static void main(String[] args) throws IOException {//創(chuàng)建客戶端的Socket對象
        Socket s = new Socket("192.168.3.55",12345);

        //獲取輸出流,寫數(shù)據(jù)
        OutputStream os = s.getOutputStream();
        os.write("hello,tcp,我來了".getBytes());

        //接受服務器反饋
        InputStream is = s.getInputStream();
        byte[] bys = new byte[1024];
        int len = is.read(bys);
        String data = new String(bys,0,len);
        System.out.println("客戶端:"+data);

        //釋放資源
        s.close();
    }
}
package heima.網絡編程.P349;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {public static void main(String[] args) throws IOException {//創(chuàng)建服務端的Socket對象
        ServerSocket ss = new ServerSocket(12345);

        //監(jiān)聽客戶端連接,返回一個Socket對象
        Socket s = ss.accept();

        //獲取輸入流,讀數(shù)據(jù),并把數(shù)據(jù)顯示在控制臺
        InputStream is = s.getInputStream();
        byte[] bys = new byte[1024];
        int len = is.read(bys);
        String data = new String(bys,0,len);
        System.out.println("服務器:"+data);

        //給出反饋
        OutputStream os = s.getOutputStream();
        os.write("數(shù)據(jù)已收到".getBytes());

        //釋放資源
        ss.close();
    }
}

在這里插入圖片描述
在這里插入圖片描述

注:

  1. 只釋放Socket對象就ok了,其他不用再釋放
  2. 端口報錯,就換一個端口
  • 練習2 --客戶數(shù)據(jù)來自于鍵盤錄入
package heima.網絡編程.TPC練習2;

import java.io.*;
import java.net.Socket;

public class ClientDemo {public static void main(String[] args) throws IOException {Socket s = new Socket("192.168.3.55",12345);

        //數(shù)據(jù)來自于鍵盤錄入,知道輸入的是886,發(fā)送數(shù)據(jù)結束
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        //封裝輸出流對象
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        String line;
        while ((line= br.readLine())!=null){if ("886".equals(line)){break;
            }

            //獲取輸出流對象
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //釋放資源4
        s.close();
    }
}
package heima.網絡編程.TPC練習2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {public static void main(String[] args) throws IOException {//創(chuàng)建服務器Socket對象
        ServerSocket ss = new ServerSocket(12345);

        //監(jiān)聽客戶端的連接,返回一個對應的Socket對象
        Socket s = ss.accept();

        //獲取輸入流
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String line;
        while ((line = br.readLine())!=null){System.out.println(line);
        }

        //釋放資源
        ss.close();
    }
}

在ClientDemo.java終端輸入,則在ServerDemo處輸出,知道輸入886為止
在這里插入圖片描述
在這里插入圖片描述

  • 練習3 --服務器數(shù)據(jù)寫入文本文件
package heima.網絡編程.TPC練習3;

import java.io.*;
import java.net.Socket;

public class ClientDemo {public static void main(String[] args) throws IOException {Socket s = new Socket("192.168.3.55",12345);

        //數(shù)據(jù)來自于鍵盤錄入,知道輸入的是886,發(fā)送數(shù)據(jù)結束
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        //封裝輸出流對象
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        String line;
        while ((line=br.readLine())!=null){if ("886".equals(line)){break;
            }
            //獲取輸出流對象
            bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //釋放資源
        s.close();
    }
}
package heima.網絡編程.TPC練習3;

import kuang.P70.B;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {public static void main(String[] args) throws IOException {//創(chuàng)建服務器Socket對象
        ServerSocket ss =new ServerSocket(12345);

        //監(jiān)聽客戶端連接,返回一個對應的Socket對象
        Socket s = ss.accept();

        //接收數(shù)據(jù)
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        //把數(shù)據(jù)寫入文本文件
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\路聃\\Desktop\\Java\\s.txt"));

        String line;
        while ((line= br.readLine())!=null){bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //釋放資源
        bw.close();
        ss.close();
    }
}

輸出:
在這里插入圖片描述在這里插入圖片描述

  • 練習4 --客戶端數(shù)據(jù)來自文本文件

莫名出錯

package heima.網絡編程.TPC練習4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(12345);

        Socket s = ss.accept();

        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\路聃\\Desktop\\Java\\1.txt"));

        String line;
        while ((line= br.readLine())!=null){bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //釋放資源
        bw.close();
        ss.close();
    }
}
package heima.網絡編程.TPC練習4;

import java.io.*;
import java.net.Socket;

public class ClientDemo {public static void main(String[] args) throws IOException {Socket s =new Socket("192.168.3.55",12345);

        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\路聃\\Desktop\\Java\\s.txt"));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

        String line;
        while ((line= br.readLine())!=null){bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //釋放資源
        br.close();
        s.close();
    }
}
  • 練習5 --上傳文件服務器給出的反饋
package heima.網絡編程.TPC練習5;

import java.io.*;
import java.net.Socket;

public class ClientDemo {public static void main(String[] args) throws IOException {Socket s = new Socket("192.168.3.55",10000);

        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\路聃\\Desktop\\Java\\s.txt"));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

        String line;;
        while ((line= br.readLine())!=null){bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //public void shutdownOutput() --非自定義結束標記
        s.shutdownInput();

        //接收反饋
        BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String data = brClient.readLine();
        System.out.println("服務器的反饋:"+data);

        //釋放資源
        br.close();
        s.close();
    }
}
package heima.網絡編程.TPC練習5;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {public static void main(String[] args) throws IOException {ServerSocket ss = new ServerSocket(10000);

        Socket s = ss.accept();

        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\路聃\\Desktop\\Java\\Copy.txt"));

        String line;
        while ((line= br.readLine())!=null){bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //給出反饋
        BufferedWriter bwServer = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
        bwServer.write("文件上傳成功");
        bwServer.newLine();
        bwServer.flush();
    }
}

連接端口失敗,原因未知

  • 練習6 --多線陳實現(xiàn)文件上傳
package heima.網絡編程.TPC練習6;

import java.io.*;
import java.net.Socket;

public class ClientDemo {public static void main(String[] args) throws IOException {Socket s = new Socket("192.168.3.55",12345);

        BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\路聃\\Desktop\\Java\\s.txt"));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));

        String line;;
        while ((line= br.readLine())!=null){bw.write(line);
            bw.newLine();
            bw.flush();
        }

        //public void shutdownOutput() --非自定義結束標記
        s.shutdownInput();

        //接收反饋
        BufferedReader brClient = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String data = brClient.readLine();
        System.out.println("服務器的反饋:"+data);

        //釋放資源
        br.close();
        s.close();
    }
}
package heima.網絡編程.TPC練習6;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerDemo {public static void main(String[] args) throws IOException {//創(chuàng)建服務器Socket對象
        ServerSocket ss = new ServerSocket(12345);

        while (true){//監(jiān)聽客戶端連接,返回一個對應的Socket對象
            Socket s = ss.accept();
            //為每一個客戶端開啟一個線程
            new Thread(new ServerThread(s)).start();
        }
        //為什么不釋放資源了?
    }
}
package heima.網絡編程.TPC練習6;

import java.io.*;
import java.net.Socket;

public class ServerThread implements Runnable{private Socket s;
    public ServerThread(Socket s){this.s = s;}

    @Override
    public void run() {try {//接收數(shù)據(jù)寫到文本文件
            BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));

            //解決名稱沖突問題
            int count = 0;
            File file = new File("C:\\Users\\路聃\\Desktop\\Java\\Copy["+count+"].java");
            while (file.exists()){count++;
                file = new File("C:\\Users\\路聃\\Desktop\\Java\\Copy["+count+"].java");
            }
            BufferedWriter bw = new BufferedWriter(new FileWriter(file));

            String line;
            while ((line= br.readLine())!=null){bw.write(line);
                bw.newLine();
                bw.flush();
            }
        } catch (IOException e) {e.printStackTrace();
        }
    }
}

依然顯示連接失敗。。。

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧


網頁標題:【java】網絡編程-創(chuàng)新互聯(lián)
URL分享:http://weahome.cn/article/depigj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部