Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
C++
  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
C++
#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;
        }
Posted
Updated 17-Sep-13 20:53pm
v2
Comments
chaau 18-Sep-13 2:17am    
Sockets aside, Are you sure that these lines are ok:
Port=port;
szServerName = servername;
Didn't you want to initialise Port and szServerName as char[some number] use strcpy instead?
vr999999999 18-Sep-13 2:28am    
code is perfect and its working fine.The question is not about error in code.
chaau 18-Sep-13 2:31am    
Please read about and google for examples for the http://msdn.microsoft.com/en-us/library/windows/desktop/ms741540%28v=vs.85%29.aspx WSAAsyncSelect function. It works by the way of callbacks. I believe this is what you are after
Richard MacCutchan 18-Sep-13 3:21am    
What do you mean by "get data automatically"? You need to check the connection for data and then receive it. Google will find you plenty of samples.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900