Click here to Skip to main content
15,889,795 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I coding the soft connect client to server by thread and Tcp Socket. But in the loop while(true) the code is not run, it's just "Not Responding".
I use win7 (64 bit) and Net FrameWork 4
C#
TcpListener serverListener;
        TcpClient client;
        ArrayList listClient = new ArrayList();


        private void btnStart_Click(object sender, EventArgs e)
        {
            label2.Text = "Started";
            serverListener = new TcpListener(System.Net.IPAddress.Any, 1000);
            serverListener.Start();
            
            while (true)
            {
                while (!serverListener.Pending())
                {
                    Thread.Sleep(1000);
                }
               
                //HandleClient handleClient = new HandleClient();
                //handleClient.start(client);                   
                new Thread(connecting).Start();
                
            }
            //client.Close();
            //serverListener.Stop();
        }
        public void connecting()
        {
            TcpClient clientConnecting = serverListener.AcceptTcpClient();
            byte[] bytesFrom = new byte[10025];
            string dataRecieved;
            while (true)
            {
                NetworkStream nkStream = clientConnecting.GetStream();
                nkStream.Read(bytesFrom, 0, (int)clientConnecting.ReceiveBufferSize);
                dataRecieved = System.Text.Encoding.ASCII.GetString(bytesFrom);
                dataRecieved = dataRecieved.Substring(0, dataRecieved.IndexOf("$"));
                txtMess.Text += dataRecieved + "   ";
            }
        }
Posted

Take a look at my article WCF Killer it has all you need and it works.
 
Share this answer
 
Comments
Nguyen Trong Hoai 24-Sep-11 3:35am    
Thanks for your rely. But your code i dont understand, and it cant run. So can you tell me, what's wrong with my code?
Thanks
Mehdi Gholam 24-Sep-11 3:50am    
Strange, if it didn't run I would be overwhelmed with comments.
Nguyen Trong Hoai 24-Sep-11 4:31am    
Um, when i open your solution, it says: .csproj is not supported by this version of the application (i using Visual studio 2010). And when i onpen .cs, i cant run.
Mehdi Gholam 24-Sep-11 4:41am    
Open the .sln file.
Nguyen Trong Hoai 24-Sep-11 4:42am    
Yes i know :).
I know the solution now.
the while(true) cant use like that. It should use in the Thread.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900