如何从tcp套接字winsock2自动获取数据 [英] How to get data automatically from tcp sockets winsock2

查看:92
本文介绍了如何从tcp套接字winsock2自动获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从tcp套接字winsock2自动获取数据。目前我正在使用计时器接收数据,当它在套接字上可用时。请有人帮助我。



我现在的代码



tcp.h

 ifndef TCP_H 
#define TCP_H

#pragma once
#include < winsock2.h >
#include < ws2tcpip.h >
#include < iostream < span class =code-keyword>>
#include config.h
#include tcp_packets.h
#include change_ending.h
#include md5.h
#pragma comment(lib,Ws2_32.lib)

class CTcpClient
{
private
char * szServerName;
char *端口;
SOCKET ConnectSocket;
md5checksum Md5Var;

public
int 大小;
bool 发​​送( char * szMsg, int len);
CTcpClient( char * servername, char * port)
{
端口=端口;
szServerName = servername;
ConnectSocket = INVALID_SOCKET;
Size = 0 ;


}

bool Start();
void Stop();
// 免费资源




// 从服务器接收消息
char recvbuf [DEFAULT_BUFFER_PKT_LENGTH];
void Recv();


};
#endif // TCP_H



tcp.cpp

< pre lang =c ++> #include tcp.h
#include config.h

bool CTcpClient :: Start(){
WSADATA wsaData;

// 初始化Winsock
int iResult = WSAStartup(MAKEWORD( 2 2 ),& WSADATA);
if (iResult!= 0
{
printf( WSAStartup失败:%d \ n,iResult);
return false ;
}

struct addrinfo * result = NULL,
* ptr = NULL,
hints;

ZeroMemory(&提示, sizeof (提示));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
// hints.ai_protocol = SOL_SOCKET;
// 解析服务器地址和端口
iResult = getaddrinfo(szServerName,Port,& hints,& result) ;
if (iResult!= 0
{
printf( getaddrinfo failed:%d \ n,iResult);
WSACleanup();
return false ;
}

解决方案

您可以循环调用recv。当没有数据可用时它会阻塞,只有在收到数据或发生套接字错误时才会返回。



如果你的程序还有其他的并行操作你正在等待下一个数据包到达,然后你应该在一个单独的线程中运行接收循环。



查看定时器回调中的套接字是穷人man的解决方案可能适用于您不希望引入多线程设计复杂性的简单应用程序。


I want to get data automatically from tcp sockets winsock2. Currently i am using a timer to receive data when its available on socket .Please somebody help me .

my current code

tcp.h

  ifndef TCP_H
    #define TCP_H

    #pragma once
    #include <winsock2.h>
    #include <ws2tcpip.h>
    #include <iostream>
    #include "config.h"
    #include "tcp_packets.h"
    #include "change_ending.h"
    #include "md5.h"
    #pragma comment(lib, "Ws2_32.lib")

    class CTcpClient
    {
    private:
    char* szServerName;
    char*   Port;
    SOCKET ConnectSocket;
    md5checksum Md5Var;

    public:
        int Size;
        bool Send(char* szMsg,int len);
        CTcpClient(char* servername,char* port)
        {
            Port=port;
            szServerName = servername;
            ConnectSocket = INVALID_SOCKET;
            Size=0;


        }

        bool Start();
        void Stop();
        // Free the resouces




        // Receive message from server
         char recvbuf[DEFAULT_BUFFER_PKT_LENGTH];
        void Recv();


    };
#endif // TCP_H


tcp.cpp

#include "tcp.h"
#include "config.h"

bool CTcpClient::Start() {
        WSADATA wsaData;

        // Initialize Winsock
        int iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
        if(iResult != 0)
        {
            printf("WSAStartup failed: %d\n", iResult);
            return false;
        }

        struct addrinfo *result = NULL,
                        *ptr = NULL,
                        hints;

        ZeroMemory(&hints, sizeof(hints));
        hints.ai_family = AF_UNSPEC;
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_protocol = IPPROTO_TCP;
        //hints.ai_protocol = SOL_SOCKET;
        //Resolve the server address and port
        iResult = getaddrinfo(szServerName,Port, &hints, &result);
        if (iResult != 0)
        {
            printf("getaddrinfo failed: %d\n", iResult);
            WSACleanup();
            return false;
        }

解决方案

You may call recv in a loop. It will block when no data are available and return only when data has been received or a socket error has occurred.

If your program has other things to do in parallel while you are waiting for the next data package to arrive, then you should run the receive loop in a separate thread.

Looking at the socket in a timer callback is the poor man's solution and may work for simple applications into which you do not want to introduce the complexities of a multi-threaded design.


这篇关于如何从tcp套接字winsock2自动获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆