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

C++實(shí)現(xiàn)socket網(wǎng)絡(luò)通信

這篇具有很好參考價(jià)值的文章主要介紹了C++實(shí)現(xiàn)socket網(wǎng)絡(luò)通信。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

??SOCKET網(wǎng)絡(luò)通信系列文章鏈接如下:??
??【小沐學(xué)python】(一)Python簡介和安裝??
??Python實(shí)現(xiàn)socket網(wǎng)絡(luò)通信??
??C++實(shí)現(xiàn)socket網(wǎng)絡(luò)通信??
??Android實(shí)現(xiàn)socket網(wǎng)絡(luò)通信??
??nodejs實(shí)現(xiàn)socket網(wǎng)絡(luò)通信??

《斗詩篇》

陳獻(xiàn)章:
窗外竹青青,窗間人獨(dú)坐。
究竟竹與人,原來無兩個(gè)。

狄狄:
門外日辣辣,門內(nèi)清涼涼。
究竟內(nèi)與外,原來差別大。

戈戈:
橋下楊柳綠,橋上人獨(dú)立。
究竟柳與人,原來無分別。

詩云:“但愿人長久,千里共嬋娟”。
許多年以后,戈戈獨(dú)坐在終南山一處僻靜的茅草屋的門檻上,仰望著滿天繁星?,回想著那一句“山中方一日,世上已千年”。回想起從前那一個(gè)個(gè)漆黑靜謐的夜??,那些模糊的兒時(shí)回憶,那些一去不復(fù)返的時(shí)光,那些熟悉而又陌生的人名。一輪明月又悄悄地掛在天上??,時(shí)隱時(shí)現(xiàn)。銀紗般淡淡的光芒灑向廣袤的大地,也輕輕地灑在戈戈那總是皺著眉頭的臉頰上……

詩云:“危樓高百尺,手可摘星辰”。
曾幾何時(shí),戈戈常常在樓頂放一張?zhí)梢危惶梢煌?。像往常一樣靜靜地凝視著星海,那整齊排列的北斗星,那十五皎潔的滿月??,以及遠(yuǎn)方星星點(diǎn)點(diǎn)的燈光。為什么,有些燈光總是一會(huì)開,一會(huì)滅呢……

詩云:“竹深樹密蟲鳴處,時(shí)有微涼不是風(fēng)”。
夏夜的風(fēng)是令人期待的,徐徐吹來,格外清新。池塘泛起陣陣漣漪,柳枝??也輕輕地拂動(dòng)著,心曠神怡。戈戈的思緒仿佛大雁一般長了翅膀??飄向那遙遠(yuǎn)的月宮……

詩云:“明月別枝驚鵲,清風(fēng)半夜鳴蟬”。
“有人又嘆氣了?!钡业覈樍艘惶?,“媽呀???”,眼睛離開天上的玉盤朝四周看去,尋找剛才聲音的來處?!芭逗?!沒人?”,附近哪有什么人影??,只有遠(yuǎn)處草叢若隱若現(xiàn)的螢火蟲飛舞??,以及不知哪處水坑傳來的陣陣蛙聲。狄狄試探地顫顫地輕聲問道:“你是誰?”,等了許久也沒有回應(yīng)……

詩云:“風(fēng)起白蘋初日晚,霜雕紅葉欲秋分”。
秋分快到了,夜長了,天冷了,心也倦了,狄狄散完步準(zhǔn)備回家。“珍重,網(wǎng)絡(luò)世界熬夜的朋友們??。”,又一聲,聽到這,狄狄愣了一下,看了一眼頭頂?shù)膱A月,接著朝天上揮了揮手,靜靜地朝前走去……
C++實(shí)現(xiàn)socket網(wǎng)絡(luò)通信

春有百花秋有月 ,夏有涼風(fēng)冬有雪 ,若無閑事掛心頭,便是人間好時(shí)節(jié)。

1、簡介

socket顧名思義就是套接字的意思,用于描述地址和端口,是一個(gè)通信鏈的句柄。應(yīng)用程序通過socket向網(wǎng)絡(luò)發(fā)出請求或者回應(yīng)。

socket編程有三種,流式套接字(SOCK_STREAM),數(shù)據(jù)報(bào)套接字(SOCK_DGRAM),原始套接字(SOCK_RAW),前兩者較常用。基于TCP的socket編程是流式套接字。

2、TCP方式

  • Server
  1. Initialize Winsock.
  2. Create a socket.
  3. Bind the socket.
  4. Listen on the socket for a client.
  5. Accept a connection from a client.
  6. Receive and send data.
  7. Disconnect.
  • Client
  1. Initialize Winsock.
  2. Create a socket.
  3. Connect to the server.
  4. Send and receive data.
  5. Disconnect.

2.1 服務(wù)端

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int __cdecl main(void) 
{
    WSADATA wsaData;
    SOCKET ListenSocket = INVALID_SOCKET,
           ClientSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL,
                    hints;
    char recvbuf[DEFAULT_BUFLEN];
    int iResult, iSendResult;
    int recvbuflen = DEFAULT_BUFLEN;
    

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    ZeroMemory(&hints, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_PASSIVE;

    // Resolve the server address and port
    iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    if ( iResult != 0 ) {
        printf("getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }

    // Create a SOCKET for connecting to server
    ListenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    if (ListenSocket == INVALID_SOCKET) {
        printf("socket failed: %ld\n", WSAGetLastError());
        freeaddrinfo(result);
        WSACleanup();
        return 1;
    }

    // Setup the TCP listening socket
    iResult = bind( ListenSocket, result->ai_addr, (int)result->ai_addrlen);
    if (iResult == SOCKET_ERROR) {
        printf("bind failed: %d\n", WSAGetLastError());
        freeaddrinfo(result);
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    freeaddrinfo(result);

    iResult = listen(ListenSocket, SOMAXCONN);
    if (iResult == SOCKET_ERROR) {
        printf("listen failed: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    // Accept a client socket
    ClientSocket = accept(ListenSocket, NULL, NULL);
    if (ClientSocket == INVALID_SOCKET) {
        printf("accept failed: %d\n", WSAGetLastError());
        closesocket(ListenSocket);
        WSACleanup();
        return 1;
    }

    // No longer need server socket
    closesocket(ListenSocket);

    // Receive until the peer shuts down the connection
    do {

        iResult = recv(ClientSocket, recvbuf, recvbuflen, 0);
        if (iResult > 0) {
            printf("Bytes received: %d\n", iResult);

        // Echo the buffer back to the sender
            iSendResult = send( ClientSocket, recvbuf, iResult, 0 );
            if (iSendResult == SOCKET_ERROR) {
                printf("send failed: %d\n", WSAGetLastError());
                closesocket(ClientSocket);
                WSACleanup();
                return 1;
            }
            printf("Bytes sent: %d\n", iSendResult);
        }
        else if (iResult == 0)
            printf("Connection closing...\n");
        else  {
            printf("recv failed: %d\n", WSAGetLastError());
            closesocket(ClientSocket);
            WSACleanup();
            return 1;
        }

    } while (iResult > 0);

    // shutdown the connection since we're done
    iResult = shutdown(ClientSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed: %d\n", WSAGetLastError());
        closesocket(ClientSocket);
        WSACleanup();
        return 1;
    }

    // cleanup
    closesocket(ClientSocket);
    WSACleanup();

    return 0;
}

2.2 客戶端

#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_BUFLEN 512
#define DEFAULT_PORT "27015"

int __cdecl main(int argc, char **argv) 
{
    WSADATA wsaData;
    SOCKET ConnectSocket = INVALID_SOCKET;
    struct addrinfo *result = NULL,
                    *ptr = NULL,
                    hints;
    char *sendbuf = "this is a test";
    char recvbuf[DEFAULT_BUFLEN];
    int iResult;
    int recvbuflen = DEFAULT_BUFLEN;
    
    // Validate the parameters
    if (argc != 2) {
        printf("usage: %s server-name\n", argv[0]);
        return 1;
    }

    // Initialize Winsock
    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
    if (iResult != 0) {
        printf("WSAStartup failed: %d\n", iResult);
        return 1;
    }

    ZeroMemory( &hints, sizeof(hints) );
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;

    // Resolve the server address and port
    iResult = getaddrinfo(argv[1], DEFAULT_PORT, &hints, &result);
    if ( iResult != 0 ) {
        printf("getaddrinfo failed: %d\n", iResult);
        WSACleanup();
        return 1;
    }

    // Attempt to connect to an address until one succeeds
    for(ptr=result; ptr != NULL ;ptr=ptr->ai_next) {

        // Create a SOCKET for connecting to server
        ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, 
            ptr->ai_protocol);
        if (ConnectSocket == INVALID_SOCKET) {
            printf("Error at socket(): %ld\n", WSAGetLastError());
            freeaddrinfo(result);
            WSACleanup();
            return 1;
        }

        // Connect to server.
        iResult = connect( ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            closesocket(ConnectSocket);
            ConnectSocket = INVALID_SOCKET;
            continue;
        }
        break;
    }

    freeaddrinfo(result);

    if (ConnectSocket == INVALID_SOCKET) {
        printf("Unable to connect to server!\n");
        WSACleanup();
        return 1;
    }

    // Send an initial buffer
    iResult = send( ConnectSocket, sendbuf, (int)strlen(sendbuf), 0 );
    if (iResult == SOCKET_ERROR) {
        printf("send failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    printf("Bytes Sent: %ld\n", iResult);

    // shutdown the connection since no more data will be sent
    iResult = shutdown(ConnectSocket, SD_SEND);
    if (iResult == SOCKET_ERROR) {
        printf("shutdown failed: %d\n", WSAGetLastError());
        closesocket(ConnectSocket);
        WSACleanup();
        return 1;
    }

    // Receive until the peer closes the connection
    do {

        iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
        if ( iResult > 0 )
            printf("Bytes received: %d\n", iResult);
        else if ( iResult == 0 )
            printf("Connection closed\n");
        else
            printf("recv failed: %d\n", WSAGetLastError());

    } while( iResult > 0 );

    // cleanup
    closesocket(ConnectSocket);
    WSACleanup();

    return 0;
}

3、UDP方式

3.1 接收端

#include <stdio.h>
#include "winsock2.h"
#pragma comment(lib, "Ws2_32.lib")

void main() {

  WSADATA wsaData;
  SOCKET RecvSocket;
  sockaddr_in RecvAddr;
  int Port = 27015;
  char RecvBuf[1024];
  int  BufLen = 1024;
  sockaddr_in SenderAddr;
  int SenderAddrSize = sizeof(SenderAddr);

  //-----------------------------------------------
  // Initialize Winsock
  WSAStartup(MAKEWORD(2,2), &wsaData);

  //-----------------------------------------------
  // Create a receiver socket to receive datagrams
  RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

  //-----------------------------------------------
  // Bind the socket to any address and the specified port.
  RecvAddr.sin_family = AF_INET;
  RecvAddr.sin_port = htons(Port);
  RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);

  bind(RecvSocket, (SOCKADDR *) &RecvAddr, sizeof(RecvAddr));

  //-----------------------------------------------
  // Call the recvfrom function to receive datagrams
  // on the bound socket.
  printf("Receiving datagrams...\n");
  recvfrom(RecvSocket, 
    RecvBuf, 
    BufLen, 
    0, 
    (SOCKADDR *)&SenderAddr, 
    &SenderAddrSize);

  //-----------------------------------------------
  // Close the socket when finished receiving datagrams
  printf("Finished receiving. Closing socket.\n");
  closesocket(RecvSocket);

  //-----------------------------------------------
  // Clean up and exit.
  printf("Exiting.\n");
  WSACleanup();
  return;
}

3.2 發(fā)送端

#include <stdio.h>
#include "winsock2.h"
#pragma comment(lib, "Ws2_32.lib")

void main() {
  
  WSADATA wsaData;
  SOCKET SendSocket;
  sockaddr_in RecvAddr;
  int Port = 27015;
  char SendBuf[1024];
  int BufLen = 1024;

  //---------------------------------------------
  // Initialize Winsock
  WSAStartup(MAKEWORD(2,2), &wsaData);

  //---------------------------------------------
  // Create a socket for sending data
  SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

  //---------------------------------------------
  // Set up the RecvAddr structure with the IP address of
  // the receiver (in this example case "123.456.789.1")
  // and the specified port number.
  RecvAddr.sin_family = AF_INET;
  RecvAddr.sin_port = htons(Port);
  RecvAddr.sin_addr.s_addr = inet_addr("123.456.789.1");

  //---------------------------------------------
  // Send a datagram to the receiver
  printf("Sending a datagram to the receiver...\n");
  sendto(SendSocket, 
    SendBuf, 
    BufLen, 
    0, 
    (SOCKADDR *) &RecvAddr, 
    sizeof(RecvAddr));

  //---------------------------------------------
  // When the application is finished sending, close the socket.
  printf("Finished sending. Closing socket.\n");
  closesocket(SendSocket);

  //---------------------------------------------
  // Clean up and quit.
  printf("Exiting.\n");
  WSACleanup();
  return;
}

4、HTTP方式

無論是Http還是Https都是基于TCP進(jìn)行傳輸?shù)模虼耸褂肧OCKET模擬HTTP訪問web站點(diǎn)的方式,就是將頭部數(shù)據(jù)拼接成數(shù)據(jù)包,發(fā)送給服務(wù)端,然后接收返回再解析就可以了。

這里通過socket訪問如下網(wǎng)易新聞api接口網(wǎng)址獲取對應(yīng)數(shù)據(jù):

http://data.live.126.net/livechannel/classifylist.json
C++實(shí)現(xiàn)socket網(wǎng)絡(luò)通信


#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#pragma comment(lib, "Ws2_32.lib")

#define DEFAULT_BUFLEN 4086
#define DEFAULT_PORT 80
#define DEFAULT_URL "/livechannel/classifylist.json"
#define DEFAULT_DOMAIN "data.live.126.net" 

void get_ip_addr(const char *domain, char *ip_addr)
{
	/*通過域名得到相應(yīng)的ip地址*/
	struct hostent *host = gethostbyname(domain);
	if (!host)
	{
		ip_addr = NULL;
		return;
	}

	for (int i = 0; host->h_addr_list[i]; i++)
	{
		strcpy(ip_addr, inet_ntoa(*(struct in_addr*) host->h_addr_list[i]));
		break;
	}
}

int __cdecl main(int argc, char **argv)
{
	WSADATA wsaData;
	SOCKET ConnectSocket = INVALID_SOCKET;
	const char *sendbuf = "this is a test";
	char recvbuf[DEFAULT_BUFLEN];
	int iResult;
	int recvbuflen = DEFAULT_BUFLEN;

	// Initialize Winsock
	iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
	if (iResult != 0) {
		printf("WSAStartup failed: %d\n", iResult);
		return 1;
	}

	char szRemoteIPAddr[128];
	get_ip_addr(DEFAULT_DOMAIN, szRemoteIPAddr);

	// Create a SOCKET for connecting to server
	ConnectSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (ConnectSocket == INVALID_SOCKET) {
		printf("Error at socket(): %ld\n", WSAGetLastError());
		WSACleanup();
		return 1;
	}

	struct sockaddr_in serverAddr;
	serverAddr.sin_family = AF_INET;
	serverAddr.sin_port = htons(DEFAULT_PORT);
	serverAddr.sin_addr.s_addr = inet_addr(szRemoteIPAddr);

	// Connect to server.
	iResult = connect(ConnectSocket, (struct sockaddr *)&serverAddr, sizeof(serverAddr));
	if (iResult == SOCKET_ERROR) {
		closesocket(ConnectSocket);
		ConnectSocket = INVALID_SOCKET;
		return 1;
	}

	// Send an initial buffer
	std::string head = "GET " DEFAULT_URL " HTTP/1.1\r\n";
	head.append("Host: " DEFAULT_DOMAIN "\r\n");//請求的域名
	head.append("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n");
	head.append("User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36\r\n");
	head.append("Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n");
	head.append("Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7");
	head.append("Accept-Encoding: gzip,deflate\r\n");
	head.append("\r\n");//表明請求頭結(jié)束了

	iResult = send(ConnectSocket, head.c_str(), (int)head.size(), 0);
	if (iResult == SOCKET_ERROR) {
		printf("send failed: %d\n", WSAGetLastError());
		closesocket(ConnectSocket);
		WSACleanup();
		return 1;
	}

	printf("Bytes Sent: %ld\n", iResult);

	// Receive until the peer closes the connection
	do {
		memset(recvbuf, 0, sizeof(recvbuf));
		iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
		if (iResult > 0) {
			printf("Bytes received: %d\n", iResult);
			printf(recvbuf);
		}
		else if (iResult == 0)
			printf("Connection closed\n");
		else
			printf("recv failed: %d\n", WSAGetLastError());

	} while (iResult > 0);

	// cleanup
	closesocket(ConnectSocket);
	WSACleanup();

	return 0;
}

C++實(shí)現(xiàn)socket網(wǎng)絡(luò)通信

后續(xù)

如果您覺得該方法或代碼有一點(diǎn)點(diǎn)用處,可以給作者點(diǎn)個(gè)贊,或打賞杯咖啡;╮( ̄▽ ̄)╭
如果您感覺方法或代碼不咋地//(ㄒoㄒ)//,就在評(píng)論處留言,作者繼續(xù)改進(jìn);o_O???
如果您需要相關(guān)功能的代碼定制化開發(fā),可以留言私信作者;(????)
感謝各位大佬童鞋們的支持!( ′ ▽′ )? ( ′ ▽′)っ?。。?mark hidden color="red">文章來源:http://www.zghlxwxcb.cn/news/detail-405435.html

C++實(shí)現(xiàn)socket網(wǎng)絡(luò)通信

終日尋春不見春,芒鞋踏破嶺頭云。歸來偶把梅花嗅,春在枝頭已十分。文章來源地址http://www.zghlxwxcb.cn/news/detail-405435.html

到了這里,關(guān)于C++實(shí)現(xiàn)socket網(wǎng)絡(luò)通信的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

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

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

相關(guān)文章

  • Linux網(wǎng)絡(luò)編程:socket實(shí)現(xiàn)client/server通信

    Linux網(wǎng)絡(luò)編程:socket實(shí)現(xiàn)client/server通信

    閱讀 UNIX網(wǎng)絡(luò)編程 卷1:套接字聯(lián)網(wǎng)API 第3版 的前4個(gè)章節(jié),覺得有必要對書籍上的源碼案例進(jìn)行復(fù)現(xiàn),并推敲TCP的C/S通信過程。 ?? 測試環(huán)境:CentOS7.6 x64 編譯server.c 和 client.c gcc server.c -g -std=gnu99 -o server 和 gcc client.c -g -std=gnu99 -o client 運(yùn)行測試: ?? server.c僅僅實(shí)現(xiàn)對單個(gè)客戶

    2024年02月03日
    瀏覽(20)
  • 【Unity】Socket網(wǎng)絡(luò)通信(TCP) - 實(shí)現(xiàn)簡單的多人聊天功能

    【Unity】Socket網(wǎng)絡(luò)通信(TCP) - 實(shí)現(xiàn)簡單的多人聊天功能

    多客戶端連接服務(wù)器其原理是在服務(wù)端保存客戶端連入后與客戶端通信的socket,由于等待客戶端連接會(huì)阻塞主線程,所以結(jié)合多線程就能實(shí)現(xiàn)多客戶端連入功能。多人聊天只需要將A客戶端發(fā)來的消息,轉(zhuǎn)發(fā)給除A客戶端外的其他客戶端,即可實(shí)現(xiàn)。如果你還不怎么熟悉服務(wù)端

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

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

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

    2024年02月11日
    瀏覽(29)
  • Linux網(wǎng)絡(luò)編程:socket & fork實(shí)現(xiàn)clients/server通信

    Linux網(wǎng)絡(luò)編程:socket & fork實(shí)現(xiàn)clients/server通信

    UNIX網(wǎng)絡(luò)編程:socket實(shí)現(xiàn)client/server通信 隨筆簡單介紹了TCP Server服務(wù)單客戶端的socket通信,但是并未涉及多客戶端通信。 對于網(wǎng)絡(luò)編程肯定涉及到多客戶端通信和并發(fā)編程 (指在同時(shí)有大量的客戶鏈接到同一服務(wù)器),故本隨筆補(bǔ)充這部分知識(shí)。 而且并發(fā)并發(fā)編程涉及到多進(jìn)程

    2024年02月05日
    瀏覽(21)
  • Socket網(wǎng)絡(luò)編程(TCP/IP)實(shí)現(xiàn)服務(wù)器/客戶端通信。

    Socket網(wǎng)絡(luò)編程(TCP/IP)實(shí)現(xiàn)服務(wù)器/客戶端通信。

    一.前言 回顧之前進(jìn)程間通信(無名管道,有名管道,消息隊(duì)列,共享內(nèi)存,信號(hào),信號(hào)量),都是在同一主機(jī)由內(nèi)核來完成的通信。 那不同主機(jī)間該怎么通信呢? 可以使用Socket編程來實(shí)現(xiàn)。 Socket編程可以通過網(wǎng)絡(luò)來實(shí)現(xiàn)實(shí)現(xiàn)不同主機(jī)之間的通訊。 二.Socket編程的網(wǎng)絡(luò)模型如

    2024年02月08日
    瀏覽(37)
  • Linux網(wǎng)絡(luò)編程:Socket服務(wù)器和客戶端實(shí)現(xiàn)雙方通信

    Linux網(wǎng)絡(luò)編程:Socket服務(wù)器和客戶端實(shí)現(xiàn)雙方通信

    目錄 一,什么是網(wǎng)絡(luò)編程 二,為什么使用端口號(hào) 三,TCP協(xié)議與UDP協(xié)議 ①TCP(傳輸控制協(xié)議) ②UDP(用戶數(shù)據(jù)報(bào)協(xié)議,User Data Protocol) ③總結(jié)歸納 四,Socket服務(wù)器和客戶端的開發(fā)流程 五,服務(wù)器和客戶端相關(guān)API說明 ①socket()函數(shù) ②bind()函數(shù) ③listen()函數(shù) ④accept()函數(shù) ⑤客戶端

    2024年02月11日
    瀏覽(34)
  • UNIX網(wǎng)絡(luò)編程:socket & fork()多進(jìn)程 實(shí)現(xiàn)clients/server通信

    UNIX網(wǎng)絡(luò)編程:socket & fork()多進(jìn)程 實(shí)現(xiàn)clients/server通信

    UNIX網(wǎng)絡(luò)編程:socket實(shí)現(xiàn)client/server通信 隨筆簡單介紹了TCP Server服務(wù)單客戶端的socket通信,但是并未涉及多客戶端通信。 對于網(wǎng)絡(luò)編程肯定涉及到多客戶端通信和并發(fā)編程 (指在同時(shí)有大量的客戶鏈接到同一服務(wù)器),故本隨筆補(bǔ)充這部分知識(shí)。 而且并發(fā)并發(fā)編程涉及到多進(jìn)程

    2024年02月06日
    瀏覽(23)
  • Linux網(wǎng)絡(luò)編程:socket & fork()多進(jìn)程 實(shí)現(xiàn)clients/server通信

    Linux網(wǎng)絡(luò)編程:socket & fork()多進(jìn)程 實(shí)現(xiàn)clients/server通信

    UNIX網(wǎng)絡(luò)編程:socket實(shí)現(xiàn)client/server通信 隨筆簡單介紹了TCP Server服務(wù)單客戶端的socket通信,但是并未涉及多客戶端通信。 對于網(wǎng)絡(luò)編程肯定涉及到多客戶端通信和并發(fā)編程 (指在同時(shí)有大量的客戶鏈接到同一服務(wù)器),故本隨筆補(bǔ)充這部分知識(shí)。 而且并發(fā)并發(fā)編程涉及到多進(jìn)程

    2024年02月05日
    瀏覽(28)
  • 網(wǎng)絡(luò)編程-Socket通信實(shí)現(xiàn)服務(wù)器與客戶端互傳文件(JAVA語言實(shí)現(xiàn))

    網(wǎng)絡(luò)編程-Socket通信實(shí)現(xiàn)服務(wù)器與客戶端互傳文件(JAVA語言實(shí)現(xiàn))

    在網(wǎng)絡(luò)通信協(xié)議下,實(shí)現(xiàn)網(wǎng)絡(luò)互連的不同計(jì)算機(jī)上運(yùn)行的程序間可以進(jìn)行數(shù)據(jù)交換. 網(wǎng)絡(luò)編程三要素:ip地址、端口、協(xié)議 ip地址: 每臺(tái)計(jì)算機(jī)指定的一個(gè)標(biāo)識(shí)符,127.0.0.1是回送地址,可以代表本機(jī)地址 ,一般用來測試使用 ipconfig:命令行中查看本機(jī)地址 ping ip地址:檢查網(wǎng)絡(luò)是

    2023年04月14日
    瀏覽(31)
  • 【Java網(wǎng)絡(luò)編程】基于UDP-Socket 實(shí)現(xiàn)客戶端、服務(wù)器通信

    【Java網(wǎng)絡(luò)編程】基于UDP-Socket 實(shí)現(xiàn)客戶端、服務(wù)器通信

    ? 哈嘍,大家好~我是你們的老朋友: 保護(hù)小周??? 本期為大家?guī)淼氖蔷W(wǎng)絡(luò)編程的 UDP Socket 套接字,基于 UDP協(xié)議的 Socket 實(shí)現(xiàn)客戶端服務(wù)器通信 ,Socket 套接字可以理解為是,傳輸層給應(yīng)用層提供的一組 API,如此程序,確定不來看看嘛~~ 本期收錄于博主的專欄 : JavaEE_保

    2024年02月02日
    瀏覽(111)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包