Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7850587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:54:31+00:00 2026-06-02T18:54:31+00:00

I am new to both C# and to client-server programming. Right now, for class,

  • 0

I am new to both C# and to client-server programming. Right now, for class, I’m attempting to create an FTP client without using any pre-established FTP libraries. I feel like I have the project down for the most part, however I’m running into a problem when I make more than one call that requires use of the data port (list, retr, etc.) Here is a sample of the code that is breaking:

writer.WriteLine(portcmd);
writer.Flush();

GetServerMessage(stream);

writer.WriteLine("list ");
writer.Flush();


tmpserver = new TcpListener(IPAddress.Any, 3128);
tmpserver.Start(); 
tmpclient = tmpserver.AcceptTcpClient();
Console.WriteLine("gothere");

if (!tmpclient.Connected)
{
    tmpserver.Start();
}

StreamReader tmpreader = new StreamReader(tmpclient.GetStream());

GetServerMessage(stream);
while (tmpreader.Peek() != -1)
{
    Console.WriteLine(tmpreader.ReadLine());
}

tmpclient.Close();
tmpserver.Stop();

GetServerMessage(stream);

Getservermessage is a method that takes a network stream and prints out everything available within a .5 second timeout, stream is the NetworkStream for the current connection to the FTP server, and writer is that same network stream wrapped in a StreamReader for ease of writing ASCII characters to the server. In case you are wondering why I use a stream reader to read from the data connection, it is because the server closes the connection after it transmits the data so I could easily get an eof notification. My GetServerMessage method was for some reason breaking when I used the closed network stream.

This code is sending the port command to the FTP server to inform it that I will be requiring a data connection (first 2 lines) Then sending the list command, establishing the data connection to the server, getting the desired information, and then terminating the data connection (the rest of the code).

This code will execute without flaw the first time I run it but if I try it again, it hangs on the ‘tmpclient = tmpserver.AcceptTcpClient();’ line. It never reaches the “gothere” print statement. I believe this is because I am receiving the client from the same machine on the same port but I’m not sure. I tried adding a Boolean value to make sure the AcceptTcpClient() only ran once but then I got a runtime error and visual studio informed me that I may have ‘released resources before I was done with them’ I predicted this would be a problem but how can I tell if the server reestablishes the connection after it has closed it once?

At the end of the given code I stop tmpserver and close tmpclient. I originally did this because I knew the FTP server would close the connection when it was finished transmitting and thought it was the proper thing to do. I find if I comment out these lines, the code will execute more than once but the streams appear to be empty… I’m not sure if this information is helpful but I figured I’d mention it.

I apologize if I am unclear at all but my lack of knowledge with the subject makes it difficult to articulate my problem. If there is any confusion over what the problem is I’d be happy to attempt to clear it up.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:54:32+00:00Added an answer on June 2, 2026 at 6:54 pm

    To be able to accept another client you should execute tmpclient = tmpserver.AcceptTcpClient(); and waiting for the first client to finish its works(before accepting second client) may not be a good idea

    Here is a sample server code that waits for the connections and echoes strings sent from each client. You can test it with telnet localhost 3128

    Thread t = new Thread(Server);
    t.IsBackground = true;
    t.Start();
    

    –

    void Server()
    {
        TcpListener listener = new TcpListener(IPAddress.Any, 3128);
        listener.Start();
        while (true)
        {
            var client = listener.AcceptTcpClient();
            new Thread(() =>
            {
                using (client)
                {
                    var reader = new StreamReader(client.GetStream());
                    var writer = new StreamWriter(client.GetStream());
                    while (true)
                    {
                        string line = reader.ReadLine();
                        if (line == "QUIT") break;
                        writer.WriteLine("From Thread[" + Thread.CurrentThread.ManagedThreadId + "] > " + line);
                        writer.Flush();
                    }
                }
            }).Start();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using socket programming in vb.net to create a server client remoting application.
I've been trying for days now to create an application client server,both on android,running
Right now I have a communication infrastructure compose of a client and server. The
I have to create an application server client in android(both the client an server
I'm currently enrolled in a Client/Server Programming class (i.e. Java 2) in college. For
I'm new to COM programming, and creating a basic native COM server / client
I'm using a TCP library with a Server and Client class called Lacewing http://lacewing-project.org/
I'm fairly new to both Django and Python. This is my first time using
I am new to both DBs and Rails (using 3.2), and I suspect this
I am building a client server application using Java Sockets (in Windows XP). For

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.