zoukankan      html  css  js  c++  java
  • MFC学习 socket

    下面未处理异常

    tcpserver.cpp

    #include "WinSock2.h"
    #include <stdio.h>
    #pragma comment(lib, "ws2_32.lib")
    #define PORT 1666
    int main()
    {
        WORD wVersion = MAKEWORD(2, 2);
        int err;
        WSADATA wsaData;
        err = WSAStartup(wVersion, &wsaData);
        SOCKET sockSrv = socket(AF_INET, SOCK_STREAM, NULL);
        sockaddr_in addSrv;
        addSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //或用inet_addr("192.168.1.1");
        addSrv.sin_family = AF_INET;
        addSrv.sin_port = htons(PORT);
        bind(sockSrv, (sockaddr FAR *)&addSrv, sizeof(addSrv));
        int iResult = listen(sockSrv, SOMAXCONN);//等待连结数, 最大连接请求数 SOMAXCONN
        printf("server ip:%s
    ", inet_ntoa(addSrv.sin_addr));
        printf("server port:%d
    ", ntohs(addSrv.sin_port));
        printf("start listening...
    ");
    
        while(TRUE)
        {
            sockaddr_in addClient;
            int iLen = sizeof(addClient);
            SOCKET sockConn = accept(sockSrv, (sockaddr FAR *)&addClient, &iLen);
            printf("client ip:%s
    ", inet_ntoa(addClient.sin_addr));
            printf("client port:%d
    ", ntohs(addClient.sin_port));
            char sendBuf[100];
            sprintf(sendBuf, "Welcome %s to server", inet_ntoa(addClient.sin_addr));
            send(sockConn, sendBuf, strlen(sendBuf) + 1, 0); //多写一个字节, 可以多个
            char recvBuf[100];
            recv(sockConn, recvBuf, 100, 0); //tcp用recv, udp用recvfrom
            printf("%s
    ", recvBuf);
            closesocket(sockConn);
        }
        closesocket(sockSrv);
        WSACleanup();
        return 0;
    }

    tcpclient.cpp

    #include "WinSock2.h"
    #include <stdio.h>
    #pragma comment(lib, "ws2_32.lib")
    #define PORT 1666
    int main()
    {
        WORD wVersion = MAKEWORD(2, 2);
        int err;
        WSADATA wsaData;
        err = WSAStartup(wVersion, &wsaData);
        SOCKET sockClient = socket(AF_INET, SOCK_STREAM, NULL);
        sockaddr_in addSrv;
        addSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //服务器地址
        addSrv.sin_family = AF_INET;
        addSrv.sin_port = htons(PORT);
        connect(sockClient, (sockaddr FAR *)&addSrv, sizeof(addSrv));
        char recvBuf[100];
        recv(sockClient, recvBuf, 100, 0);
        printf("%s
    ", recvBuf);
        send(sockClient, "hello123", strlen("hello123") + 1, 0);
        closesocket(sockClient);
        WSACleanup();
        system("pause");
        return 0;
    }

    udpserver.cpp

    #include "WinSock2.h"
    #include <stdio.h>
    #pragma comment(lib, "ws2_32.lib")
    #define PORT 1666
    int main()
    {
        WORD wVersion = MAKEWORD(2, 2);
        int err;
        WSADATA wsaData;
        err = WSAStartup(wVersion, &wsaData);
        SOCKET sockSrv = socket(AF_INET, SOCK_DGRAM, NULL);
        sockaddr_in addSrv;
        addSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //或用inet_addr("192.168.1.1");
        addSrv.sin_family = AF_INET;
        addSrv.sin_port = htons(PORT);
        bind(sockSrv, (sockaddr FAR *)&addSrv, sizeof(addSrv));
    
        printf("server ip:%s
    ", inet_ntoa(addSrv.sin_addr));
        printf("server port:%d
    ", ntohs(addSrv.sin_port));
    
    
        while(TRUE)
        {
            sockaddr_in addClient;
            int iLen = sizeof(addClient);
            char recvBuf[100];
            recvfrom(sockSrv, recvBuf, 100, 0, (sockaddr*)&addClient, &iLen);
            printf("client ip:%s
    ", inet_ntoa(addClient.sin_addr));
            printf("client port:%d
    ", ntohs(addClient.sin_port));
            printf("%s", recvBuf);
        }
        closesocket(sockSrv);
        WSACleanup();
        return 0;
    }

    udpclient.cpp

    #include "WinSock2.h"
    #include <stdio.h>
    #pragma comment(lib, "ws2_32.lib")
    #define PORT 1666
    int main()
    {
        WORD wVersion = MAKEWORD(2, 2);
        int err;
        WSADATA wsaData;
        err = WSAStartup(wVersion, &wsaData);
        SOCKET sockClient = socket(AF_INET, SOCK_DGRAM, NULL);
        sockaddr_in addSrv;
        addSrv.sin_addr.S_un.S_addr = inet_addr("127.0.0.1"); //或用inet_addr("192.168.1.1");
        addSrv.sin_family = AF_INET;
        addSrv.sin_port = htons(PORT);
        sendto(sockClient, "hello", strlen("hello") + 1, 0, (SOCKADDR*)&addSrv, sizeof(addSrv));
        closesocket(sockClient);
        WSACleanup();
        system("pause");
        return 0;
    }

     代码下载

  • 相关阅读:
    备忘: Visual Studio 2013 VC++ IDE 使用小贴示。
    获取动态数组指针 所指向数组长度的一种方法
    备忘:C++ 类 (初学之实录)。
    备忘:VC++ 中的异常处理
    备忘: C++中的 vector 容器
    备忘:C++基础 -- 数据类型的学习总结
    Windws Server 2008 R2 WEB环境配置之MYSQL 5.6.22安装配置
    Windows Server 2008 R2 WEB服务器配置系列文章索引
    php学习过程二
    php学习过程一
  • 原文地址:https://www.cnblogs.com/barrysgy/p/3224994.html
Copyright © 2011-2022 走看看