C++网络编程笔记

环境准备

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;
}

你可能感兴趣的:(C++网络编程笔记)