C++网络编程笔记

简介: 环境准备codeblocks1 Settings–Compiler–Linker Settings中添加~\CodeBlocks\MinGW\lib\libwsock32.a 2 #include <winsock2.h>vs2010#include <winsock2.h> #pragma comment(lib, "ws2

环境准备

codeblocks

1 Settings–Compiler–Linker Settings中添加~\CodeBlocks\MinGW\lib\libwsock32.a
2 #include <winsock2.h>

vs2010

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

获取本机IP:

#include <iostream>
#include <winsock2.h>
using namespace std;

int main()
{
    WSADATA wsaData;
    WORD version = MAKEWORD(2, 2);
    if(::WSAStartup(version, &wsaData) != 0)
    {
        exit(0);
    }

    char szHost[256];
    ::gethostname(szHost, 256);
    hostent *pHost = ::gethostbyname(szHost);
    in_addr addr;
    for (int i = 0; ; i++)
    {
        char *p = pHost->h_addr_list[i];
        if (p == NULL)
        {
            break;
        }
        memcpy(&addr.S_un.S_addr, p, pHost->h_length);
        char *szIp = ::inet_ntoa(addr);
        cout<<szIp<<endl;
    }

    ::WSACleanup();
    return 0;
}

最简单的TCP程序:

服务器:

#include <iostream>
#include <winsock2.h>
#pragma comment(lib, "ws2_32")
using namespace std;

int main()
{
    WSADATA wsaData;
    WORD version = MAKEWORD(2, 2);
    WSAStartup(version, &wsaData);

    SOCKET sListen = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sListen == INVALID_SOCKET)
    {
        return 0;
    }

    sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(4567);
    sin.sin_addr.S_un.S_addr = INADDR_ANY;
    if (::bind(sListen, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
    {
        cout<<"Failed bind()"<<endl;
        return 0;
    }

    if (::listen(sListen, 2) == SOCKET_ERROR)
    {
        cout<<"Failed listen()"<<endl;
        return 0;
    }

    sockaddr_in remoteAddr;
    int nAddrLen = sizeof(remoteAddr);
    SOCKET sClient;
    char szText[] = "TCP Server Demo!\r\n";
    while (true)
    {
        //sClient = ::accept(sListen, (SOCKADDR*)&remoteAddr, &nAddrLen);
        sClient = ::accept(sListen, (LPSOCKADDR)&remoteAddr, &nAddrLen);
        if (sClient == INVALID_SOCKET)
        {
            cout<<"Failed accept()";
            continue;
        }
        cout<<"Received an connection: "<<inet_ntoa(remoteAddr.sin_addr)<<endl;
        ::send(sClient, szText, strlen(szText), 0);
        ::closesocket(sClient);
    }
    ::closesocket(sListen);
    return 0;
}

客户端:

#include <iostream>
#include <winsock2.h>
#pragma comment(lib, "ws2_32")
using namespace std;

int main()
{
    WSADATA wsaData;
    WORD version = MAKEWORD(2, 2);
    if (::WSAStartup(version, &wsaData) != 0)
    {
        exit(0);
    }

    SOCKET s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (s == INVALID_SOCKET)
    {
        cout<<"Failed socket()"<<endl;
        return 0;
    }

    sockaddr_in servAddr;
    servAddr.sin_family = AF_INET;
    servAddr.sin_port = htons(4567);
    servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    if (::connect(s, (sockaddr*)&servAddr, sizeof(servAddr)) == -1)
    {
        cout<<"Failed connect()"<<endl;
        return 0;
    }

    char buff[255];
    int nRecv = ::recv(s, buff, 256, 0);
    if (nRecv > 0)
    {
        buff[nRecv] = '\0';
        cout<<"Received information: "<<buff<<endl;
    }

    ::closesocket(s);
    ::WSACleanup();
    return 0;
}

最简单的UDP程序:

服务器:

#include <iostream>
#include <winsock2.h>
#pragma comment(lib, "ws2_32")
using namespace std;

int main()
{
    WSADATA wsaData;
    WORD version = MAKEWORD(2, 2);
    if (::WSAStartup(version, &wsaData) != 0)
    {
        exit(0);
    }

    SOCKET s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (s == INVALID_SOCKET)
    {
        cout<<"Failed socket()\n";
        return 0;
    }

    sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(4567);
    sin.sin_addr.S_un.S_addr = INADDR_ANY;
    if (::bind(s, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
    {
        cout<<"Failed bind()\n";
        return 0;
    }
    char buff[1024];
    sockaddr_in addr;
    int nLen = sizeof(addr);
    while (true)
    {
        int nRecv = ::recvfrom(s, buff, 1024, 0, (sockaddr*)&addr, &nLen);
        if (nRecv > 0)
        {
            buff[nRecv] = '\0';
            cout<<"Received information ("<<inet_ntoa(addr.sin_addr)<<"):"<<buff<<endl;
        }
    }
    ::closesocket(s);
    ::WSACleanup();

    return 0;
}

客户端:

#include <iostream>
#include <winsock2.h>
#pragma comment(lib, "ws2_32")
using namespace std;

int main()
{
    WSADATA wsaData;
    WORD version = MAKEWORD(2, 2);
    if (::WSAStartup(version, &wsaData) != 0)
    {
        exit(0);
    }

    SOCKET s = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (s == INVALID_SOCKET)
    {
        cout<<"Failed socket()\n";
        return 0;
    }

    sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(4567);
    addr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");

    char szText[] = "TCP Server Demo!\r\n";
    ::sendto(s, szText, strlen(szText), 0, (sockaddr*)&addr, sizeof(addr));
    closesocket(s);
    ::WSACleanup();
    return 0;
}
目录
相关文章
|
5月前
|
算法 C++
算法笔记:递归(c++实现)
算法笔记:递归(c++实现)
|
5月前
|
编译器 C++
《Effective C++ 改善程序与设计的55个具体做法》 第一章 笔记
《Effective C++ 改善程序与设计的55个具体做法》 第一章 笔记
|
3月前
|
C++ 容器
【C/C++笔记】迭代器
【C/C++笔记】迭代器
26 1
|
3月前
|
存储 安全 程序员
【C/C++笔记】迭代器范围
【C/C++笔记】迭代器范围
68 0
|
4月前
|
C++ Windows
FFmpeg开发笔记(三十九)给Visual Studio的C++工程集成FFmpeg
在Windows上使用Visual Studio 2022进行FFmpeg和SDL2集成开发,首先安装FFmpeg至E:\msys64\usr\local\ffmpeg,然后新建C++控制台项目。在项目属性中,添加FFmpeg和SDL2的头文件及库文件目录。接着配置链接器的附加依赖项,包括多个FFmpeg及SDL2的lib文件。在代码中引入FFmpeg的`av_log`函数输出"Hello World",编译并运行,若看到"Hello World",即表示集成成功。详细步骤可参考《FFmpeg开发实战:从零基础到短视频上线》。
168 0
FFmpeg开发笔记(三十九)给Visual Studio的C++工程集成FFmpeg
|
6月前
|
存储 C++ 容器
黑马c++ STL部分 笔记(7) list容器
黑马c++ STL部分 笔记(7) list容器
|
6月前
|
算法 C++ 容器
黑马c++ STL常用算法 笔记(5) 常用算术生成算法
黑马c++ STL常用算法 笔记(5) 常用算术生成算法
|
6月前
|
算法 C++ 容器
黑马c++ STL常用算法 笔记(4) 常用拷贝和替换算法
黑马c++ STL常用算法 笔记(4) 常用拷贝和替换算法
|
6月前
|
存储 算法 搜索推荐
黑马c++ STL常用算法 笔记(3) 排序算法
黑马c++ STL常用算法 笔记(3) 排序算法
|
6月前
|
算法 C++
黑马c++ STL常用算法 笔记(2) 查找算法
黑马c++ STL常用算法 笔记(2) 查找算法