国产成人精品18p,天天干成人网,无码专区狠狠躁天天躁,美女脱精光隐私扒开免费观看

Java網(wǎng)絡(luò )編程之基于TCP協(xié)議

發(fā)布時(shí)間:2021-07-05 18:40 來(lái)源:腳本之家 閱讀:0 作者:花傷情猶在 欄目: 開(kāi)發(fā)技術(shù)

目錄

一、單向通信

功能:客戶(hù)端發(fā)送一句話(huà)到服務(wù)器

客戶(hù)端:

public class TestClient {//客戶(hù)端
    //這是一個(gè)main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.創(chuàng  )建套接字:指定服務(wù)器的ip和端口號:
        Socket s = new Socket("192.168.199.217",8888);
        //2.對于程序員來(lái)說(shuō),向外發(fā)送數據 感受 --》利用輸出流:
        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        //利用這個(gè)OutputStream就可以向外發(fā)送數據了,但是沒(méi)有直接發(fā)送String的方法
        //所以我們又在OutputStream外面套了一個(gè)處理流:DataOutputStream
        dos.writeUTF("你好");
        //3.關(guān)閉流  +  關(guān)閉網(wǎng)絡(luò )資源:
        dos.close();
        os.close();
        s.close();
    }
}

服務(wù)器端:

public class TestServer {//服務(wù)器
    //這是一個(gè)main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.創(chuàng  )建套接字: 指定服務(wù)器的端口號
        ServerSocket ss = new ServerSocket(8888);
        //2.等著(zhù)客戶(hù)端發(fā)來(lái)的信息:
        Socket s = ss.accept();//阻塞方法:等待接收客戶(hù)端的數據,什么時(shí)候接收到數據,什么時(shí)候程序繼續向下執行。
        //accept()返回值為一個(gè)Socket,這個(gè)Socket其實(shí)就是客戶(hù)端的Socket
        //接到這個(gè)Socket以后,客戶(hù)端和服務(wù)器才真正產(chǎn)生了連接,才真正可以通信了
        //3.感受到的操作流:
        InputStream is = s.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        //4.讀取客戶(hù)端發(fā)來(lái)的數據:
        String str = dis.readUTF();
        System.out.println("客戶(hù)端發(fā)來(lái)的數據為:"+str);
        
        //5.關(guān)閉流+關(guān)閉網(wǎng)絡(luò )資源:
        dis.close();
        is.close();
        s.close();
        ss.close();
    }
}

測試:

先開(kāi)啟客戶(hù)端還是先開(kāi)啟服務(wù)器:先開(kāi)服務(wù)器,再開(kāi)啟客戶(hù)端 側面驗證:先開(kāi)客戶(hù)端:出錯。

如圖:

二、雙向通信

客戶(hù)端:

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

public class TestClient {//客戶(hù)端
    //這是一個(gè)main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.創(chuàng  )建套接字:指定服務(wù)器的ip和端口號:
        Socket s = new Socket("192.168.199.217",8888);
        //2.對于程序員來(lái)說(shuō),向外發(fā)送數據 感受 --》利用輸出流:
        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        //利用這個(gè)OutputStream就可以向外發(fā)送數據了,但是沒(méi)有直接發(fā)送String的方法
        //所以我們又在OutputStream外面套了一個(gè)處理流:DataOutputStream
        dos.writeUTF("你好");
        //接收服務(wù)器端的回話(huà)--》利用輸入流:
        InputStream is = s.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        String str = dis.readUTF();
        System.out.println("服務(wù)器端對我說(shuō):"+str);
        //3.關(guān)閉流  +  關(guān)閉網(wǎng)絡(luò )資源:
        dis.close();
        is.close();
        dos.close();
        os.close();
        s.close();
    }
}

服務(wù)器端:

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

public class TestServer {//服務(wù)器
    //這是一個(gè)main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.創(chuàng  )建套接字: 指定服務(wù)器的端口號
        ServerSocket ss = new ServerSocket(8888);
        //2.等著(zhù)客戶(hù)端發(fā)來(lái)的信息:
        Socket s = ss.accept();//阻塞方法:等待接收客戶(hù)端的數據,什么時(shí)候接收到數據,什么時(shí)候程序繼續向下執行。
        //accept()返回值為一個(gè)Socket,這個(gè)Socket其實(shí)就是客戶(hù)端的Socket
        //接到這個(gè)Socket以后,客戶(hù)端和服務(wù)器才真正產(chǎn)生了連接,才真正可以通信了
        //3.感受到的操作流:
        InputStream is = s.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        //4.讀取客戶(hù)端發(fā)來(lái)的數據:
        String str = dis.readUTF();
        System.out.println("客戶(hù)端發(fā)來(lái)的數據為:"+str);
        //向客戶(hù)端輸出一句話(huà):---》操作流---》輸出流
        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeUTF("你好,我是服務(wù)器端,我接受到你的請求了");
        //5.關(guān)閉流+關(guān)閉網(wǎng)絡(luò )資源:
        dos.close();
        os.close();
        dis.close();
        is.close();
        s.close();
        ss.close();
    }
}

注意:關(guān)閉防火墻

三、對象流傳送

封裝的User類(lèi):

import java.io.Serializable;

public class User implements Serializable {
    private static final long serialVersionUID = 9050691344308365540L;
    private String name;
    private String pwd;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    public User(String name, String pwd) {
        this.name = name;
        this.pwd = pwd;
    }
}

客戶(hù)端:

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

public class TestClient {//客戶(hù)端
    //這是一個(gè)main方法,是程序的入口:
    public static void main(String[] args) throws IOException {
        //1.創(chuàng  )建套接字:指定服務(wù)器的ip和端口號:
        Socket s = new Socket("192.168.199.217",8888);
        //錄入用戶(hù)的賬號和密碼:
        Scanner sc = new Scanner(System.in);
        System.out.println("請錄入您的賬號:");
        String name = sc.next();
        System.out.println("請錄入您的密碼:");
        String pwd = sc.next();
        //將賬號和密碼封裝為一個(gè)User的對象:
        User user = new User(name,pwd);
        //2.對于程序員來(lái)說(shuō),向外發(fā)送數據 感受 --》利用輸出流:
        OutputStream os = s.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(user);
        //接收服務(wù)器端的回話(huà)--》利用輸入流:
        InputStream is = s.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        boolean b = dis.readBoolean();
        if(b){
            System.out.println("恭喜,登錄成功");
        }else{
            System.out.println("對不起,登錄失敗");
        }
        //3.關(guān)閉流  +  關(guān)閉網(wǎng)絡(luò )資源:
        dis.close();
        is.close();
        oos.close();
        os.close();
        s.close();
    }
}

服務(wù)器:

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

public class TestServer {//服務(wù)器
    //這是一個(gè)main方法,是程序的入口:
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //1.創(chuàng  )建套接字: 指定服務(wù)器的端口號
        ServerSocket ss = new ServerSocket(8888);
        //2.等著(zhù)客戶(hù)端發(fā)來(lái)的信息:
        Socket s = ss.accept();//阻塞方法:等待接收客戶(hù)端的數據,什么時(shí)候接收到數據,什么時(shí)候程序繼續向下執行。
        //accept()返回值為一個(gè)Socket,這個(gè)Socket其實(shí)就是客戶(hù)端的Socket
        //接到這個(gè)Socket以后,客戶(hù)端和服務(wù)器才真正產(chǎn)生了連接,才真正可以通信了
        //3.感受到的操作流:
        InputStream is = s.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        //4.讀取客戶(hù)端發(fā)來(lái)的數據:
        User user = (User)(ois.readObject());
        //對對象進(jìn)行驗證:
        boolean flag = false;
        if(user.getName().equals("娜娜")&&user.getPwd().equals("123123")){
            flag = true;
        }
        //向客戶(hù)端輸出結果:---》操作流---》輸出流
        OutputStream os = s.getOutputStream();
        DataOutputStream dos = new DataOutputStream(os);
        dos.writeBoolean(flag);
        //5.關(guān)閉流+關(guān)閉網(wǎng)絡(luò )資源:
        dos.close();
        os.close();
        ois.close();
        is.close();
        s.close();
        ss.close();
    }
}

四、加入完整的處理異常方式

服務(wù)器端:

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

public class TestServer {//服務(wù)器
    //這是一個(gè)main方法,是程序的入口:
    public static void main(String[] args) {
        //1.創(chuàng  )建套接字: 指定服務(wù)器的端口號
        ServerSocket ss = null;
        Socket s = null;
        InputStream is = null;
        ObjectInputStream ois = null;
        OutputStream os = null;
        DataOutputStream dos = null;
        try {
            ss = new ServerSocket(8888);
            //2.等著(zhù)客戶(hù)端發(fā)來(lái)的信息:
            s = ss.accept();//阻塞方法:等待接收客戶(hù)端的數據,什么時(shí)候接收到數據,什么時(shí)候程序繼續向下執行。
            //accept()返回值為一個(gè)Socket,這個(gè)Socket其實(shí)就是客戶(hù)端的Socket
            //接到這個(gè)Socket以后,客戶(hù)端和服務(wù)器才真正產(chǎn)生了連接,才真正可以通信了
            //3.感受到的操作流:
            is = s.getInputStream();
            ois = new ObjectInputStream(is);
            //4.讀取客戶(hù)端發(fā)來(lái)的數據:
            User user = (User)(ois.readObject());
            //對對象進(jìn)行驗證:
            boolean flag = false;
            if(user.getName().equals("娜娜")&&user.getPwd().equals("123123")){
                flag = true;
            }
            //向客戶(hù)端輸出結果:---》操作流---》輸出流
            os = s.getOutputStream();
            dos = new DataOutputStream(os);
            dos.writeBoolean(flag);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            //5.關(guān)閉流+關(guān)閉網(wǎng)絡(luò )資源:
            try {
                if(dos!=null){
                    dos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(os!=null){
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(ois!=null){
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(is!=null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(s!=null){
                    s.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(ss!=null){
                    ss.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
        
    }
}

客戶(hù)端:

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

public class TestClient {//客戶(hù)端
    //這是一個(gè)main方法,是程序的入口:
    public static void main(String[] args){
        //1.創(chuàng  )建套接字:指定服務(wù)器的ip和端口號:
        Socket s = null;
        OutputStream os = null;
        ObjectOutputStream oos = null;
        InputStream is = null;
        DataInputStream dis = null;
        try {
            s = new Socket("192.168.199.217",8888);
            //錄入用戶(hù)的賬號和密碼:
            Scanner sc = new Scanner(System.in);
            System.out.println("請錄入您的賬號:");
            String name = sc.next();
            System.out.println("請錄入您的密碼:");
            String pwd = sc.next();
            //將賬號和密碼封裝為一個(gè)User的對象:
            User user = new User(name,pwd);
            //2.對于程序員來(lái)說(shuō),向外發(fā)送數據 感受 --》利用輸出流:
            os = s.getOutputStream();
            oos = new ObjectOutputStream(os);
            oos.writeObject(user);
            //接收服務(wù)器端的回話(huà)--》利用輸入流:
            is = s.getInputStream();
            dis = new DataInputStream(is);
            boolean b = dis.readBoolean();
            if(b){
                System.out.println("恭喜,登錄成功");
            }else{
                System.out.println("對不起,登錄失敗");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            //3.關(guān)閉流  +  關(guān)閉網(wǎng)絡(luò )資源:
            try {
                if(dis!=null){
                    dis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(is!=null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(oos!=null){
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(os!=null){
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(s!=null){
                    s.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

五、多線(xiàn)程接收用戶(hù)請求

問(wèn)題:

  • 服務(wù)器針對一個(gè)請求服務(wù),之后服務(wù)器就關(guān)閉了(程序自然結束了)

需要解決:

  • 服務(wù)器必須一直在監聽(tīng) ,一直開(kāi)著(zhù),等待客戶(hù)端的請求
  • 在當前代碼中,客戶(hù)端不用動(dòng)了

更改服務(wù)器代碼:

服務(wù)器線(xiàn)程:

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

public class ServerThread extends Thread {//線(xiàn)程:專(zhuān)門(mén)處理客戶(hù)端的請求
    InputStream is = null;
    ObjectInputStream ois = null;
    OutputStream os = null;
    DataOutputStream dos = null;
    Socket s = null;
    public ServerThread(Socket s){
        this.s = s;
    }
    @Override
    public void run() {
        try{
            //2.等著(zhù)客戶(hù)端發(fā)來(lái)的信息:
            is = s.getInputStream();
            ois = new ObjectInputStream(is);
            //4.讀取客戶(hù)端發(fā)來(lái)的數據:
            User user = (User)(ois.readObject());
            //對對象進(jìn)行驗證:
            boolean flag = false;
            if(user.getName().equals("娜娜")&&user.getPwd().equals("123123")){
                flag = true;
            }
            //向客戶(hù)端輸出結果:---》操作流---》輸出流
            os = s.getOutputStream();
            dos = new DataOutputStream(os);
            dos.writeBoolean(flag);
        }catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                if(dos!=null){
                    dos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(os!=null){
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(ois!=null){
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(is!=null){
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

服務(wù)器端:

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

public class TestServer {//服務(wù)器
    //這是一個(gè)main方法,是程序的入口:
    public static void main(String[] args) {
        System.out.println("服務(wù)器啟動(dòng)了");
        //1.創(chuàng  )建套接字: 指定服務(wù)器的端口號
        ServerSocket ss = null;
        Socket s = null;
        int count = 0;//定義一個(gè)計數器,用來(lái)計數  客戶(hù)端的請求
        try {
            ss = new ServerSocket(8888);
            while(true){//加入死循環(huán),服務(wù)器一直監聽(tīng)客戶(hù)端是否發(fā)送數據
                s = ss.accept();//阻塞方法:等待接收客戶(hù)端的數據,什么時(shí)候接收到數據,什么時(shí)候程序繼續向下執行。
                //每次過(guò)來(lái)的客戶(hù)端的請求 靠 線(xiàn)程處理:
                new ServerThread(s).start();
                count++;
                //輸入請求的客戶(hù)端的信息:
                System.out.println("當前是第"+count+"個(gè)用戶(hù)訪(fǎng)問(wèn)我們的服務(wù)器,對應的用戶(hù)是:"+s.getInetAddress());
            }
        } catch (IOException  e) {
            e.printStackTrace();
        }
    }
}

到此這篇關(guān)于Java網(wǎng)絡(luò )編程之基于TCP協(xié)議的文章就介紹到這了,更多相關(guān)Java基于TCP的網(wǎng)絡(luò )編程內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自互聯(lián)網(wǎng)轉載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權請聯(lián)系QQ:712375056 進(jìn)行舉報,并提供相關(guān)證據,一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容。

ja
亚洲AV无码成H人动漫在线观看| 曰韩欧美亚洲美日更新在线| 国产AV一区二区精品久久凹凸| 无码GOGO大胆啪啪艺术| 亚洲国产一成人久久精品| 亚洲爆乳精品无码一区二区|