Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi,

I trying to build an application to exchange files using tcp sockets, I have used a thread to have port listing, the app seems to work fine except that when I close the server the app or the thread hang in memory. please help me to solve this problem..

public void threadHandler()
        {
            Socket handlersocket = (Socket)alSockets[alSockets.Count - 1];
            NetworkStream networkStream = new NetworkStream(handlersocket);
            int thisRead = 0;
            int blockSize = 1024;
            byte[] dataByte = new byte[blockSize];
            lock (this)
            {
                // Only one process can access
                // the same file at any given time
                Stream fileStream = File.OpenWrite(@"c:\upload");
                while (true)
                {
                    thisRead = networkStream.Read(dataByte, 0, blockSize);
          
                    fileStream.Write(dataByte, 0, thisRead);
                    if (thisRead == 0) break;
                }
                fileStream.Close();
                networkStream.Close();
            }
            lBConnections.Items.Add("File Recieved");
            handlersocket = null;
        }
        public void ThreadListener()
        {
            
                TcpListener tcpListener = new TcpListener(8880);
                tcpListener.Start();
                while (true)
                {
                    Socket handlerSocket = tcpListener.AcceptSocket();
                    if (handlerSocket.Connected)
                    {

                        lBConnections.Items.Add(handlerSocket.RemoteEndPoint.ToString() + "..Connected.");

                        lock (this)
                        {
                            alSockets.Add(handlerSocket);
                        }
                        ThreadStart thdstHandler = new ThreadStart(threadHandler);
                     Thread    thdHandler = new Thread(thdstHandler);
                        
                        thdHandler.Start();
                     
                    }

                }                                       
        }

        private void ReceiveFile_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();
            IPHostEntry IPHost = Dns.GetHostByName(Dns.GetHostName());
            lBConnections.Text = "Host IP address is " +
            IPHost.AddressList[0].ToString();
            alSockets = new ArrayList();

          Thread   thdListener = new Thread(new ThreadStart(ThreadListener));
            thdListener.Start();                       
        }
Posted
Updated 8-Jun-11 0:50am
v2

One thing you can do is set the IsBackground property of the thread to true.
C#
thdListener.IsBackground = true;

The thread will stop after the main application ends.
 
Share this answer
 
Comments
Luay Kshedan 8-Jun-11 7:21am    
Thanks that works fine, except that when I send file the app append some garbage to the end of the file, do you know why?
Tarun.K.S 8-Jun-11 7:35am    
Oh I don't know why its coming. Can you provide more details?
BobJanova 8-Jun-11 8:21am    
Only when closing the app? Or always?

By the way I recommend you use the asynchronous socket capabilities of the .Net Framework instead of starting a new thread for each client, particularly as you are synchronising them all anyway.

On which note: don't lock them all on reading. Read the content into memory, or a temporary file on disk with a thread-unique name, and only lock after you have received everything for one client. NetworkStream.Read will block if the connection is interrupted, and because it's inside the lock, it will stall -all- your file threads.
Set the IsBackground property of the helper thread to true. See Thread.IsBackground property.
 
Share this answer
 
Comments
Tarun.K.S 8-Jun-11 7:15am    
Yep that's right. My 5.
You need to terminate the thread by using tis command:

thread.abort();
 
Share this answer
 
Comments
Kubajzz 8-Jun-11 7:14am    
That's not really true... There is a better and more elegant solution, see what Tarun.K.S and I posted...
Thanks for the answer, I try to do that in the Dispose method,like

protected override void Dispose(bool disposing)
{
// thdListener.Abort();
// thdHandler.Abort();
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

but it didn't work..
 
Share this answer
 
Comments
Tarun.K.S 8-Jun-11 7:13am    
Well no, set the IsBackground property to true. See my 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