You can never know ahead of time what will come in on a socket. The socket is buffered by the OS drivers and the only way to read "up until a point" is to read in data in segments (packets), find the point you want and truncate the rest (or ignore it). Hopefully, you are using TCP (vs. UDP) so the packet will arrive more likely intact. Read the packet in (you should know it's size perhaps with a peek) and split(/1234/,$buffer,1); on the data. The returned data is the data is relevant to you.