1. 程式人生 > 實用技巧 >windows下IPv4通訊(C++、MFC)

windows下IPv4通訊(C++、MFC)


Cilect

#include <stdio.h>
#include <Ws2tcpip.h>
#include <winsock2.h>
#define HELLO_PORT  7905    
#define HELLO_GROUP "224.0.0.1"    
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main()
{
    WSADATA wsaData;//初始化
    //初始化Socket
    WSAStartup(MAKEWORD(2, 2), &wsaData);
​
    sockaddr_in RecvAddr;         //伺服器地址
    sockaddr_in SenderAddr;       //本地地址
    int localPort = 4001;         //本地監聽埠
    int Port = 4000;              //伺服器監聽埠
​
    char SendBuf[1024];           //傳送資料的緩衝區
    int BufLen = 1024;            //緩衝區大小
    //建立Socket物件
    SOCKET SendSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    //設定伺服器地址
    RecvAddr.sin_family = AF_INET;
    RecvAddr.sin_port = htons(Port);
    inet_pton(AF_INET, "127.0.0.1", &RecvAddr.sin_addr);
​
    //繫結繫結監聽埠
    SenderAddr.sin_family = AF_INET;
    SenderAddr.sin_port = htons(localPort);
    SenderAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    bind(SendSocket, (SOCKADDR*)&SenderAddr, sizeof(SenderAddr));
​
    int l_naddLen1 = sizeof(SenderAddr);
    printf("客戶端傳送:\n");
    while (1)
    {
        strcpy_s(SendBuf, "hello");
        int l_nLen = sendto(SendSocket, SendBuf, strlen(SendBuf), 0, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr));
        if (l_nLen < 0)
        {
            perror("傳送失敗");
            exit(1);
        }
​
        printf("\nSend:");
        for (int i = 0; i < strlen(SendBuf); i++)
        {
            printf("%02x ", (UCHAR)SendBuf[i]);
        }
        int l_nReadLen = recvfrom(SendSocket, SendBuf, BufLen, 0, (struct sockaddr*)&SenderAddr, &l_naddLen1);
        printf("\nread:");
        for (int i = 0; i < l_nReadLen; i++)
        {
            printf("%02x ", SendBuf[i]);
        }
        Sleep(1000);
​
    }
    //傳送完成,關閉Socket
    closesocket(SendSocket);
    WSACleanup();
    return 0;
}

Server

#include<WINSOCK2.H>
#include<iostream>
#pragma comment(lib,"WS2_32.lib")
using namespace std;
int main()
{
    WSADATA wsaData;//初始化
    //初始化Socket
    WSAStartup(MAKEWORD(2, 2), &wsaData);
​
    sockaddr_in RecvAddr;        //本地地址
    sockaddr_in SenderAddr;      //伺服器地址
    int Port = 4000;             //本地監聽地址
​
    char RecvBuf[1024];//傳送資料的緩衝區
    int BufLen = 1024;//緩衝區大小
    //建立接收資料報的socket
    SOCKET RecvSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    //繫結本地監聽地址
    RecvAddr.sin_family = AF_INET;
    RecvAddr.sin_port = htons(Port);
    RecvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    bind(RecvSocket, (SOCKADDR*)&RecvAddr, sizeof(RecvAddr));
​
    int SenderAddrSize = sizeof(SenderAddr);
    printf("服務的接收:\n");
    while (1)
    {
        strcpy_s(RecvBuf, "hello");
        int l_nLen = recvfrom(RecvSocket, RecvBuf,BufLen, 0, (SOCKADDR*)&SenderAddr, &SenderAddrSize);
​
        if (l_nLen < 0)
        {
            perror("傳送失敗");
            exit(1);
        }
​
        printf("\nSend:");
        for (int i = 0; i < strlen(RecvBuf); i++)
        {
            printf("%02x ", (UCHAR)RecvBuf[i]);
        }
        int l_nReadLen = sendto(RecvSocket, RecvBuf, strlen(RecvBuf), 0, (SOCKADDR*)&SenderAddr, SenderAddrSize);
        printf("\nread:");
        for (int i = 0; i < l_nReadLen; i++)
        {
            printf("%02x ", RecvBuf[i]);
        }
        Sleep(1000);
​
    }
    //呼叫Recvfrom函式在繫結的socket上接收資料
    //關閉socket,結束接收資料
    closesocket(RecvSocket);
    //釋放資源,退出
    WSACleanup();
    return 0;
}