• C++ Linux Window


    绘制五角星

    #include<windows.h>
    #include<math.h>
    #include<stdio.h>
    const double PI = 3.1415926;
     
    LRESULT CALLBACK WndProc(
      HWND hwnd,     
      UINT uMsg,     
      WPARAM wParam,  
      LPARAM lParam  
    );
     
    int WINAPI WinMain(
      HINSTANCE hInstance,     
      HINSTANCE hPrevInstance,  
      LPSTR lpCmdLine,         
      int nCmdShow   
    )
    {
        WNDCLASS wndclass;
     
        wndclass.cbClsExtra = 0;
        wndclass.cbWndExtra = 0;
        wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
        wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        wndclass.hInstance = hInstance;
        wndclass.lpfnWndProc = WndProc;
        wndclass.lpszClassName = "我的窗口";
        wndclass.lpszMenuName = NULL;
        wndclass.style = CS_HREDRAW | CS_VREDRAW;
        RegisterClass(&wndclass);
     
        HWND hwnd;
        hwnd = CreateWindow("我的窗口", "窗口", WS_OVERLAPPEDWINDOW, 
            0, 0, 640, 480, NULL, NULL, hInstance, NULL);
     
        ShowWindow(hwnd, SW_SHOWNORMAL);
        UpdateWindow(hwnd);
     
        MSG Msg;
        while(GetMessage(&Msg, NULL, 0, 0))
        {
            TranslateMessage(&Msg); 
            DispatchMessage(&Msg); 
        }
     
        return 0;
    }
     
    LRESULT CALLBACK WndProc(
      HWND hwnd,     
      UINT uMsg,      
      WPARAM wParam,  
      LPARAM lParam 
    )
    {
        HDC hdc;
        PAINTSTRUCT ps;
        HPEN hpen;
        int r = 100;
        switch(uMsg)
        {
        case WM_PAINT:    
            hdc = BeginPaint(hwnd, &ps);
            hpen = (HPEN)GetStockObject(BLACK_PEN);
            SelectObject(hdc, hpen);
     
            MoveToEx(hdc, 200 , 200 - r, NULL);
            int k;
            for(k=2; k<12; k=k+2)
            {
                LineTo(hdc, (int)(200 + r * cos((90-72*k)*PI/180)), (int)(200 - r * sin((90-72*k)*PI/180)));
            }
     
            EndPaint(hwnd, &ps);
            DeleteObject(hpen);
            break;
     
        case WM_CLOSE:
            DestroyWindow(hwnd);
            break;
     
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
     
        default:
            return DefWindowProc(hwnd, uMsg, wParam, lParam);
        }
        return 0;
    }



    Window

    TCP编程
    // ServerDemo.cpp文件
    #include <winsock2.h>​
    #include <stdio.h>
    #include <windows.h>
    #pragma comment(lib,"WS2_32.lib")
     
    
    int main(int argc, char* argv[])
    {
    ​WSADATA wsaData;
    ​WORD sockVersion = MAKEWORD(2, 0);
    ​::WSAStartup(sockVersion, &wsaData);
    
    ​SOCKET s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    ​if(s == INVALID_SOCKET)
    ​{
    ​​printf("Failed socket() \n");
    ​​::WSACleanup();
    ​​return 0;
    ​}
    ​
    ​sockaddr_in sin;
    ​sin.sin_family = AF_INET;
    ​sin.sin_port = htons(8080);
    ​sin.sin_addr.S_un.S_addr = INADDR_ANY;
    ​if(::bind(s, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
    ​{
    ​​printf("Failed bind() \n");
    ​​::WSACleanup();
    ​​return 0;
    ​}
    ​
    ​if(::listen(s, 2) == SOCKET_ERROR)
    ​{
    ​​printf("Failed listen()");
    ​​::WSACleanup();
    ​​return 0;
    ​}
    ​
    ​sockaddr_in remoteAddr;
    ​int nAddrLen = sizeof(remoteAddr);
    ​SOCKET client;
    ​char szText[] = " 10ServerDemo! \r\n";
    ​while(TRUE)
    ​{
    ​​client = ::accept(s, (SOCKADDR*)&remoteAddr, &nAddrLen);
    ​​if(client == INVALID_SOCKET)
    ​​{
    ​​​printf("Failed accept()");
    ​​​continue;
    ​​}
    ​​printf(" 接受到一个连接 地址为:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
    ​​::send(client, szText, strlen(szText), 0);
    ​​::closesocket(client);
    ​}
    ​::closesocket(s);
    ​::WSACleanup();​
     
    ​return 0;
    }
     
     
     
     
    ///////////////////////////////////////////
    // ClientDemo.cpp文件
    #include <winsock2.h>
    #include <stdio.h>
    #include <windows.h>
    #pragma comment(lib,"WS2_32.lib")
     
    int main(int argc, char* argv[])
    {
    ​WSADATA wsaData;
    ​WORD sockVersion = MAKEWORD(2, 0);
    ​::WSAStartup(sockVersion, &wsaData);
    
    ​SOCKET s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    ​if(s == INVALID_SOCKET)
    ​{
    ​​printf("Failed socket() \n");
    ​​::WSACleanup();
    ​​return 0;
    ​}
    
    ​sockaddr_in servAddr;
    ​servAddr.sin_family = AF_INET;
    ​servAddr.sin_port = htons(8080);
    ​servAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
    ​if(::connect(s, (sockaddr*)&servAddr, sizeof(servAddr)) == -1)
    ​{
    ​​printf("Failed connect() \n");
    ​​::WSACleanup();
    ​​return 0;
    ​}
    
    ​char buff[256];
    ​int nRecv = ::recv(s, buff, 256, 0);
    ​if(nRecv > 0)
    ​{
    ​​buff[nRecv] = '\0';
    ​​printf(" 接收到数据:%s", buff);
    ​}
    ​  
    ​::closesocket(s);
    ​::WSACleanup();​
    ​return 0;
    }


    Linux


    ///服务器端
     
    #include "pch.h"
    #include <iostream>
    #include <WinSock2.h>
     
    int main()
    {
        WORD wVersionRequested;
        WSADATA wsaData;
        int err;
        wVersionRequested = MAKEWORD(1, 1);
        err = WSAStartup(wVersionRequested, &wsaData);
        if (err != 0) {
            return -1;
        }
        if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1) {
            WSACleanup();
            return -1;
        }
        SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, 0);
        SOCKADDR_IN addrSrv;
        addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
        addrSrv.sin_family = AF_INET;
        addrSrv.sin_port = htons(8080);
        bind(sockSrv, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
        listen(sockSrv, 5);
     
        SOCKADDR_IN addrClient;
        int len = sizeof(SOCKADDR);
     
        while (true)
        {
            SOCKET sockConn = accept(sockSrv, (SOCKADDR*)&addrClient, &len);
            char sendbuf[100];
            sprintf(sendbuf, "Welcome %s Chat !", inet_ntoa(addrClient.sin_addr));
            send(sockConn, sendbuf, sizeof(sendbuf)+1, 0);
            char recvbuf[100];
            recv(sockConn, recvbuf, sizeof(recvbuf), 0);
            std::cout << recvbuf << std::endl;
            closesocket(sockConn);
        }
     
        return 0;
    }
    
    //客户端
     #include "pch.h"
    #include <iostream>
    #include <WinSock2.h>
    #include <string.h>
    int main()
    {
        WORD wVersionRequested;
        WSADATA wsaData;
        int err;
        wVersionRequested = MAKEWORD(1, 1);
        err = WSAStartup(wVersionRequested, &wsaData);
        if (err != 0) {
            return -1;
        }
        if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1) {
            WSACleanup();
            return -1;
        }
        SOCKET sockClient = socket(AF_INET, SOCK_STREAM, 0);
        SOCKADDR_IN addrSrv;
        addrSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
        addrSrv.sin_family = AF_INET;
        addrSrv.sin_port = htons(8080);
        connect(sockClient, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
        char recvbuf[100];
        recv(sockClient, recvbuf, sizeof(recvbuf), 0);
        std::cout << recvbuf << std::endl;
        send(sockClient, "This is zhl", strlen("This is zhl") + 1, 0);
        closesocket(sockClient);
        WSACleanup();
        return 0;
    }
  • 相关阅读:
    【VB编程】05.MsgBox与InputBox函数
    【VBA编程】04.使用自定义数据类型
    【VBA编程】03.判断输入年份是否是闰年
    ubuntu终端颜色设置
    常用命令
    adbd cannot run as root in production builds的解决方法
    camera table表编译
    [Linux]history 显示命令执行的时间
    ubuntu下动态链接库的编译和使用实例
    xxx is not in the sudoers file.This incident will be reported.的解决方法
  • 原文地址:https://www.cnblogs.com/dgwblog/p/15627631.html
Copyright © 2020-2023  润新知