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

UDP套接字的通信(實(shí)現(xiàn)英漢互譯/程序替換/多線程聊天室/Windows與Linux通信)

這篇具有很好參考價(jià)值的文章主要介紹了UDP套接字的通信(實(shí)現(xiàn)英漢互譯/程序替換/多線程聊天室/Windows與Linux通信)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

實(shí)現(xiàn)英漢互譯

思路

我們?cè)诳蛻舳税l(fā)英文,服務(wù)端做翻譯工作,讓翻譯好的中文再次發(fā)給我們的客戶端,然后打印出來(lái)。

服務(wù)端代碼

翻譯的操作

創(chuàng)建一個(gè)txt文件里面包含英漢互譯的數(shù)據(jù)

dict.txt

banana:香蕉
apple:蘋(píng)果
pig:豬
beef:牛肉
hello:你好
對(duì)txt中的數(shù)據(jù)進(jìn)行操作
分割函數(shù)

將英漢通過(guò)冒號(hào)分開(kāi)。

// 分割函數(shù)
static bool cutString(const string &target, string *s1, string *s2, const string &sep)
{
    // apple:蘋(píng)果
    auto pos = target.find(sep);

    if (pos == string::npos)
    {
        return false;
    }

    *s1 = target.substr(0, pos);
    *s2 = target.substr(pos + sep.size());

    return true;
}
將文件數(shù)據(jù)插入map里面
// 按行將文件里面的數(shù)據(jù)給插入到map里面
static void initDict()
{
    dict.clear();
    ifstream in(dictTxt, std::ios::binary);
    if (!in.is_open())
    {
        std::cerr << "open file " << dictTxt << " error" << endl;
        exit(OPEN_ERR);
    }

    string line;
    std::string key, value;

    while (getline(in, line))
    {
        // cout << line << endl;
        if (cutString(line, &key, &value, ":"))
        {
            dict.insert(make_pair(key, value));
        }
    }

    in.close();

    cout << "load dict success" << endl;
}
重新加載文件

通過(guò)捕捉2號(hào)(ctrl c)信號(hào)來(lái)進(jìn)行重新加載文件。

void reload(int signo)
{
    (void)signo;
    initDict();
}

// ./udpClient server_ip server_port
int main(int argc, char *argv[])
{
   
    signal(2, reload); // 通過(guò)發(fā)2號(hào)信號(hào)來(lái)使dict.txt中的數(shù)據(jù)進(jìn)行更新

    
}

網(wǎng)絡(luò)通信的操作

將翻譯后的數(shù)據(jù)發(fā)送給客戶端
void handlerMessage(int sockfd, string message, uint16_t clientport, string clientip)
{
    // 就可以對(duì)message進(jìn)行特定的業(yè)務(wù)處理,而不關(guān)心message怎么來(lái)的 --- server通信和業(yè)務(wù)邏輯解耦!
    // 嬰兒版的業(yè)務(wù)邏輯
    string response_message;
    auto iter = dict.find(message);
    if (iter == dict.end())
        response_message = "unknown";
    else
        response_message = iter->second;

    // 開(kāi)始返回
    struct sockaddr_in client;
    bzero(&client, sizeof(client));

    client.sin_family = AF_INET;
    client.sin_port = htons(clientport);
    client.sin_addr.s_addr = inet_addr(clientip.c_str());

    // 在服務(wù)端收到客戶端數(shù)據(jù)的時(shí)候我們獲取到了客戶端的端口號(hào)和ip,因此回調(diào)到了這個(gè)函數(shù)中,
    // 此時(shí)我們就可以使用客戶端的端口號(hào)和ip來(lái)給客戶端發(fā)送翻譯后的信息
    sendto(sockfd, response_message.c_str(), response_message.size(), 0, (struct sockaddr *)&client, sizeof(client));
}

客戶端代碼

創(chuàng)建socket

void initClient()
{
    // 1. 創(chuàng)建socket
    _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (_sockfd == -1)
    {
        cerr << "socket error: " << errno << " : " << strerror(errno) << endl;
        exit(2);
    }
    cout << "socket success: " << _sockfd << endl;

    // 2. client要不要bind[不需要的]  , client要不要顯示的bind,需不需要程序員自己bind? 不需要
    // 寫(xiě)服務(wù)器的一家公司,寫(xiě)客戶端是無(wú)數(shù)家公司 -- 因此讓OS自動(dòng)形成端口進(jìn)行bind! -- OS在什么時(shí)候,如何bind
}

數(shù)據(jù)處理

將用戶輸入的數(shù)據(jù)發(fā)送給服務(wù)端,并且接受服務(wù)端翻譯后的數(shù)據(jù)并進(jìn)行打印。

void run()
{

    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(_serverip.c_str());
    server.sin_port = htons(_serverport);

    string message;
    char buffer[1024];
    while (!_quit)
    {
        // fprintf(stderr, "Please Enter# ");
        // fflush(stderr);

        // fgets(buffer, sizeof(buffer), stdin);

        cout << "Please Enter# ";
        cin >> message;

        // buffer[strlen(buffer) - 1] = 0;

        // message = buffer;

        sendto(_sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&server, sizeof(server));

        char recv_buffer[1024];
        // 接受翻譯后的信息
        struct sockaddr_in peer;
        socklen_t len = sizeof(peer);
        //
        ssize_t s = recvfrom(_sockfd, recv_buffer, sizeof(recv_buffer) - 1, 0, (struct sockaddr *)&peer, &len);
        if (s > 0)
        {
            // 讀取數(shù)據(jù)
            buffer[s] = 0;
        }
        cout << "服務(wù)器翻譯成# " << recv_buffer << endl;
        // recv_buffer[0] = 0;
        memset(recv_buffer, 0, sizeof(buffer)); // 清空緩沖區(qū)
    }
}

成果展示

UDP套接字的通信(實(shí)現(xiàn)英漢互譯/程序替換/多線程聊天室/Windows與Linux通信),Linux網(wǎng)絡(luò)編程,udp,網(wǎng)絡(luò)協(xié)議,網(wǎng)絡(luò)

程序替換

思路

主要使用popen接口同時(shí)實(shí)現(xiàn)管道+創(chuàng)建子進(jìn)程+程序替換的功能。將我們客戶端輸入的命令信息發(fā)送給服務(wù)端。服務(wù)端將該命令經(jīng)過(guò)popen接口讓它執(zhí)行命令運(yùn)行出來(lái)的結(jié)果放在一個(gè)文件中,然后在將文件中的內(nèi)容讀取出來(lái)發(fā)送給客戶端。

popen接口

#include <stdio.h>

FILE *popen(const char *command,const char *type);  // 相當(dāng)于pipe+fork+exec* 

int pclose(FILE *stream);

參數(shù)

const char *command

未來(lái)要執(zhí)行的命令字符串:?

ls -a -l 等 ?可以將這些命令執(zhí)行的結(jié)果返回到一個(gè)文件當(dāng)中

const char *type

對(duì)文件的操作方式"r" "w"?"a" 等

返回值

返回 nullptr 說(shuō)明執(zhí)行失敗了

服務(wù)端代碼

void execCommand(int sockfd, string cmd, uint16_t clientport, string clientip)
{
    // 1. com解析,ls -a -l
    // 2. 如果必要,可能需要fork,exec*

    if (cmd.find("rm") != string::npos || cmd.find("mv") != string::npos || cmd.find("remdir") != string::npos)
    {
        cerr << clientip << " : " << clientport << " 正在做一個(gè)非法的操作: " << cmd << endl;
        return;
    }

    string response;
    FILE *fp = popen(cmd.c_str(), "r");

    if (fp == nullptr)
        response = cmd + " exec failed";

    char line[1024];
    // 按行讀取
    while (fgets(line, sizeof(line), fp))
    {
        response += line;
    }

    pclose(fp);

    // 開(kāi)始返回
    struct sockaddr_in client;
    bzero(&client, sizeof(client));

    client.sin_family = AF_INET;
    client.sin_port = htons(clientport);
    client.sin_addr.s_addr = inet_addr(clientip.c_str());

    // 在服務(wù)端收到客戶端數(shù)據(jù)的時(shí)候我們獲取到了客戶端的端口號(hào)和ip,因此回調(diào)到了這個(gè)函數(shù)中,
    // 此時(shí)我們就可以使用客戶端的端口號(hào)和ip來(lái)給客戶端發(fā)送翻譯后的信息
    sendto(sockfd, response.c_str(), response.size(), 0, (struct sockaddr *)&client, sizeof(client));
}

成果展示?

UDP套接字的通信(實(shí)現(xiàn)英漢互譯/程序替換/多線程聊天室/Windows與Linux通信),Linux網(wǎng)絡(luò)編程,udp,網(wǎng)絡(luò)協(xié)議,網(wǎng)絡(luò)

多線程聊天室

思路

通過(guò)在客戶端的角度創(chuàng)建多線程,一個(gè)線程進(jìn)行發(fā)消息,一個(gè)線程進(jìn)行讀消息。這樣我們就能讓多個(gè)線程(用戶)一起聊天。

讓用戶的id(ip + "-" + port)作為key值,User作為value值。來(lái)形成一個(gè)個(gè)unordered_map類(lèi)型的容器。如果用戶上線了就將該對(duì)象添加到unordered_map容器里面,下線就從unordered_map容器刪除。

通過(guò)輸入"online"來(lái)將該用戶添加到unordered_map容器里面,意味上線。

通過(guò)輸入"offline"來(lái)將該用戶從unordered_map容器里面刪除,意味下線。

我們服務(wù)端收到客戶端發(fā)來(lái)的數(shù)據(jù)的時(shí)候通過(guò)isOnline函數(shù)來(lái)判斷該用戶是否在unordered_map容器里面,如果在就將該信息發(fā)送給客戶端并且客戶端接受后打印出來(lái),如果不在直接打印"未上線"。

用戶信息代碼代碼 onlineUser.hpp

#pragma once

#include <iostream>
#include <string>
#include <unordered_map>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

using namespace std;

class User
{
public:
    User(const string &ip, const uint16_t &port)
        : _ip(ip), _port(port)
    {
    }

    ~User()
    {
    }

    string ip()
    {
        return _ip;
    }
    uint16_t port()
    {
        return _port;
    }

private:
    string _ip;
    uint16_t _port;
};

class OnlineUser
{
public:
    OnlineUser() {}
    ~OnlineUser() {}
    void addUser(const string &ip, const uint16_t &port)
    {
        string id = ip + "-" + to_string(port);
        users.insert(make_pair(id, User(ip, port)));
    }
    void delUser(const string &ip, const uint16_t &port)
    {
        string id = ip + "-" + to_string(port);
        users.erase(id);
    }

    bool isOnline(const string &ip, const uint16_t &port)
    {
        string id = ip + "-" + to_string(port);
        return users.find(id) == users.end() ? false : true;
    }

    void broadcastMessage(int sockfd, const string &ip, const uint16_t &port, const string &message)
    {
        for (auto &user : users)
        {
            // 開(kāi)始返回
            struct sockaddr_in client;
            bzero(&client, sizeof(client));

            client.sin_family = AF_INET;
            client.sin_port = htons(user.second.port());
            client.sin_addr.s_addr = inet_addr(user.second.ip().c_str());
            // 將用戶id也加入
            string s = ip + "-" + to_string(port) + "# ";
            s += message;
            sendto(sockfd, s.c_str(), s.size(), 0, (struct sockaddr *)&client, sizeof(client));
        }
    }

private:
    unordered_map<string, User> users;
};

服務(wù)端代碼

// demo 3
void routeMessage(int sockfd, string message, uint16_t clientport, string clientip)
{
    // 判斷是否上線
    // 上線就將該用戶添加到onlineuser
    if (message == "online")
        onlineuser.addUser(clientip, clientport);
    // 下線就將該用戶在onlineuser中刪除
    if ((message == "offline"))
        onlineuser.delUser(clientip, clientport);

    // 如果以下if為真 那么說(shuō)明用戶已經(jīng)上線,因此需要將用戶發(fā)的信息進(jìn)行路由
    if (onlineuser.isOnline(clientip, clientport))
    {
        // 消息的路由
        onlineuser.broadcastMessage(sockfd, clientip, clientport, message);
    }
    else
    {
        // 開(kāi)始返回
        struct sockaddr_in client;
        bzero(&client, sizeof(client));

        client.sin_family = AF_INET;
        client.sin_port = htons(clientport);
        client.sin_addr.s_addr = inet_addr(clientip.c_str());

        string response_message = "你還沒(méi)有上線,請(qǐng)先上線,運(yùn)行: online";

        sendto(sockfd, response_message.c_str(), response_message.size(), 0, (struct sockaddr *)&client, sizeof(client));
    }

客戶端代碼(多線程)

static void *readMessage(void *args)
{
    int sockfd = *(static_cast<int *>(args));
    pthread_detach(pthread_self());

    while (true)
    {
        char buffer_recv[1024];
        struct sockaddr_in peer;
        socklen_t len = sizeof(peer);

        ssize_t s = recvfrom(sockfd, buffer_recv, sizeof(buffer_recv) - 1, 0, (struct sockaddr *)&peer, &len);

        if (s >= 0)
            buffer_recv[s] = 0;

        cout << buffer_recv << endl;
    }
    return nullptr;
}
void run()
{
    pthread_create(&_reader, nullptr, readMessage, (void *)&_sockfd);

    struct sockaddr_in server;
    memset(&server, 0, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(_serverip.c_str());
    server.sin_port = htons(_serverport);

    string message;
    char buffer[1024];
    while (!_quit)
    {
        fprintf(stderr, "Please Enter# ");
        fflush(stderr);

        fgets(buffer, sizeof(buffer), stdin);

        buffer[strlen(buffer) - 1] = 0;

        message = buffer;

        sendto(_sockfd, message.c_str(), message.size(), 0, (struct sockaddr *)&server, sizeof(server));
    }

成果展示

UDP套接字的通信(實(shí)現(xiàn)英漢互譯/程序替換/多線程聊天室/Windows與Linux通信),Linux網(wǎng)絡(luò)編程,udp,網(wǎng)絡(luò)協(xié)議,網(wǎng)絡(luò)

Windows與Linux通信

思路

讓Linux當(dāng)服務(wù)端,Windows當(dāng)客戶端。讓W(xué)indows與Linux通信,主要用到了一些Windows系統(tǒng)的socket創(chuàng)建的接口和sendto等接口,這些接口與Linux是及其相似的不做過(guò)多贅述。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-515784.html

Windows代碼

windows_client.cpp

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include<iostream>
#include<cstring>
#include<string>
#include<WinSock2.h>

#pragma comment(lib,"ws2_32.lib") // 將ws2_32.lib的庫(kù)

using namespace std;

// 顯示的定義并且初始化ip和port
uint16_t serverport = 8080;
string  serverip = "124.223.97.182";


int main()
{
	WSAData wsd;
	//啟動(dòng)Winsock
	//查看庫(kù)的版本是否正確
	if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
	{
		cout << "WSAStartup Error = " << WSAGetLastError() << endl;
		return 0;
	}
	else  cout << "WSAStartup Success" << endl;

	//創(chuàng)建套接字
	const SOCKET csock = socket(AF_INET, SOCK_DGRAM, 0);

	if (csock == SOCKET_ERROR)
	{
		cout << "sock Error = " << WSAGetLastError() << endl;
		return 1;
	}
	else  cout << "socket Success" << endl;

	//定義一個(gè) struct sockaddr_in類(lèi)型的結(jié)構(gòu)體
	struct sockaddr_in server;
	memset(&server, 0, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_port = htons(serverport);// 主機(jī)轉(zhuǎn)網(wǎng)絡(luò)
	server.sin_addr.s_addr = inet_addr(serverip.c_str()); // 主機(jī)轉(zhuǎn)網(wǎng)絡(luò) 字符串轉(zhuǎn)整型

#define NUM 1024
	char inbuffer[NUM];
	string line;
	while (true)
	{
		//發(fā)送邏輯
		cout << "Please Enter# ";
		getline(cin, line);

		int n = sendto(csock, line.c_str(), line.size(), 0, (struct sockaddr*)&server, sizeof(server));
		if (n < 0)
		{
			cerr << "sendto error!" << endl;
			break;
		}

		struct sockaddr_in peer;
		int peerlen = sizeof(peer);
		//收取數(shù)據(jù)

		inbuffer[0] = 0; // C風(fēng)格的清空
		n = recvfrom(csock, inbuffer, sizeof(inbuffer) - 1, 0, (struct sockaddr*)&peer, &peerlen);
		if (n > 0)
		{
			/*inbuffer[n] = 0;
			cout << "serever 返回的消息是# " << inbuffer << endl;*/
		}
		else break;
	}


	closesocket(csock);
	// 釋放資源
	WSACleanup();
	return 0;
}

Linux代碼

makefile

cc=g++

udpServer:udpServer.cc
	$(cc) -o $@ $^ -std=c++11

.PHONY:clean
clean:
	rm -f udpServer

udpServer.hpp

#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <cstdlib>
#include <strings.h>
#include <functional>

#include <netinet/in.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>

using namespace std;

namespace Server
{
    enum
    {
        SOCKET_ERR = 1,
        BIND_ERR,
        OPEN_ERR
    };

    const int NUM = 1024;
    static const string defaultIp = "0.0.0.0";

    typedef function<void(int, string, uint16_t, string)> func_t;

    class udpServer
    {

    public:
        udpServer(const func_t &cb, const uint16_t &port, const string ip = defaultIp)
            : _callback(cb), _serverport(port), _serverip(defaultIp), _sockfd(-1)
        {
            // cout << "拷貝構(gòu)造" << endl;
        }

        void initServer()
        {
            // 1. 套接字的創(chuàng)建
            _sockfd = socket(AF_INET, SOCK_DGRAM, 0);
            if (_sockfd == -1)
            {
                cerr << "socket error :" << errno << " : " << strerror(errno) << endl;
                exit(SOCKET_ERR);
            }
            cout << "socket success : " << _sockfd << endl;

            // 2. bind
            struct sockaddr_in local;
            bzero(&local, sizeof(local)); // 初始化local

            local.sin_family = AF_INET;

            // 1. 主機(jī)轉(zhuǎn)網(wǎng)絡(luò) 點(diǎn)分十進(jìn)制轉(zhuǎn)int
            local.sin_addr.s_addr = inet_addr(_serverip.c_str());
            // local.sin_addr.s_addr = INADDR_ANY;
            local.sin_port = htons(_serverport);
            int n = bind(_sockfd, (struct sockaddr *)&local, sizeof(local));

            if (n == -1)
            {
                cerr << "bind error: " << errno << " : " << strerror(errno) << endl;
                exit(BIND_ERR);
            }
        }
        void start()
        {
            char buffer[NUM];
            for (;;)
            {
                struct sockaddr_in peer;
                socklen_t len = sizeof(peer);
                //
                ssize_t s = recvfrom(_sockfd, buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&peer, &len);

                if (s > 0)
                {
                    // 讀取數(shù)據(jù)
                    buffer[s] = 0;
                    uint16_t clientport = ntohs(peer.sin_port);
                    string clientip = inet_ntoa(peer.sin_addr);
                    string message = buffer;

                    cout << clientip << " [" << clientport << "]# " << message << endl;
                    
                    // 回調(diào) 文件描述符和客戶端的端口號(hào)和ip
                    _callback(_sockfd,message,clientport,clientip);
                }
            }
        }

        ~udpServer()
        {
        }

    private:
        int _sockfd;
        uint16_t _serverport;
        string _serverip;
        func_t _callback;
    };
} // udpServer

udpServer.cc

#include "udpServer.hpp"
#include <memory>
#include <fstream>
#include <unordered_map>
#include <signal.h>

using namespace Server;
using namespace std;
static void Usage(string proc)
{
    cout << "\nUsage:\n\t" << proc << " local_port\n\n";
}

void handlerMessage(int sockfd, string message, uint16_t clientport, string clientip)
{
    string response_message = message;
    response_message += " [server echo]";
    // 開(kāi)始返回
    struct sockaddr_in client;
    bzero(&client, sizeof(client));

    client.sin_family = AF_INET;
    client.sin_port = htons(clientport);
    client.sin_addr.s_addr = inet_addr(clientip.c_str());

    sendto(sockfd, response_message.c_str(), response_message.size(), 0, (struct sockaddr *)&client, sizeof(client));
}

// ./udpClient server_ip server_port
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }

    uint16_t port = atoi(argv[1]); // 字符串轉(zhuǎn)整數(shù)

    unique_ptr<udpServer> usvr(new udpServer(handlerMessage, port));

    usvr->initServer();

    usvr->start();

    return 0;
}

成果展示

到了這里,關(guān)于UDP套接字的通信(實(shí)現(xiàn)英漢互譯/程序替換/多線程聊天室/Windows與Linux通信)的文章就介紹完了。如果您還想了解更多內(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)文章

  • UDP Ping程序?qū)崿F(xiàn)--第1關(guān):Ping服務(wù)端創(chuàng)建UDP套接字

    創(chuàng)作不易,請(qǐng)留個(gè)贊吧?。?! 任務(wù)描述 本關(guān)任務(wù):在 Ping 的服務(wù)程序中創(chuàng)建一個(gè)使用 UDP 協(xié)議的套接字。 相關(guān)知識(shí) 為了完成本關(guān)任務(wù),你需要掌握: 數(shù)據(jù)包套接字類(lèi)型; 為套接字綁定 IP 地址及端口。 數(shù)據(jù)包套接字 套接字有三種類(lèi)型:流式套接字( SOCK_STREAM ),數(shù)據(jù)包套接字

    2024年02月22日
    瀏覽(33)
  • 【Java】網(wǎng)絡(luò)編程與Socket套接字、UDP編程和TCP編程實(shí)現(xiàn)客戶端和服務(wù)端通信

    【Java】網(wǎng)絡(luò)編程與Socket套接字、UDP編程和TCP編程實(shí)現(xiàn)客戶端和服務(wù)端通信

    為什么需要網(wǎng)絡(luò)編程? 現(xiàn)在網(wǎng)絡(luò)普及程序越來(lái)越高,網(wǎng)絡(luò)上保存著我們?nèi)粘I钪行枰母鞣N資源,使用程序通過(guò)網(wǎng)絡(luò)來(lái)獲取這些資源的過(guò)程就需要網(wǎng)絡(luò)編程來(lái)實(shí)現(xiàn)。 什么是網(wǎng)絡(luò)編程? 網(wǎng)絡(luò)編程,指網(wǎng)絡(luò)上的主機(jī),通過(guò)不同的進(jìn)程以程序的方式實(shí)現(xiàn)網(wǎng)絡(luò)通信(網(wǎng)絡(luò)數(shù)據(jù)傳輸)

    2024年02月17日
    瀏覽(91)
  • 【網(wǎng)絡(luò)】socket——預(yù)備知識(shí) | 套接字 | UDP網(wǎng)絡(luò)通信

    【網(wǎng)絡(luò)】socket——預(yù)備知識(shí) | 套接字 | UDP網(wǎng)絡(luò)通信

    ??作者:一只大喵咪1201 ??專欄:《網(wǎng)絡(luò)》 ??格言: 你只管努力,剩下的交給時(shí)間! 在前面本喵對(duì)網(wǎng)絡(luò)的整體輪廓做了一個(gè)大概的介紹,比如分層,協(xié)議等等內(nèi)容,現(xiàn)在我們直接進(jìn)入socket(套接字)編程,先來(lái)感受到網(wǎng)絡(luò)編程。 我們知道,在網(wǎng)絡(luò)通信中,存在兩套地址,一

    2024年02月13日
    瀏覽(21)
  • 【Linux網(wǎng)絡(luò)】網(wǎng)絡(luò)編程套接字 -- 基于socket實(shí)現(xiàn)一個(gè)簡(jiǎn)單UDP網(wǎng)絡(luò)程序

    【Linux網(wǎng)絡(luò)】網(wǎng)絡(luò)編程套接字 -- 基于socket實(shí)現(xiàn)一個(gè)簡(jiǎn)單UDP網(wǎng)絡(luò)程序

    我們把數(shù)據(jù)從A主機(jī)發(fā)送到B主機(jī),是目的嗎?不是,真正通信的不是這兩個(gè)機(jī)器!其實(shí)是這兩臺(tái)機(jī)器上面的軟件(人) 數(shù)據(jù)有 IP(公網(wǎng)) 標(biāo)識(shí)一臺(tái)唯一的主機(jī) ,用誰(shuí)來(lái)標(biāo)識(shí)各自主機(jī)上客戶或者服務(wù)進(jìn)程的唯一性呢? 為了更好的表示一臺(tái)主機(jī)上服務(wù)進(jìn)程的唯一性,我們采用 端口號(hào)

    2024年02月12日
    瀏覽(848)
  • 【探索Linux】P.28(網(wǎng)絡(luò)編程套接字 —— 簡(jiǎn)單的UDP網(wǎng)絡(luò)程序模擬實(shí)現(xiàn))

    【探索Linux】P.28(網(wǎng)絡(luò)編程套接字 —— 簡(jiǎn)單的UDP網(wǎng)絡(luò)程序模擬實(shí)現(xiàn))

    在前一篇文章中,我們?cè)敿?xì)介紹了UDP協(xié)議和TCP協(xié)議的特點(diǎn)以及它們之間的異同點(diǎn)。 本文將延續(xù)上文內(nèi)容,重點(diǎn)討論簡(jiǎn)單的UDP網(wǎng)絡(luò)程序模擬實(shí)現(xiàn) 。通過(guò)本文的學(xué)習(xí),讀者將能夠深入了解UDP協(xié)議的實(shí)際應(yīng)用,并掌握如何編寫(xiě)簡(jiǎn)單的UDP網(wǎng)絡(luò)程序。讓我們一起深入探討UDP網(wǎng)絡(luò)程序的

    2024年04月08日
    瀏覽(585)
  • python3套接字編程之socket和socketserver(TCP和UDP通信)

    python3套接字編程之socket和socketserver(TCP和UDP通信)

    socket和socketserver是python3中socket通信模塊,關(guān)于其使用做如下總結(jié)。 目錄 1.socket 1.1模塊引入 1.2套接字獲取 1.3套接字接口 1.3.1 服務(wù)端 1.3.2 客戶端套接字函數(shù) 1.3.3 公共套接字函數(shù) 1.3.4 面向鎖的套接字方法 1.3.5 面向文件的套接字的函數(shù) 2.socketserver 3.TCP 3.1 socket類(lèi)型TCP 3.2 sockets

    2024年02月15日
    瀏覽(33)
  • 網(wǎng)絡(luò)編程套接字(2): 簡(jiǎn)單的UDP網(wǎng)絡(luò)程序

    網(wǎng)絡(luò)編程套接字(2): 簡(jiǎn)單的UDP網(wǎng)絡(luò)程序

    3.1 服務(wù)端創(chuàng)建 (1) 創(chuàng)建套接字 create an endpoint for communication: 創(chuàng)建用于通信的端點(diǎn) 關(guān)于socket參數(shù)詳細(xì)介紹: (1) domain: 指定套接字的通信域,相當(dāng)于 struct sockaddr結(jié)構(gòu)體的前16比特位(2字節(jié)) domain的選項(xiàng)是以宏的形式給出的,我們直接選用即可。常用就是上面框住的兩個(gè): AF_UNIX,本

    2024年02月10日
    瀏覽(91)
  • 網(wǎng)絡(luò)編程『socket套接字 ‖ 簡(jiǎn)易UDP網(wǎng)絡(luò)程序』

    網(wǎng)絡(luò)編程『socket套接字 ‖ 簡(jiǎn)易UDP網(wǎng)絡(luò)程序』

    ??個(gè)人主頁(yè): 北 海 ??所屬專欄: Linux學(xué)習(xí)之旅、神奇的網(wǎng)絡(luò)世界 ??操作環(huán)境: CentOS 7.6 阿里云遠(yuǎn)程服務(wù)器 在當(dāng)今數(shù)字化時(shí)代,網(wǎng)絡(luò)通信作為連接世界的橋梁,成為計(jì)算機(jī)科學(xué)領(lǐng)域中至關(guān)重要的一部分。理解網(wǎng)絡(luò)編程是每一位程序員必備的技能之一,而掌握套接字編程則

    2024年02月04日
    瀏覽(103)
  • 「網(wǎng)絡(luò)編程」第二講:網(wǎng)絡(luò)編程socket套接字(三)_ 簡(jiǎn)單TCP網(wǎng)絡(luò)通信程序的實(shí)現(xiàn)

    「網(wǎng)絡(luò)編程」第二講:網(wǎng)絡(luò)編程socket套接字(三)_ 簡(jiǎn)單TCP網(wǎng)絡(luò)通信程序的實(shí)現(xiàn)

    「前言」文章是關(guān)于網(wǎng)絡(luò)編程的socket套接字方面的,上一篇是網(wǎng)絡(luò)編程socket套接字(二),下面開(kāi)始講解!? 「歸屬專欄」網(wǎng)絡(luò)編程 「主頁(yè)鏈接」個(gè)人主頁(yè) 「筆者」楓葉先生(fy) 「楓葉先生有點(diǎn)文青病」「每篇一句」 I?do?not?know?where?to?go,but?I?have?been?on?the?road. 我不知

    2024年02月11日
    瀏覽(29)
  • 網(wǎng)絡(luò)編程套接字(二)之UDP服務(wù)器簡(jiǎn)單實(shí)現(xiàn)

    網(wǎng)絡(luò)編程套接字(二)之UDP服務(wù)器簡(jiǎn)單實(shí)現(xiàn)

    目錄 一、服務(wù)端UdpServer 1、udp_server.hpp 1、服務(wù)器的初始化 2、服務(wù)器的運(yùn)行 2、udp_server.cc 二、客戶端UdpClient udp_client.cc 三、完整代碼 首先,我們?cè)谠撐募?,將服?wù)器封裝成一個(gè)類(lèi),而作為一款服務(wù)器,必須要有自己的端口號(hào),同時(shí)網(wǎng)絡(luò)服務(wù)器需要有對(duì)應(yīng)的IP地址,文件描述

    2024年04月16日
    瀏覽(20)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包