A
A
Arseniy2014-03-23 09:40:37
C++ / C#
Arseniy, 2014-03-23 09:40:37

Why does Socket.Receive hang tightly?

static void Main(string[] args)
        {
            TcpListener myTCP = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8888));
            Console.WriteLine("Запускаю процесс прослушивания...");
            while (true)
            {   
                myTCP.Start(); // Запускаю процесс прослушивания
                if (myTCP.Pending()) // Если есть запрос
                {
                    Console.WriteLine("Ура, найден запрос!");
                    using (Socket myClient = myTCP.AcceptSocket()) // То создаю сокет для этого запроса
                    {
                        if (myClient.Connected) // Открываю сокет
                        {
                            try
                            {
                                Console.WriteLine("Открываю сокет для считывания запроса");
                                string data = "";
                                byte[] sqlQuery = new byte[myClient.ReceiveBufferSize];
                                int bytesRec = myClient.Receive(sqlQuery); // Считываю массив байтов в sqlQuery и возвращаю количество считанных байтов
                                data += Encoding.UTF8.GetString(sqlQuery, 0, bytesRec);
                                Console.Write("Я попал сюда " + data);
                            }
                            catch (SocketException e)
                            {
                                Console.WriteLine("{0} Error Code {1}", e.Message, e.ErrorCode);
                            }
                        }
                    }
                }
            }
        }

int bytesRec = myClient.Receive(sqlQuery); //
The program freezes in this line of code, please tell me how to fix it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2014-03-23
@avorsa

it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question