套接字编程 - 长连接模式

时间:2015-08-26 02:48:46

标签: sockets

如图所示,在长连接模式下,服务器recv并发送,客户端发送和recv.And我的问题是:

  • 我只能在客户端使用send,只需在服务器中使用recv吗?

测试代码: 服务器:

//#include "stdafx.h"
#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")

int main(int argc, char* argv[])
{

    WORD sockVersion = MAKEWORD(2, 2);
    WSADATA wsaData;
    if (WSAStartup(sockVersion, &wsaData) != 0)
    {
        return 0;
    }


    SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (slisten == INVALID_SOCKET)
    {
        printf("socket error !");
        return 0;
    }


    sockaddr_in sin;
    sin.sin_family = AF_INET;
    sin.sin_port = htons(8888);
    sin.sin_addr.S_un.S_addr = INADDR_ANY;//
    if (bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)
    {
        printf("bind error !");
    }


    if (listen(slisten, 30) == SOCKET_ERROR)
    {
        printf("listen error !");
        return 0;
    }


    SOCKET sClient;
    sockaddr_in remoteAddr;
    int nAddrlen = sizeof(remoteAddr);
    char revData[255];
    while (true)
    {
        printf("waitting...\n");
        sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);
        if (sClient == INVALID_SOCKET)
        {
            printf("accept error !");
            continue;
        }
        printf("receive a connection:%s \r\n", inet_ntoa(remoteAddr.sin_addr));
        char * sendData = "R\n";
        //
        while (1)
        {
            int ret = recv(sClient, revData, 255, 0);
            if (ret > 0)
            {
                revData[ret] = 0x00;
                printf(revData);
                //new add code
                //send(sClient, sendData, strlen(sendData), 0);

            }
            else
                break;
        }





        closesocket(sClient);
    }

    closesocket(slisten);
    WSACleanup();
    return 0;
}

客户:

//#include "stdafx.h"
#include <WINSOCK2.H>
#include <STDIO.H>

#pragma  comment(lib,"ws2_32.lib")


int main(int argc, char* argv[])
{
    WORD sockVersion = MAKEWORD(2, 2);
    WSADATA data;
    if (WSAStartup(sockVersion, &data) != 0)
    {
        return 0;
    }

    SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sclient == INVALID_SOCKET)
    {
        printf("invalid socket !");
        return 0;
    }

    sockaddr_in serAddr;
    serAddr.sin_family = AF_INET;
    serAddr.sin_port = htons(8888);
    serAddr.sin_addr.S_un.S_addr = inet_addr("10.92.1.148");
    if (connect(sclient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
    {
        printf("connect error !");
        closesocket(sclient);
        return 0;
    }
    char * sendData = "T\n";
    int cnt = 0;
    char recData[255];
    while (1)
    {

        send(sclient, sendData, strlen(sendData), 0);

        //new add code
        //int ret = recv(sclient, recData, 255, 0);
        //if (ret > 0)
        //{
        //  recData[ret] = 0x00;
        //  printf(recData);
        //}
    }
    printf("cnt=%d\n",cnt);



    closesocket(sclient);
    WSACleanup();
    return 0;
}

我测试时代码是错误的。当我添加新代码时,程序可以运行。新代码是//之后的代码。所以我认为只是在一边发送并且recv in另一方可能是错的。它必须有一个循环。它是对的吗?我必须有一个循环(图片中的请求和响应循环)?

0 个答案:

没有答案