Is this a good way to receive a message from a server?
-
I am developing some software that uses a pretty ancient low-level TCP/IP protocol from (I think) the eighties. I can send messages to the server very reliably, but I am a little unsure about receiving them. I cannot predict when the server will send me a message, so I have this method in an infinite loop. GetNextMessage() blocks the thread until it receives a message, whereupon the message is acted upon, and the loop repeats. Messages are sent at a rate of about maybe 5 or 6 a minute, and I can process them in less than a quarter of a second, so I'm not currently spinning off a new thread when I receive them. This code fragment contains all the relevant symbols and the logic that I am unsure of: /// this is filled earlier in the program private Socket _socket; private const int BufferSize = 4096; /// <summary> /// This byte (hex 02) indicates the start of the message. /// </summary> private const byte RecordStart = 2; /// <summary> /// This byte (hex 03) indicates the end of the message. /// </summary> private const byte RecordEnd = 3; private string GetNextMessage() { byte[] bytesReceived = new byte[BufferSize]; // ReSharper disable PossibleNullReferenceException _socket.Receive(bytesReceived); // ReSharper restore PossibleNullReferenceException if (_socket.Poll(1, SelectMode.SelectRead)) { throw new SocketException((int)SocketError.ConnectionReset); } IEnumerable<byte> bytes = bytesReceived .Where(character => character != RecordStart) .TakeWhile(character => character != RecordEnd); return Encoding.ASCII.GetString(bytes.ToArray()); } My concerns: I am not sure if _socket.Poll() is the correct way to determine if the socket has gone away. It most certainly works and is very necessary, but I'm sure I should be doing something else. I once performed an operation that sent three messages immediately after each other, but I only appeared to receive two of them. It is possible that there is a bug in the product I am developing for (seems likely), but I am worried that the method I have chosen is somehow causing messages to be discarded.
-
Answer:
One thing I do with reading from sockets is separate receiving of the data from what the data contains. Meaning, you may get your information in progressive chunks..... so receive a chunk of data, then see if you have a complete "Message". If not, wait for the next chunk of data. When you have your full "Message" then you can pass that on to whatever wants it. In fact what I have done is abstract the idea of a streaming connection so I can interchange serial ports / tcp sockets / and any other streaming type connection. Have something that receives from a connection, and then do interpreting.
Steve Rukuts at Code Review Visit the source
Other answers
A: 1. Here's a better way to check if a socket has disconnected. bool SocketConnected(Socket s) { bool part1 = s.Poll(1000, SelectMode.SelectRead); bool part2 = (s.Available == 0); if (part1 & part2) return false; else return true; }
Jethro
Related Q & A:
- What is a good way to structure mark-up generating code and avoid the example mess?Best solution by Code Review
- What is a good way to advertise enexpensively for a small business? It is handyman work?Best solution by Yahoo! Answers
- What's a good way to let a girl know you are interested in her?Best solution by Yahoo! Answers
- What is a good way to break in a snowboard?Best solution by answers.yahoo.com
- What is a good way to promote a movie?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.