Welcome to Dream.In.Code
Become a C# Expert!

Join 150,390 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,072 people online right now. Registration is fast and FREE... Join Now!




Threads and sockets

 
Reply to this topicStart new topic

Threads and sockets, Starting a new thread to receive from a socket

Echilon
4 Mar, 2008 - 02:17 AM
Post #1

New D.I.C Head
*

Joined: 23 Feb, 2007
Posts: 12


My Contributions
I'm working on an FTP client, and using sockets to receive data from a server. When the server sends any data back to the application, I have to wait before the application repsonds. I'd like to starts the transfer for the data in a new thread if possible, leaving the rest of the app free to respond to the user and not just show an hourglass. Is this possible? I'm unsure how to get the data back from the thread.
CODE

            Socket socket = openSocket();
            SendCommand("MLSD");
            if(statusCode != 125 && statusCode != 150) {
                LogText = "Error opening connection\n";
                return;
            }
            DateTime timeOutDate = DateTime.Now.AddSeconds(timeOut);
            bldBuffer.Remove(0, bldBuffer.Length);
            while(DateTime.Now < timeOutDate) {
                int bytes = socket.Receive(buffer, buffer.Length, 0);
                bldBuffer.Append(Encoding.ASCII.GetString(buffer, 0, bytes));
                if(bytes < buffer.Length) {
                    // there weren't enough bytes to fill the buffer, so the stream is finished
                    break;
                }
            }
            socket.Close();

OpenSocket() creates a new socket to the server, and SendCommand() send the specified command over the control connection. The application works, it's just that I have to wait for it to respond.

Thanks for any input.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 05:35PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month