国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Java分別用BIO、NIO實(shí)現(xiàn)簡(jiǎn)單的客戶端服務(wù)器通信

這篇具有很好參考價(jià)值的文章主要介紹了Java分別用BIO、NIO實(shí)現(xiàn)簡(jiǎn)單的客戶端服務(wù)器通信。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

前言:
Java I/O模型發(fā)展以及Netty網(wǎng)絡(luò)模型的設(shè)計(jì)思想

BIO

Java BIO是Java平臺(tái)上的BIO(Blocking I/O)模型,是Java中用于實(shí)現(xiàn)同步阻塞網(wǎng)絡(luò)編程的一種方式。 在Java中,使用BIO模型需要通過(guò)Socket和ServerSocket類來(lái)完成網(wǎng)絡(luò)連接和數(shù)據(jù)傳輸,但是由于BIO是同步阻塞的,所以會(huì)導(dǎo)致線程阻塞和資源浪費(fèi)的問(wèn)題。
因此,在高并發(fā)的網(wǎng)絡(luò)編程場(chǎng)景中,通常會(huì)選擇使用NIO(Non-blocking I/O)模型或者Netty等框架來(lái)實(shí)現(xiàn)。

Java分別用BIO、NIO實(shí)現(xiàn)簡(jiǎn)單的客戶端服務(wù)器通信,手撕代碼,網(wǎng)絡(luò)編程,nio,服務(wù)器,rpc
服務(wù)端類代碼

package com.yu.io.bio;

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

public class BIOServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8081);
        while (true) {
            System.out.println("wait connect...");
            Socket clientSocket = serverSocket.accept();
            System.out.println(clientSocket.getPort() + "connected");
            System.out.println("start read...");

            byte[] readArr = new byte[1024];
            int read = clientSocket.getInputStream().read(readArr);
            if (read != -1) {
                System.out.println("read info : " + new String(readArr,0,read));
            }
            System.out.println("end read...");
            byte[] resBytes = "server response".getBytes();
            clientSocket.getOutputStream().write(resBytes);
            System.out.println("response info : " + new String(readArr,0,read));
            clientSocket.getOutputStream().flush();
        }
    }
}

客戶端類代碼

package com.yu.io.bio;

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

public class BIOClient {
    public static void main(String[] args) throws IOException {
        Socket clientSocket = new Socket("127.0.0.1",8081);
        byte[] resBytes = "client response".getBytes();
        clientSocket.getOutputStream().write(resBytes);
        System.out.println("response info : " + new String(resBytes));
        clientSocket.getOutputStream().flush();
        byte[] readArr = new byte[1024];
        int read = clientSocket.getInputStream().read(readArr);
        if (read != -1) {
            System.out.println("read info : " + new String(readArr,0,read));
        }
    }
}

NIO

Java NIO 能夠支持非阻塞網(wǎng)絡(luò)編程,可以理解為new io 或者no blok io 我更喜歡稱之為new io,因?yàn)樗粌H僅實(shí)現(xiàn)了非阻塞的網(wǎng)絡(luò)編程方式,同時(shí)也封裝了常用的網(wǎng)絡(luò)編程api,更重要的是引入了多路復(fù)用器Selector的概念

Java分別用BIO、NIO實(shí)現(xiàn)簡(jiǎn)單的客戶端服務(wù)器通信,手撕代碼,網(wǎng)絡(luò)編程,nio,服務(wù)器,rpc

下面的代碼只是展示NIO非阻塞的實(shí)現(xiàn),并沒(méi)有展示NIO的真正用法

NIO演示(無(wú)Selector)

服務(wù)端類代碼

package com.yu.io.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.ArrayList;
import java.util.List;

public class NIOServer {

    public static List<SocketChannel> socketChannelList = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(8081));
        serverSocketChannel.configureBlocking(false);
        System.out.println("server start...");
        while (true) {
            SocketChannel socketChannel = serverSocketChannel.accept();
            if (socketChannel != null) {
                System.out.println(socketChannel.socket().getPort() + " connected");
                socketChannel.configureBlocking(false);
                socketChannelList.add(socketChannel);
            }
            List<SocketChannel> rmChannelList = new ArrayList<>();
            for (SocketChannel channel : socketChannelList) {
                try {
                    doChannel(rmChannelList, channel);
                } catch (IOException ioException) {
                    //有客戶端斷開連接
                    System.out.println(channel.socket().getPort() + " disconnected");
                    channel.close();
                    rmChannelList.add(channel);
                }
            }
            socketChannelList.removeAll(rmChannelList);
        }
    }

    private static void doChannel(List<SocketChannel> rmChannelList, SocketChannel channel) throws IOException {
        ByteBuffer readByteBuffer = ByteBuffer.allocate(2048);
        int read = channel.read(readByteBuffer);
        String readStr = new String(readByteBuffer.array());
        if (read > 0) {
            System.out.println(channel.socket().getPort() + " : " + readStr);
            if (readStr.contains("hello")) {
                ByteBuffer sendByteBuffer = ByteBuffer.wrap("hello! I am robot.".getBytes());
                channel.write(sendByteBuffer);
                System.out.println("me : " + new String(sendByteBuffer.array()));
            }
            if (readStr.contains("old")) {
                ByteBuffer sendByteBuffer = ByteBuffer.wrap("I am 1 years old.".getBytes());
                channel.write(sendByteBuffer);
                System.out.println("me : " + new String(sendByteBuffer.array()));
            }
            if (readStr.contains("bey")) {
                ByteBuffer sendByteBuffer = ByteBuffer.wrap("see you.".getBytes());
                channel.write(sendByteBuffer);
                System.out.println("me : " + new String(sendByteBuffer.array()));
            }
        }
        if (read == -1) {
            rmChannelList.add(channel);
        }
    }
}

客戶端類代碼

package com.yu.io.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class NIOClient {
    public static void main(String[] args) throws IOException {
        SocketChannel clientSocketChannel = SocketChannel.open();
        clientSocketChannel.connect(new InetSocketAddress("127.0.0.1",8081));
        clientSocketChannel.configureBlocking(false);
        String sendStr = "hello! I am client " + clientSocketChannel.socket().getPort() + ".";
        ByteBuffer sendByteBuffer = ByteBuffer.wrap(sendStr.getBytes());
        clientSocketChannel.write(sendByteBuffer);
        System.out.println("me : " + new String(sendByteBuffer.array()));
        int msgSize = 0;
        while (msgSize < 10) {
            ByteBuffer readByteBuffer = ByteBuffer.allocate(1024);
            int read = clientSocketChannel.read(readByteBuffer);
            String readStr = new String(readByteBuffer.array());
            if (read > 0) {
                System.out.println("robot : " + readStr);
                msgSize ++;
                ByteBuffer resByteBuffer = null;
                if (readStr.contains("hello")) {
                    resByteBuffer = ByteBuffer.wrap("how old are you?.".getBytes());
                    clientSocketChannel.write(resByteBuffer);
                    System.out.println("me : " + new String(resByteBuffer.array()));
                    resByteBuffer.clear();
                }
                if (readStr.contains("old")) {
                    resByteBuffer = ByteBuffer.wrap("en, place say hello!".getBytes());
                    clientSocketChannel.write(resByteBuffer);
                    System.out.println("me : " + new String(resByteBuffer.array()));
                    resByteBuffer.clear();
                }
            }
        }
        ByteBuffer resByteBuffer = ByteBuffer.wrap("bey bey!".getBytes());
        clientSocketChannel.write(resByteBuffer);
        System.out.println("me : " + new String(resByteBuffer.array()));
        resByteBuffer.clear();
        clientSocketChannel.close();
    }
}

NIO演示(Selector)

無(wú)Selector的NIO演示中,顯然會(huì)出現(xiàn)空轉(zhuǎn)的情況,以及無(wú)效連接的處理問(wèn)題,這些問(wèn)題都會(huì)影響性能。
NIO提供Selector多路復(fù)用器,優(yōu)化上述問(wèn)題
以下demo實(shí)現(xiàn)服務(wù)器與客戶端通信,相互發(fā)消息。并由服務(wù)器轉(zhuǎn)發(fā)給其他客戶端(廣播功能)

服務(wù)端類代碼

server main類

import java.io.IOException;

public class NIOServerMain {
    public static void main(String[] args) throws IOException {
        NIOSelectorServer server = new NIOSelectorServer();
        server.start();
    }
}

server run類

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

public class NIOSelectorServer {

    public void start() throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.socket().bind(new InetSocketAddress(8081));
        serverSocketChannel.configureBlocking(false);
        //注冊(cè)到selector多路復(fù)用器中
        Selector selector = Selector.open();
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);
        System.out.println("server start...");
        run(selector);
    }

    /**
     * 遍歷多路復(fù)用器的事件,處理事件
     */
    private void run(Selector selector) throws IOException {
        while (true) {
            //阻塞等待事件
            selector.select();

            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while (iterator.hasNext()) {
                SelectionKey selectedKey = iterator.next();
                try {
                    if (selectedKey.isAcceptable()) {
                        doAccept(selector, selectedKey);
                    }
                    if (selectedKey.isReadable()) {
                        doReadChannel(selectedKey, selector);
                    }
                } catch (IOException ioException) {
                    //有客戶端斷開連接
                    disConnect(selectedKey, "exception");
                }
                iterator.remove();
            }
        }
    }

    /**
     * 處理連接事件
     */
    private void doAccept(Selector selector, SelectionKey selectedKey) throws IOException {
        ServerSocketChannel serverChannel = (ServerSocketChannel)selectedKey.channel();
        SocketChannel socketChannel = serverChannel.accept();
        socketChannel.configureBlocking(false);
        socketChannel.register(selector, SelectionKey.OP_READ);
        System.out.println(socketChannel.socket().getPort() + " connected");
    }

    /**
     * 處理接收信息事件
     */
    private void doReadChannel(SelectionKey selectedKey, Selector selector) throws IOException {
        SocketChannel channel = (SocketChannel)selectedKey.channel();
        ByteBuffer readByteBuffer = ByteBuffer.allocate(2048);
        int read = channel.read(readByteBuffer);
        String readStr = "";
        readByteBuffer.flip();
        readStr += StandardCharsets.UTF_8.decode(readByteBuffer);
        if (read > 0) {
            System.out.println(channel.socket().getPort() + " : " + readStr.length() + " - "+ readStr);
            //轉(zhuǎn)發(fā)消息(其他客戶端)
            broadcast(selectedKey, selector, readStr);
            if (readStr.contains("hello")) {
                sendMsg(channel, "hello! I am robot.");
            }
            if (readStr.contains("old")) {
                sendMsg(channel, "I am 1 years old.");
            }
            if (readStr.contains("bye")) {
                sendMsg(channel, "see you.");
            }
        }
        if (read == -1) {
            //有客戶端斷開連接
            disConnect(selectedKey, "read = -1");
        }
    }

    /**
     * 連接異常的處理
     */
    private void disConnect(SelectionKey selectedKey, String type) throws IOException {
        SocketChannel channel = (SocketChannel)selectedKey.channel();
        System.out.println(channel.socket().getPort() + " disconnected. " + type);
        selectedKey.cancel();
        channel.close();
    }

    /**
     * 發(fā)送消息
     */
    private void sendMsg(SocketChannel channel, String s) throws IOException {
        ByteBuffer sendByteBuffer = ByteBuffer.wrap(s.getBytes());
        channel.write(sendByteBuffer);
        System.out.println("me : " + new String(sendByteBuffer.array()));
    }

    /**
     * 廣播
     * 轉(zhuǎn)發(fā)消息(給其他客戶端)
     */
    private void broadcast(SelectionKey  fromSelectedKey, Selector selector, String readStr) throws IOException {
        Iterator<SelectionKey> selectionKeyIterator = selector.keys().iterator();
        while (selectionKeyIterator.hasNext()) {
            SelectionKey otherKey = selectionKeyIterator.next();
            if (otherKey == fromSelectedKey) {
                continue;
            }
            if (!(otherKey.channel() instanceof SocketChannel)) {
                continue;
            }
            SocketChannel otherChannel = (SocketChannel)otherKey.channel();
            sendMsg(otherChannel, "(轉(zhuǎn)發(fā)自 "+ ((SocketChannel)fromSelectedKey.channel()).socket().getPort() + ")" + readStr);
        }
    }
}

客戶端代碼

一共構(gòu)造了兩個(gè)客戶端(消息客戶端和looker客戶端), looker客戶端優(yōu)先啟動(dòng),隨后啟動(dòng)消息客戶端,消息客戶端與服務(wù)器的通信會(huì)被轉(zhuǎn)發(fā)給looker客戶端

look client main類

import java.io.IOException;

public class NIOClientLookMain {
    public static void main(String[] args) throws IOException, InterruptedException {
        NIOSelectorClient client = new NIOSelectorLookClient();
        client.start(8081, 100);
    }
}

msg client main類

import java.io.IOException;

public class NIOClientMain {
    public static void main(String[] args) throws IOException, InterruptedException {
        NIOSelectorClient lookClient = new NIOSelectorClient();
        lookClient.start(8081, 10);
    }
}

client run類

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

public class NIOSelectorClient {
    protected int port;
    protected int size;
    public void start(int port, int size) throws IOException, InterruptedException {
        this.port = port;
        this.size = size;
        SocketChannel clientSocketChannel = SocketChannel.open();
        clientSocketChannel.connect(new InetSocketAddress("127.0.0.1",port));
        clientSocketChannel.configureBlocking(false);
        Selector selector = Selector.open();
        clientSocketChannel.register(selector, SelectionKey.OP_READ);
        //發(fā)送開始數(shù)據(jù)
        sendMsg(clientSocketChannel, "hello! I am client " + clientSocketChannel.socket().getLocalPort() + ".");
        run(selector);
        sendMsg(clientSocketChannel, "bye bye!");
        clientSocketChannel.close();
    }

    /**
     * 遍歷多路復(fù)用器的事件,處理事件
     */
    protected void run(Selector selector) throws IOException, InterruptedException {
        int msgSize = 0;
        while (msgSize < size) {
            int length=selector.select();
            if(length ==0){
                continue;
            }
            Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
            while(iterator.hasNext()){
                SelectionKey selectionKey = iterator.next();
                if(selectionKey.isReadable()){
                    boolean readChannel = false;
                    try {
                        readChannel = doReadChannel(selectionKey, selector);
                    } catch (IOException e) {
                        selectionKey.cancel();
                        System.out.println("robot disconnect, restart connect...");
                        while (true){
                            try {
                                reConnect();
                                return;
                            } catch (IOException ioException) {
                                System.out.println("restart connecting(5s) ");
                                //ioException.printStackTrace();
                                Thread.sleep(5000);
                            }
                        }
                    }
                    if (readChannel) {
                        msgSize ++;
                    }
                }
                iterator.remove();
            }
        }
    }

    protected boolean doReadChannel(SelectionKey selectedKey, Selector selector) throws IOException {
        SocketChannel channel = (SocketChannel)selectedKey.channel();
        ByteBuffer readByteBuffer = ByteBuffer.allocate(2048);
        int read = channel.read(readByteBuffer);
        String readStr = "";
        readByteBuffer.flip();
        readStr += StandardCharsets.UTF_8.decode(readByteBuffer);
        if (read > 0) {
            System.out.println("robot : " + readStr);
            if (readStr.contains("hello")) {
                sendMsg(channel, "how old are you?.");
            }
            if (readStr.contains("old")) {
                sendMsg(channel, "en, place say hello!");
            }
            return true;
        }
        return false;
    }

    protected void sendMsg(SocketChannel channel, String sendStr) throws IOException {
        ByteBuffer sendByteBuffer = ByteBuffer.wrap(sendStr.getBytes());
        channel.write(sendByteBuffer);
        System.out.println("me : " + new String(sendByteBuffer.array()));
    }

    protected void reConnect() throws IOException, InterruptedException {
        NIOSelectorClient client = new NIOSelectorClient();
        client.start(port, size);
    }
}

look client run類文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-697245.html

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;

public class NIOSelectorLookClient extends NIOSelectorClient{
    @Override
    public void start(int port, int size) throws IOException, InterruptedException {
        this.port = port;
        this.size = size;
        SocketChannel clientSocketChannel = SocketChannel.open();
        clientSocketChannel.connect(new InetSocketAddress("127.0.0.1",port));
        clientSocketChannel.configureBlocking(false);
        Selector selector = Selector.open();
        clientSocketChannel.register(selector, SelectionKey.OP_READ);
        //發(fā)送開始數(shù)據(jù)
        sendMsg(clientSocketChannel, "I am looker. " + clientSocketChannel.socket().getLocalPort() + ".");
        run(selector);
    }

    @Override
    protected boolean doReadChannel(SelectionKey selectedKey, Selector selector) throws IOException {
        SocketChannel channel = (SocketChannel)selectedKey.channel();
        ByteBuffer readByteBuffer = ByteBuffer.allocate(2048);
        int read = channel.read(readByteBuffer);
        String readStr = "";
        readByteBuffer.flip();
        readStr += StandardCharsets.UTF_8.decode(readByteBuffer);
        if (read > 0) {
            System.out.println("robot : " + readStr.length() + " - "+ readStr);
            if (readStr.contains("bye")) {
                sendMsg(channel, "bye.");
            }
            return true;
        }
        return false;
    }

    @Override
    protected void reConnect() throws IOException, InterruptedException {
        NIOSelectorClient client = new NIOSelectorLookClient();
        client.start(port, size);
    }
}

到了這里,關(guān)于Java分別用BIO、NIO實(shí)現(xiàn)簡(jiǎn)單的客戶端服務(wù)器通信的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • java代碼構(gòu)建簡(jiǎn)單http服務(wù)器和客戶端

    java代碼構(gòu)建簡(jiǎn)單http服務(wù)器和客戶端

    初識(shí)http a、超文本傳輸 、應(yīng)用層的面向?qū)ο蟮膮f(xié)議,概念介紹網(wǎng)上資源一大堆,關(guān)鍵是基于TCP/IP通信協(xié)議來(lái)傳遞數(shù)據(jù)。 b、一開始接觸web項(xiàng)目,都是先接觸的servlet,tomcat服務(wù)器默認(rèn)實(shí)現(xiàn)的一套http規(guī)范,提供了基礎(chǔ)服務(wù)和組件環(huán)境,直接拿到請(qǐng)求、構(gòu)建正文、響應(yīng)客戶端 然而

    2024年02月10日
    瀏覽(28)
  • c語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的tcp客戶端

    功能:實(shí)現(xiàn)一個(gè)簡(jiǎn)單的tcp客戶端,連接本地端口8888的tcp服務(wù)端,并發(fā)送一條報(bào)文。 ?

    2024年02月14日
    瀏覽(22)
  • 因項(xiàng)目只做socket客戶端,不想用workerman或者swoole框架,簡(jiǎn)單實(shí)現(xiàn)ws PHP客戶端

    docs/Client.md · master · mirrors / Textalk / websocket-php · GitCode

    2024年02月13日
    瀏覽(20)
  • 計(jì)算機(jī)網(wǎng)絡(luò) 簡(jiǎn)單FTP客戶端軟件的實(shí)現(xiàn)

    計(jì)算機(jī)網(wǎng)絡(luò) 簡(jiǎn)單FTP客戶端軟件的實(shí)現(xiàn)

    文件傳送協(xié)議FTP(File Transfer Protocol)是TCP/IP體系的一個(gè)重要協(xié)議,它采用Internet標(biāo)準(zhǔn)文件傳輸協(xié)議FTP的用戶界面,向用戶提供了一組用來(lái)管理計(jì)算機(jī)之間文件傳輸?shù)膽?yīng)用程序。FTP是基于客戶—服務(wù)器(C/S)模型而設(shè)計(jì)的,在客戶端和FTP建立兩個(gè)TCP連接。 FTP 的獨(dú)特的優(yōu)勢(shì)同時(shí)

    2024年02月12日
    瀏覽(20)
  • 10. Linux下實(shí)現(xiàn)簡(jiǎn)單的http客戶端請(qǐng)求

    本文Linux下實(shí)現(xiàn)簡(jiǎn)單的http客戶端請(qǐng)求 HTTP(超文本傳輸協(xié)議)是一種用于在網(wǎng)絡(luò)上進(jìn)行數(shù)據(jù)通信的協(xié)議。HTTP 協(xié)議定義了客戶端和服務(wù)器之間如何交換信息,包括請(qǐng)求和響應(yīng)格式、使用的方法、狀態(tài)碼等。 在 HTTP 協(xié)議中,資源(Resource)指的是由 URL (統(tǒng)一資源定位符)所標(biāo)識(shí)

    2024年02月16日
    瀏覽(24)
  • 基于C# Socket實(shí)現(xiàn)的簡(jiǎn)單的Redis客戶端

    基于C# Socket實(shí)現(xiàn)的簡(jiǎn)單的Redis客戶端

    ???? Redis 是一款強(qiáng)大的高性能鍵值存儲(chǔ)數(shù)據(jù)庫(kù),也是目前 NOSQL 中 最流行 比較流行的一款數(shù)據(jù)庫(kù),它在廣泛的應(yīng)用場(chǎng)景中扮演著至關(guān)重要的角色,包括但不限于緩存、消息隊(duì)列、會(huì)話存儲(chǔ)等。在本文中,我們將介紹如何基于 C# Socket 來(lái)實(shí)現(xiàn)一個(gè)簡(jiǎn)單的Redis客戶端類 RedisClien

    2024年02月05日
    瀏覽(19)
  • 使用socket.io簡(jiǎn)單實(shí)現(xiàn)多客戶端可編輯表格

    之前看了B站小野森森老師的可編輯表格的視頻深受啟發(fā),今天使用React簡(jiǎn)單實(shí)現(xiàn)一下。 當(dāng)處于編輯狀態(tài)的時(shí)候,自己和其他人可以看到; 編輯內(nèi)容后,自己及其他人可以同步看到修改后的內(nèi)容; 后端服務(wù),使用socket.io起一個(gè)后端服務(wù),用于監(jiān)聽連接和發(fā)送數(shù)據(jù); 前端準(zhǔn)備:

    2024年02月06日
    瀏覽(20)
  • 【網(wǎng)絡(luò)原理】使用Java基于TCP搭建簡(jiǎn)單客戶端與服務(wù)器通信

    【網(wǎng)絡(luò)原理】使用Java基于TCP搭建簡(jiǎn)單客戶端與服務(wù)器通信

    TCP服務(wù)器與客戶端的搭建需要借助以下API ServerSocket 是創(chuàng)建TCP服務(wù)端Socket的API。 ServerSocket 構(gòu)造方法 : 方法簽名 方法說(shuō)明 ServerSocket(int port) 創(chuàng)建一個(gè)服務(wù)端流套接字Socket,并綁定到指定端口 ServerSocket 方法: 方法簽名 方法說(shuō)明 Socket accept() 開始監(jiān)聽指定端口(創(chuàng)建時(shí)綁定的端

    2024年03月12日
    瀏覽(34)
  • C#實(shí)現(xiàn)簡(jiǎn)單TCP服務(wù)器和客戶端網(wǎng)絡(luò)編程

    C#實(shí)現(xiàn)簡(jiǎn)單TCP服務(wù)器和客戶端網(wǎng)絡(luò)編程

    在C#中進(jìn)行網(wǎng)絡(luò)編程涉及許多類和命名空間,用于創(chuàng)建和管理網(wǎng)絡(luò)連接、傳輸數(shù)據(jù)等。下面是一些主要涉及的類和命名空間: System.Net 命名空間: 這個(gè)命名空間提供了大部分網(wǎng)絡(luò)編程所需的類,包括: IPAddress :用于表示IP地址。 IPEndPoint :表示IP地址和端口號(hào)的組合。 Socke

    2024年02月11日
    瀏覽(37)
  • C++實(shí)現(xiàn)一個(gè)簡(jiǎn)單的客戶端與服務(wù)端的通信(筆記附代碼)

    C++實(shí)現(xiàn)一個(gè)簡(jiǎn)單的客戶端與服務(wù)端的通信(筆記附代碼)

    目錄 前言 一、Socket的客戶端與服務(wù)端的通訊原理 二、各接口介紹 1.WSAStartup:異步啟動(dòng)套接字命令 2.Socket創(chuàng)建套接字 3.bind:綁定套接字 4.listen:監(jiān)聽 5.accept:接受連接請(qǐng)求 6. connet:發(fā)送連接請(qǐng)求 ?7.send:發(fā)送數(shù)據(jù) 8.recv:接收數(shù)據(jù)函數(shù)? 9.closesocket,?WSACleanup:釋放socket 三、代碼塊的

    2024年02月06日
    瀏覽(18)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包