• C/S模型之TCP协议






    服务端:
    // WSASever.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <WinSock2.h> #include <Windows.h> #pragma comment (lib,"wSock32.lib") int _tmain(int argc, _TCHAR* argv[]) { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD(2, 2); err = WSAStartup(wVersionRequested, &wsaData); if (err != 0) { printf("WSAStartup failed with error: %d ", err); return 1; } if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) { printf("Could not find a usable version of Winsock.dll "); WSACleanup(); return 1; } else printf("The Winsock 2.2 dll was found okay "); //程序开始 //创建socket->bing-》listen->accept->recv->send->closesocket SOCKET severSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (severSocket == INVALID_SOCKET) { printf("new socket error!"); } SOCKADDR_IN sockAddr; sockAddr.sin_port = htons(10086); sockAddr.sin_family = AF_INET; //IP地址表示方法 /*方法1: m_addr.sin_addr.S_un.S_un_b.s_b1 = 192; m_addr.sin_addr.S_un.S_un_b.s_b2 = 168; m_addr.sin_addr.S_un.S_un_b.s_b3 = 0; m_addr.sin_addr.S_un.S_un_b.s_b4 = 1; 方法2: m_addr.sin_addr.S_un.S_un_w.s_w1 = (168 << 8) | 192; m_addr.sin_addr.S_un.S_un_w.s_w2 = (1 << 8) | 0; 方法3: m_addr.sin_addr.S_un.S_addr = (1 << 24) | (0 << 16) | (168 << 8) | 192 方法4; service.sin_addr.s_addr = inet_addr("127.0.0.1"); */ //sockAddr.sin_addr.s_addr = inet_addr(INADDR_ANY); sockAddr.sin_addr.S_un.S_un_b.s_b1 = 127; sockAddr.sin_addr.S_un.S_un_b.s_b2 = 0; sockAddr.sin_addr.S_un.S_un_b.s_b3 = 0; sockAddr.sin_addr.S_un.S_un_b.s_b4 = 1; if (bind(severSocket, (sockaddr*)&sockAddr, sizeof(SOCKADDR_IN)) == SOCKET_ERROR) { printf("bind error! %d ", WSAGetLastError()); } //监听1个 if (listen(severSocket, 5) == SOCKET_ERROR) { printf("listen error!%d ", WSAGetLastError()); } //创建一个一客户端连接的socket SOCKET sockLink = accept(severSocket, NULL, NULL); if (sockLink != INVALID_SOCKET) { printf("communication sucess! "); } int nErr=0; while (true) { char pSeverBuff[MAXBYTE] = { 0 }; nErr = recv(sockLink, pSeverBuff, MAXBYTE, 0); printf("%s(%d)%s ", inet_ntoa(sockAddr.sin_addr), sockAddr.sin_port, pSeverBuff); send(sockLink, pSeverBuff, MAXBYTE, 0); //当客户端关闭时,服务端也随之关闭 if (nErr == INVALID_SOCKET) break; } closesocket(severSocket); closesocket(sockLink); WSACleanup(); return 0; }
    客户端
    // WASClient.cpp : 定义控制台应用程序的入口点。
    //
    
    //#include <WinSock2.h>一定要在#include <Windows.h>前面
    
    #include "stdafx.h"
    #include <WinSock2.h>
    #include <Windows.h>
    #pragma comment (lib,"wSock32.lib")
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        WORD wVersionRequested;
        WSADATA wsaData;
        int err;
        
        //版本检测
        wVersionRequested = MAKEWORD(2, 2);
    
        err = WSAStartup(wVersionRequested, &wsaData);
        if (err != 0) {
    
            printf("WSAStartup failed with error: %d
    ", err);
            return 1;
        }
    
        if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) 
        {
            printf("Could not find a usable version of Winsock.dll
    ");
            WSACleanup();
            return 1;
        }
        else
            printf("The Winsock 2.2 dll was found okay
    ");
    
    
        //程序开始
        //创建socket-》连接connect-》发送send-》接受recv-》释放closesocke
    
        SOCKET clientSocket=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (clientSocket == INVALID_SOCKET)
        {
            printf("new socket error!");
        }
    
        SOCKADDR_IN sockAddr;
        //一定要把主机字节序换成网络字节序 并是short类型   htons()
        sockAddr.sin_port = htons(10086);
        sockAddr.sin_family = AF_INET;
    
        //IP地址表示方法
        /*方法1:  m_addr.sin_addr.S_un.S_un_b.s_b1 = 192; m_addr.sin_addr.S_un.S_un_b.s_b2 = 168; m_addr.sin_addr.S_un.S_un_b.s_b3 = 0; m_addr.sin_addr.S_un.S_un_b.s_b4 = 1; 
        方法2:  m_addr.sin_addr.S_un.S_un_w.s_w1 = (168 << 8) | 192; m_addr.sin_addr.S_un.S_un_w.s_w2 = (1 << 8) | 0; 
        方法3:  m_addr.sin_addr.S_un.S_addr = (1 << 24) | (0 << 16) | (168 << 8) | 192*/
    
        sockAddr.sin_addr.S_un.S_un_b.s_b1 = 127;
        sockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;
        sockAddr.sin_addr.S_un.S_un_b.s_b3 = 0;
        sockAddr.sin_addr.S_un.S_un_b.s_b4 = 1;
    
    
        if (connect(clientSocket, (sockaddr*)&sockAddr, sizeof(SOCKADDR_IN)) != SOCKET_ERROR)
        {
            printf("communication sucess!
    ");
        }
    
    
        char pClientBuf[MAXBYTE] = {0};
    
        //请求连接
        while (TRUE)
        {
            gets_s(pClientBuf);
            send(clientSocket, pClientBuf, MAXBYTE, 0);
            
            recv(clientSocket, pClientBuf, MAXBYTE, 0);
            printf("return from sever:%s
    ", pClientBuf);
    
        }
    
        WSACleanup();
        closesocket(clientSocket);
    
        return 0;
    
    }
  • 相关阅读:
    react dva 的 connect 与 @connect
    es6 解构赋值 新认知/新习惯
    从一到面试题了解js异步机制:setTimeout 和 Pronmise
    React.Fragment 的作用:代替div作为外层
    解决dva dispatch yield生成器函数中异常中断,无法继续调用的问题
    vue v-model 与 组件化的表单组件如何沟通
    react 事件绑定的2种常用方式
    React dva 的使用
    gulp#4.0 Did you forget to signal async completion?
    gulp#4.0
  • 原文地址:https://www.cnblogs.com/gd-luojialin/p/7694158.html
Copyright © 2020-2023  润新知