WinSock TCP connection length (while circulating)

Previous program, regardless of the server or the client, there is a problem that a request has been processed immediately quit, there is not much practical significance. Could that have been receiving the client's request it as a Web server? Can, you can use a while loop.

The only caveat here is that every time a client operate after connection are done, they need to be socketclose (), because the server calls closesocket () will not only close the socket server, but also to inform the client connection is disconnected, so the client also needs to direct socketclose () operation

Server-side code:

#include<winSock2.h>
#include<iostream>
#pragma comment(lib, "ws2_32.lib") //添加动态链接库

using namespace std;

int main(int argc, char * argv[]) {
    const int   BUFSIZE = 1024;
    SOCKET      ListeningSocket; //定义一个套接字变量
    SOCKET      NewConnection; //客户端的请求生成的新的套接字
    SOCKADDR_IN ClientAddr;
    SOCKADDR_IN ServerAddr;
    char        Message[BUFSIZE];
    int         ClientAddrLen;
    ZeroMemory(Message, BUFSIZE);

    if (argc <= 1)
    {
        cout << "USAGE: TcpServer <Listen Port>" << endl;
        return -1;
    }


    int ret; //用来检查初始化成功是否
    WSADATA wsaDATA; // 创建一个WSADATA 用来初始化套接字网络库

    if ((ret = WSAStartup(MAKEWORD(2, 2), &wsaDATA)) != 0) { // 初始化套接字网络库WinSock2.2版本
        cout << "WSAStartup初始化失败 with error " << ret << endl;
        return -1;
    }

    //创建套接字 AF_INET协议 SOCK_STREAM流 TCP协议
    if ((ListeningSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR) {
        cout << "创建套接字失败 with error " << WSAGetLastError() << endl;
        WSACleanup();
        return -1;
    }

    //绑定套接字
    ServerAddr.sin_family = AF_INET;
    ServerAddr.sin_port = htons(atoi(argv[1]));
    ServerAddr.sin_addr.S_un.S_addr = htonl(INADDR_ANY);

    if (bind(ListeningSocket, (SOCKADDR *)&ServerAddr, sizeof(SOCKADDR)) == SOCKET_ERROR) {
        cout << "绑定套接字失败 with error " << WSAGetLastError() << endl;
        closesocket(ListeningSocket);
        WSACleanup();
        return -1;
    }

    //监听套接字
    if ((ret = listen(ListeningSocket, 1)) == SOCKET_ERROR) {
        cout << "监听套接字失败 with error " << WSAGetLastError() << endl;
        closesocket(ListeningSocket);
        WSACleanup();
        return -1;
    }
    cout << "正在监听端口" << argv[1] << "中..." << endl;


    //接收客户端数据
    ClientAddrLen = sizeof(SOCKADDR);
    while (true) {
        if ((NewConnection = accept(ListeningSocket, (SOCKADDR*)&ClientAddr, &ClientAddrLen)) == INVALID_SOCKET) {//创建一个当前客户端和服务端的套接字,accept会阻塞,后面的不能继续执行,直到有新的请求
            cout << "创建NewConnection失败 with error " << WSAGetLastError() << endl;
            closesocket(ListeningSocket);
            WSACleanup();
            return -1;
        }

        int StrLen = recv(NewConnection, Message, sizeof(Message), 0);
        cout << "客户端 > " << Message << endl;

        send(NewConnection, Message, StrLen, 0);
        closesocket(NewConnection);

        ZeroMemory(Message, BUFSIZE);
    }

    //关闭套接字
    closesocket(ListeningSocket);
    WSACleanup();


    return 0;
}

The client code:

#include<WinSock2.h>
#include<iostream>
#pragma comment(lib, "ws2_32.lib") //添加动态链接库
#pragma warning(disable:4996) //忽略旧函数使用缺陷的警告

using namespace std;

int main(int argc, char * argv[]) {
    const int   BUFSIZE = 1024;
    SOCKET      ClientSocket;
    SOCKADDR_IN ServerAddr;
    char        SendBuf[BUFSIZE];  //发送存储的数据缓冲区
    char        BufRecv[BUFSIZE]; //接收收到的数据缓冲区

    ZeroMemory(SendBuf, BUFSIZE);
    ZeroMemory(BufRecv, BUFSIZE);
    strcpy(SendBuf, "Hello, My Name is Client");

    if (argc <= 2)
    {
        cout << "USAGE: TcpServer <Server IP> <Server PORT>" << endl;
        return -1;
    }

    WSADATA     WSAData;
    int         ret;
    if ((ret = WSAStartup(MAKEWORD(2, 2), &WSAData)) != 0) {
        cout << "WSAStartup初始化失败 with error " << ret << endl;
        return -1;
    }


    //cout << "连接建立成功!" << endl;;

    while (true) {
        //创建套接字
        if ((ClientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == SOCKET_ERROR) {
            cout << "创建套接字失败 with error " << WSAGetLastError() << endl;
            WSACleanup();
            return -1;
        }

        //连接connet服务端
        ServerAddr.sin_family = AF_INET;
        ServerAddr.sin_addr.s_addr = inet_addr(argv[1]);
        ServerAddr.sin_port = htons(atoi(argv[2]));
        if (connect(ClientSocket, (SOCKADDR *)&ServerAddr, sizeof(ServerAddr)) == SOCKET_ERROR) {
            cout << "连接服务端失败 with error " << WSAGetLastError() << endl;
            closesocket(ClientSocket);
            WSACleanup();
            return -1;
        }

        cout << "客户端 > ";
        cin >> SendBuf;

        //send发送数据
        if ((ret = send(ClientSocket, SendBuf, strlen(SendBuf), 0)) == SOCKET_ERROR) {
            cout << "send failed with error " << WSAGetLastError() << endl;;
            closesocket(ClientSocket);
            WSACleanup();
            return -1;
        }

        recv(ClientSocket, BufRecv, BUFSIZ, 0);
        cout << "服务端 > " << BufRecv << endl;

        ZeroMemory(SendBuf, BUFSIZE);
        ZeroMemory(BufRecv, BUFSIZE);

        //关闭套接字
        closesocket(ClientSocket);
    }

    WSACleanup();
    return 0;


}

Guess you like

Origin www.cnblogs.com/zpchcbd/p/12173924.html