Sir, I am sending some string using TCP socket to another system. I have taken 8 buttons. On click of each button a different string is sent to the same IP and PORT using same socket. Socket is connected to specified IP once at the startin开发者_运维百科g. Now in the second system in am analysing the packet(using some packet analyzer), but i could not find data properly. Problem:If i click one button and analyse the packet the its fine. i found packet and data part seperately. its quit fine. BUt if i click 3 or 4 buttons and after that analyse the packet in packet analyser than i found all the strings(from clicked buttons) connected under data part while there is only one header part. There must be different packets for different clicks. i mean each string must be encapsulated in different packet. What can be the problem? Please reply... Thank you.....
No, tcp is a stream protocol. You put data in in chunks at one end, but the boundaries between these chunks are likely to disappear when the data arrives at the remote end.
If you must use tcp, then you will have several choices.
- Prefix each chunk of data with a header that includes the length of the data, flush the transmitting queue, read just the header at the remote end and use that to further read the data.
- Make a new tcp connection for every packet (rather like basic http protocol).
- Make every chunk the same size (40 bytes say) and read exaxctly that at the remote end.
BTW, you probably need to ensure there is only one writing thread per socket.
精彩评论