Help needed to understand how to deal with TCP connections

Hi,

Anyone here can explain how this code works ?

I’m using ofxTCPClient.

Protocol is PJLINK here. Seems to work but as i will need to use other kind of messages and protocols i would like to know how this works :

Steps :

  • Connect to an IP on port 4352 (here with setup(IP, PORT):wink:
  • server answers PJLINK 0 as acknowledgement
  • then i can send command like “%1POWR ?” to query power status
  • after 2 seconds max i should have the answer “%1POWR=1” or “%1POWR=0” as power ON or Power OFF states.

The code i actually have :

NOTE : i commented the lines i get the meaning of but can’t deeply understand.

  
  
	string msgRx;  
	  
	if(!connected) {  
		connected = pjClient.setup("192.168.5.225", 4352);  
		cout << "connection established: 192.168.5.225 port 4352" << endl;  
		while (msgRx.length() < 8)                          // WHY < 8 ?  
		{  
			msgRx = pjClient.receiveRaw();  
			if(msgRx.length() > 6)                        // WHAT IS THIS CONDITION FOR ?  
			{  
				cout<< msgRx << endl;  
			}  
		}  
	}  
	else if(!pjClient.isConnected()) {  
		cout << "connection failed for 192.168.5.225 on port 4352" << endl;  
		connected = false;  
	}  
  
	cout << "sending command: %1POWR ?" << endl;  
	string command = "%1POWR ?\r";  
	pjClient.sendRaw(command);  
	msgRx = "";  
	//for(int i=0; i<200000000;i++){}  
	while (msgRx.length() < 8)                               // SAME QUESTIONS AND WHY IT DIFFERS FROM ABOVE ?  
	{  
		msgRx = pjClient.receiveRaw();  
		cout<< msgRx << endl;  
	}  
	  
	pjClient.close();  
	connected = false;  
}  
  
  

NOTE : when connection is initialized i get the message “PJLINK 0”.
Then i send the command but i also get the answer “PJLINK 0”.
When i wait for a while before reading the answer i get my answer “%1POWR=1” but i used some tricks (empty loop) as i couldn’t make pause(), delay() or sleep() to work : the app freezes !
What’s the clean way to make a pause in oF ?

Thank you for your help.