JAVA開發系列之:網絡編程

一:網絡知識
1:JAVA程序調用計算機命令
try {
            //查詢當前機器的IP信息
            Process pro = Runtime.getRuntime().exec("ipconfig");
            InputStream is = pro.getInputStream();
            InputStreamReader isr = new InputStreamReader(is,"GBK");
            BufferedReader br = new BufferedReader(isr);
            String str = null;
            while((str = br.readLine())!=null) {
                System.out.println(str);
            }
            br.close();
            isr.close();
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
2:網絡分類
局域網,城域網,廣域網
3:網絡分層
一:應用層
支持網絡應用,應用協議僅僅是網絡應用的一個組成部分,運行在不一樣主機上的進程則使用應用層協議進行通訊。主要的協有:http、ftp、telnet、smtp、pop3等。
二:傳輸層
負責爲信源和信宿提供應用程序進程間的數據傳輸服務,這一層上主要定義了兩個傳輸協議,傳輸控制協議即TCP和用戶數據報協議UDP。
三:網絡層
負責將數據報獨立地從信源發送到信宿,主要解決路由選擇、擁塞控制和網絡互聯等問題。
四:數據鏈路層
負責將IP數據報封裝成合適在物理網絡上傳輸的幀格式並傳輸,或將從物理網絡接收到的幀解封,取出IP數據報交給網絡層。
五:物理層
負責將比特流在結點間傳輸,即負責物理傳輸。該層的協議既與鏈路有關也與傳輸介質有關。
4:IP地址的分類
A類:1~126
B類:128~191
C類:192~223
D類:224~239(組播通訊)
E類:240~254(科學研究)
5:端口
計算機與外界通訊的入口/出口,值介於0~65535之間(0~2^16)(每一個程序都有一個端口)。java

二:TCP/IP通訊編程

代碼實現:
TCP/IP編程實現
客戶端:
public class Client {
    public static void main(String[] args) throws UnknownHostException, IOException {
        Socket socket = new Socket("localhost",8899);
        OutputStream os = socket.getOutputStream();
        InputStream is = socket.getInputStream();
        String mess = "hello";
        os.write(mess.getBytes());
        socket.shutdownOutput();
        int num;
        while((num = is.read())!=-1){
            System.out.print((char)num);
        }
        is.close();
        os.close();
        socket.close();
    }
}
服務器端:
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(8899);
        Socket socket = ss.accept();
        InputStream is = socket.getInputStream();
        OutputStream os = socket.getOutputStream();
        int num;
        while((num = is.read())!=-1){
            System.out.print((char)num);
        }
        String mess = "me too";
        os.write(mess.getBytes());
        socket.shutdownOutput();
        os.close();
        is.close();
        socket.close();
        ss.close();
    }
}
三:UPD通訊服務器

代碼實現:
客戶端A:
package com.company.demo.server;網絡

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.Scanner;socket

public class Test13 {
    public static void main(String[] args) {
        try {
            DatagramSocket service = new DatagramSocket(6666);
            InputA i = new InputA(service);
            OutputA o = new OutputA(service);
            Thread thread1 = new Thread(i);
            Thread thread2 = new Thread(o);
            thread1.start();
            thread2.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }ide

    // 負責輸出的內部類
    private static class InputA implements Runnable {this

        private DatagramSocket service;
        private byte[] input;
        private DatagramPacket packet;.net

        public InputA(DatagramSocket service) {
            super();
            this.service = service;
            this.input = new byte[1024];
            this.packet = new DatagramPacket(input, 0, input.length);
        }server

        @Override
        public void run() {
            while (true) {
                show();
            }
        }進程

        public void show() {
            try {
                service.receive(packet);
                input = packet.getData();
                System.out.println(new String(input, 0, input.length));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    // 負責輸入的內部類
    private static class OutputA implements Runnable {

        private DatagramSocket service;
        private byte[] output;
        private DatagramPacket packet;
        private InetSocketAddress socket;
        private Scanner scan;

        public OutputA(DatagramSocket service) {
            super();
            this.service = service;
            this.socket = new InetSocketAddress("localhost", 8888);
            this.output = new byte[1024];
        }

        @Override
        public void run() {
            while (true) {
                show();
            }
        }

        public void show() {
            try {
                scan = new Scanner(System.in);
                String str = scan.next();
                output = str.getBytes();
                packet = new DatagramPacket(output, output.length, socket);
                service.send(packet);
            } catch (Exception e) {

            }
        }

    }

}


客戶端B:
package com.company.demo.server;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.Scanner;


public class Test14 {
    public static void main(String[] args) {
        try {
            DatagramSocket service = new DatagramSocket(8888);
            InputA i = new InputA(service);
            OutputA o = new OutputA(service);
            Thread thread1 = new Thread(i);
            Thread thread2 = new Thread(o);
            thread1.start();
            thread2.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 負責輸出的內部類
    private static class InputA implements Runnable {

        private DatagramSocket service;
        private byte[] input;
        private DatagramPacket packet;

        public InputA(DatagramSocket service) {
            super();
            this.service = service;
            this.input = new byte[1024];
            this.packet = new DatagramPacket(input, 0, input.length);
        }

        @Override
        public void run() {
            while (true) {
                show();
            }
        }

        public void show() {
            try {
                service.receive(packet);
                input = packet.getData();
                System.out.println(new String(input, 0, input.length));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    // 負責輸入的內部類
    private static class OutputA implements Runnable {

        private DatagramSocket service;
        private byte[] output;
        private DatagramPacket packet;
        private InetSocketAddress socket;
        private Scanner scan;

        public OutputA(DatagramSocket service) {
            super();
            this.service = service;
            this.socket = new InetSocketAddress("localhost", 6666);
            this.output = new byte[1024];
        }

        @Override
        public void run() {
            while (true) {
                show();
            }
        }

        public void show() {
            try {
                scan = new Scanner(System.in);
                String str = scan.next();
                output = str.getBytes();
                packet = new DatagramPacket(output, output.length, socket);
                service.send(packet);
            } catch (Exception e) {

            }
        }

    }

}

相關文章
相關標籤/搜索