zoukankan      html  css  js  c++  java
  • windows网络编程-socket

    server部分

    1,Initialize Winsock.
    2,Create a socket.
    3,Bind the socket.
    4,Listen on the socket for a client.
    5,Accept a connection from a client.
    6,Receive and send data.
    7,Disconnect.
    #ifndef WIN32_LEAN_AND_MEAN
    #define WIN32_LEAN_AND_MEAN
    #endif
    
    #include <windows.h>
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <iphlpapi.h>
    #include <stdio.h>
    
    #pragma comment(lib, "Ws2_32.lib")
    
    int main()
    {
        //Initializing Winsock
        WSADATA wsaData;
        int iResult;
        iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
        if (iResult != 0)
        {
            printf("WSAStartup failed: %d
    ", iResult);
            return 1;
        }
    
        //Creating a Socket for the Server
        struct addrinfo* result = NULL, * ptr = NULL, hints;
    
        ZeroMemory(&hints, sizeof(hints));
        hints.ai_family = AF_INET;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
        hints.ai_flags = AI_PASSIVE;
    
        iResult = getaddrinfo(NULL, "20000", &hints, &result);
        if (iResult != 0)
        {
            printf("getaddrinfo failed: %d
    ", iResult);
            WSACleanup();
            return 1;
        }
    
        SOCKET listenSocket = INVALID_SOCKET;
        listenSocket = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
    
        if (listenSocket == INVALID_SOCKET) {
            printf("Error at socket(): %ld
    ", WSAGetLastError());
            freeaddrinfo(result);
            WSACleanup();
            return 1;
        }
    
        //Binding a Socket
        iResult = bind(listenSocket, result->ai_addr, (int)result->ai_addrlen);
        if (iResult == SOCKET_ERROR) {
            printf("bind failed with error: %d
    ", WSAGetLastError());
            freeaddrinfo(result);
            closesocket(listenSocket);
            WSACleanup();
            return 1;
        }
    
        freeaddrinfo(result);
    
        //Listening on a Socket
        iResult = listen(listenSocket, SOMAXCONN);
        if (iResult == SOCKET_ERROR) {
            printf("Listen failed with error: %ld
    ", WSAGetLastError());
            closesocket(listenSocket);
            WSACleanup();
            return 1;
        }
    
        //Accepting a Connection
        SOCKET clientSocket = INVALID_SOCKET;
        clientSocket = accept(listenSocket, NULL, NULL);
        if (clientSocket == INVALID_SOCKET) {
            printf("accept failed: %d
    ", WSAGetLastError());
            closesocket(listenSocket);
            WSACleanup();
            return 1;
        }
    
        //Receiving and Sending Data on the Server
        #define DEFAULT_BUFLEN 512
    
        char recvbuf[DEFAULT_BUFLEN];
        int  iSendResult;
        int recvbuflen = DEFAULT_BUFLEN;
    
        // Receive until the peer shuts down the connection
        do {
    
            iResult = recv(clientSocket, recvbuf, recvbuflen, 0);
            if (iResult > 0) {
                printf("Bytes received: %d
    ", iResult);
    
                // Echo the buffer back to the sender
                iSendResult = send(clientSocket, recvbuf, iResult, 0);
                if (iSendResult == SOCKET_ERROR) {
                    printf("send failed: %d
    ", WSAGetLastError());
                    closesocket(clientSocket);
                    WSACleanup();
                    return 1;
                }
                printf("Bytes sent: %d
    ", iSendResult);
            }
            else if (iResult == 0)
                printf("Connection closing...
    ");
            else {
                printf("recv failed: %d
    ", WSAGetLastError());
                closesocket(clientSocket);
                WSACleanup();
                return 1;
            }
    
        } while (iResult > 0);
    
        //Disconnecting the Server
        // shutdown the send half of the connection since no more data will be sent
        iResult = shutdown(clientSocket, SD_SEND);
        if (iResult == SOCKET_ERROR) {
            printf("shutdown failed: %d
    ", WSAGetLastError());
            closesocket(clientSocket);
            WSACleanup();
            return 1;
        }
    
        return 0;
    }

    client部分

    1,Initialize Winsock.
    2,Create a socket.
    3,Connect to the server.
    4,Send and receive data.
    5,Disconnect.
  • 相关阅读:
    构建之法阅读笔记01
    软件工程个人作业01
    第一个PSP0级
    java实现课表的增加
    软件工程概论01
    异常处理
    流与文件课件课后作业1计算容量
    第九周课堂测试
    第八周动手动脑
    JAVA项目中常用的异常知识点总结
  • 原文地址:https://www.cnblogs.com/a-s-m/p/12268228.html
Copyright © 2011-2022 走看看