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(); }
IsBackground
thdListener.IsBackground = true;
thread.abort();
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)