linux - Receiving binary data over socket in c++ -
I have an MCU unit connected to the local network. This device is sending data of 100 times per second to the TCP socket. I can also issue commands and data on the same socket.
I am trying to write a simple TCP client which can connect to the device, take the data and if the request is sent then command I can send commands without problems (command string And it works). My problem is the reception of data from the device.
I know from the device manual that the data must first be 32-bit integer, then an array of approximately 30 pairs)
I am installing a new thread that I will insert data I want to stop and process it:
void * com :: setListenerSocket (zero * thread socketdata) {struct socketData * sockData; SockData = (socketdata *) threadSocketData; Com :: tcp_client cListener; Structure Timeless Timeout; Timeout.tv_sec = 0; Timeout.tv_usec = 12500; // SetOpP (sealister, IPPROTOPCP, SO_RCVTEMO, (four *) timeout, size (timeout)); // an effect cListener.connect (sockData-> host, sockData-> port); Cout & lt; & Lt; "Listener Thread Begins" & lt; & Lt; Endl; Cout & lt; & Lt; "Host =" & lt; & Lt; SockData-> Host & lt; & Lt; "At port =" & lt; & Lt; SockData-> Port & lt; & Lt; Endl; CListener.send_data ("Audience Reporting \ n"); While (listenerRun) {listenerHB ++; Cout & lt; & Lt; Andal & lt; & Lt; "*************************************************************************************************** **************************************************************************************************** ****************************************************************************************************** & Lt; Endl; Four * receive = new four [packet]; CListener.receive (PACKETSIZE, received); Unsigned int test; Received test = (uint32_t); Cout & lt; & Lt; "Receive =" & lt; & Lt; Trial & lt; & Lt; Endl; For (Int i = 0; I and listener is:
void com :: tcp_client :: received (integer size, four * buffer) {unsigned int bytecnt = 0, Collection = 1; While (biotechant & lt; (unsigned integer size)) // bitecant + = rqv (sock, and buffer [bytecuta], size biotech, 0); Biotechant + = rquv (sock, buffer, size-biotech, MSG_Vatol); Unsigned int test; Test = buffer [0]; Cout & lt; & Lt; "Value =" & lt; & Lt; Trial & lt; & Lt; "¤¤" & lt; & Lt; Endl; } I took the indicator at the beginning of the buffer and put it in UIT 32_ T, but the result is completely wrong (I know that this packet should be length, but in millions of it Easily goes) When I use the nl command: nc -l 12354 I can write text in my program and everything is correct
Edit: Someone else Was more experienced in a meeting with: Solution: I can not post a solution myself , So I will keep it here if someone encounters such a problem.
At first, I expected that the socket should be handled automatically, where the same transmission is starting and where its end is - which is not true, as Basile Starykekech said. However, TCP ensures that the data is coming in order and nothing has been lost so I can safely believe that 500 bytes should be promised that I will get 500 bytes or whatever.
Secondly, the data is coming into the big end - as Basell told me I made a little change, but still had the wrong result but ...
Finally, type me There was another problem with casting. I was using four * while data was an unsigned four * resolved the issue of decoding * with unsigned cells by changing buffers.
This can be a solution for all those who can interact with local electronic networks communicating with local electronic networks.
I think that if a person has written me up in a clean way, then this matter can be considered closed.
You want to check the TCP client (), you just need to get your C / C ++ program under TCP Client I.e. TCP Client will create your program and open a TCP connection on the server, and pipe the programout of the program to the pipe, and pipe the output to the server's programmers. The good thing about doing this in such a way is that you can give TCP Clinton to take all heavy loads, install socket etc., and you can focus on the main focus of your program.
Comments
Post a Comment