nullox
nullox

Reputation: 395

TCP listener only receive the first message

I wrote a TCP server to receive the messages from multiple iOS clients. However, it only receives the first TCP message and stops receiving the subsequent messages.What is the problem?

            int recv;
            byte[] data = new byte[1024];
            IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 8060);
            newsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPAddress ipadr = IPAddress.Parse("10.10.10.10");
            System.Net.IPEndPoint EndPoint = new System.Net.IPEndPoint(ipadr, 8060);
            newsock.Bind(EndPoint);
            newsock.Listen(255);

            while (true)
            {
                Socket client = newsock.Accept();
                recv = client.Receive(data,SocketFlags.None);
                if (recv == 0)
                    break;
                string receivedText = Encoding.ASCII.GetString(data, 0, recv);
                Console.Write(receivedText);
            }

            newsock.Close();

Upvotes: 1

Views: 2234

Answers (1)

HABO
HABO

Reputation: 15816

You have the Accept inside the loop. You only want to accept any given connection once, then loop on the Receive to get the data.

If you want to be able to handle multiple clients at the same time it becomes a little more complicated. One approach is to create a thread for each client. This allows you to continue to use a blocking Receive and lets each progress more or less independently. This may be helpful when the processing for each message takes a significant amount of time and the number of clients is reasonably small. A different approach is to use non-blocking I/O. You can set Blocking to false so that the Receive completes immediately, even if no data is available, and then loop through all of your active connections checking for new data. Or you can use BeginReceive and perform the operations asynchronously. Note that even if you perform all of the receives in a single thread, you can still process them in multiple threads by depositing the received messages in a work queue and using a pool of worker threads to process the data.

You used the term message as if to imply that the received data has some sort of structure larger than a byte. Prepare to be disappointed. TCP guarantees that the bytes pushed in at one end of a connection will appear at the other end in the same order and without duplication. There is no guarantee about how they are grouped, i.e. if you put in six 500-byte "messages" you may receive a single 3000-byte block or any assortment of smaller groupings that add up to 3000 bytes. A state machine can be quite useful for reassembling messages from fragments. I have generally used a fixed-length message header that includes the overall message length to simplify the process. Once you have enough bytes to have a complete message header then you can process the header. That gives you the overall message length so that you can extract it from the buffer or wait until the remainder is received.

Upvotes: 3

Related Questions