Click here to Skip to main content
15,437,301 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i had an structure like this(it is a global structure)
C++
typedef struct MachineList
{
    BSTR AccountId;
    BSTR MachineId;
    
    char* Make;
    
    char* Model;
    BSTR SerialNumber;
    
    char* IpAddress;
    
    int Port;
    BSTR LocationCode;
    SOCKET Sock;
    char* Status;
} MACHINELIST,*PMACHINELIST;


This structure is filled by parsing an XML.and added to an STL list

Then comparing the make and model for each entry in the structure i create an pointer to a class using new operator and call the connect to machine function by passing the structure like this

C++
while(iList!=SS1.end())
{
    MACHINELIST* pList=*iList;
    
    
    if((pList->Status=NO_CONNECTION)||(pList->Status=NOT_ACTIVE))
    {
        if((strcmp(pList->Make1,MAKE)==0)&&(strcmp(pList->Model1,MODEL)==0))
        {
            CConnector *p_CDiamax=new CConnector();
            pList->Sock=p_CDiamax->ConnectToMachine(*pList);
            printf("ip address is %s\n",pList->IpAddress);
            printf(" status is %ls\n",pList->Status);
            
            if(pList->Sock!=NULL)
            {
                pList->Status="Active";
                
            
            }
            printf(" status is %ls\n",pList->Status);
            printf("ip address is %s\n",pList->IpAddress);
        }
    }
    iList++;
}


I want to connect each machine one by one and then pass the pointer created for the machine to which i have connected to call the sendRequest,recvResponse
like this

C++
while(1)
{
    for(int i=0;i<machinecount;i++)
    {
        //make all connection as shown above
    }
    for(int i=0;i<machinecount;i++)
    {
        //pointer created in connect function use that to send request,recv response
    }
    Sleep(20000);
}

now i want to
Posted
Updated 31-Oct-12 3:34am
v3

You may want to google std::for_each.

Hope this helps,
Pablo.
 
Share this answer
 
C++
if((pList->Status=NO_CONNECTION)||(pList->Status=NOT_ACTIVE))

This line is not going to do what you expect.

Where is the pointer that is created in the connect function, and what do you want to use it for?
 
Share this answer
 
There's a couple of things that leap out at me immediately.

Firstly was the pair of assignments inside an if statement that you've since fixed. Next thing I saw was this section of code:
C++
while(iList!=SS1.end())
{
    MACHINELIST* pList=*iList;


    if((pList->Status=NO_CONNECTION)||(pList->Status=NOT_ACTIVE))
    {
        if((strcmp(pList->Make1,MAKE)==0)&&(strcmp(pList->Model1,MODEL)==0))
        {
            CConnector *p_CDiamax=new CConnector();

I'm assuming the last line is the one in which you make the connection and the object you'd like to use later in the send/recv loop. If thats's the case, I wonder two things - (1) wouldn't you need to save this to either (a) a temporary list at the same index as the machine it belongs to or (b) better yet, inside the machine pointed to by pList?
(2) Is that all there is to it - do you need to specify a port number and url now or do you do that in your loop later on?

Lastly, I see in the while(1) loop that you do two things - (1) connect to all of the machines (2) send/receive some data from them all. As it stands, that approach has a memory leak. Every time you run though the code I quoted, you're creating a new connector for each machine. If you need a connector per machine, then just add the member to the MachineList struct, initialize it once each per machine. If, one the other-hand, you just use it as an intermediary - a tool to get a socket for send/recv, then create one on the stack outside the loop and extract the needed information from it for each machine each time through the loop. It's on the stack, so it will delete itself when the func ends. I really don't know - I'm not familiar with CConnector, also there appears to be code missing, as opposed to not producing the expected results. Really can't say further.
 
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