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 7555417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:36:06+00:00 2026-05-30T11:36:06+00:00

I’ve written a file uploader designed to send a file of program code from

  • 0

I’ve written a file uploader designed to send a file of program code from a C# client to a Java server. The server requires a valid username and password to be submitted prior to accepting the file upload.

Independently both the security (username and password) and the file uploader work correctly. However when I attempt to combine the two the code freezes on the C# client after receiving a ‘true’ boolean response back from the server (indicating a correct username and password). The relevant code from the client and server is attached.

C# Client

public static string sendValidatedFile(string username, string password, string path) {

        string inputString = "NotSent";

        try {

            TcpClient client = new TcpClient("127.0.0.1", 42000);

            StreamReader input = new StreamReader(stream);

            Stream securityStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(username + "_" + password + "_\n"));

            byte[] bufferA = new byte[securityStream.Length];
            securityStream.Read(bufferA, 0, bufferA.Length);

            stream.Write(bufferA, 0, bufferA.Length);
            stream.Flush();

            inputString = input.ReadToEnd();
            bool result = bool.Parse(inputString);

            if (result) {

                print("Login Accepted");

                Stream fileStream = File.OpenRead(path);

                byte[] buffer = new byte[fileStream.Length];
                fileStream.Read(buffer, 0, buffer.Length);

                // This is where the code seems to lock up
                stream.Write(buffer, 0, buffer.Length);
                stream.Flush();

                inputString = input.ReadToEnd();

            }

            else {

                inputString = "Invalid Username or Password";

            }

            input.Close();
            stream.Close();
            client.Close();

        }

        catch (SocketException e) {

            print("Error" + e);

        }

        catch (IOException e) {

            print("Error" + e);

        }

        return inputString;

    }

Java Server

public void run() {

try {

  // Get username and password
    byte[] byteArrayJAR = new byte[filesize];
    byte[] byteArraySecurity = new byte[filesize];
    InputStream input = socket.getInputStream();
    PrintWriter output = new PrintWriter(socket.getOutputStream());

    int bytesSecurity = input.read(byteArraySecurity, 0, byteArraySecurity.length);
    int currentByte1 = bytesSecurity;

    if (input.available() > 0) {

        do {

            bytesSecurity = input.read(
                    byteArraySecurity,
                    currentByte1,
                    (byteArraySecurity.length - currentByte1));

            if (bytesSecurity >= 0) {

                currentByte1 += bytesSecurity;

            }
        }

        while (bytesSecurity > -1);
    }

    String securityString = new String(byteArraySecurity).trim();
    String[] authenticationString = securityString.split("_");
    String username = authenticationString[0];
    String password = authenticationString[1];

  // Validate the username and password with a stored database
    if (security.validateUser(username, password)) {

       // Inforn the client their username and password were accepted
        output.println(true);
        output.flush();

      // Get the program code file
        int bytesRead = input.read(byteArrayJAR, 0, byteArrayJAR.length);
        int currentByte = bytesRead;

       if (input.available() > 0) {

                do {

                   bytesRead = input.read(
                     byteArrayJAR,
                            currentByte,
                            (byteArrayJAR.length - currentByte));

                if (bytesRead >= 0) {

                        currentByte += bytesRead;

                    }
             }

                 while (bytesRead > -1);

     }

      // Inform the client that the code was received
        output.println("Success");
        output.flush();

    }

    else {

      // Inform the client their username or password was incorrect
        output.println(false);
        output.flush();

    }

  // Disconnect from client
    output.flush();
    output.close();
    input.close();
    socket.close();

}

catch (IOException e) {

    e.printStackTrace();

}

}

Can anybody see anything wrong with the above that could be causing my code to hang? There is definately be data for the C# client to transmit on both instances. Is there a reason why two byte arrays can’t be sent from the same Network Stream in C#?

I’d appreciate any help anybody can offer in getting the above code to work without hanging.

Regards,

Midavi.

  • 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-05-30T11:36:08+00:00Added an answer on May 30, 2026 at 11:36 am

    You are using input.ReadToEnd() twice. A stream only has one end; I suspect the first of these is out of place, and should be replaced with more direct reading – or at best, a ReadLine().

    If the conversation here depends on multiple messages between client and server, you might also want to check if nagle is enabled, as it could be that one of the messages is still buffered and has not actually been sent yet (Flush() does nothing on a network stream) – you may have to set NoDelay (or the equivalent) for the socket. The default behaviour is to buffer small messages to prevent flooding the network with lots of tiny packets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from

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.