2
votes

I'm trying creating a program that allows users to upload JAR Files for some third-party code they've written to an online server and then receive a String message back in response.

The online server is coded using Java with standard TCP Socket networking. The client uses a piece of additional software, which means I have to use C# for the File uploader. The code I've written is included below. In this program the File uploader works fine, but for some reason the client hangs when it reaches input.ReadLine() where it is supposed to receive the String message response from the server.

public static string sendFile(string filepath) {

    String response = "";

    // Get the file
    Stream fileStream = File.OpenRead(filepath);

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

    try {

        // Create the connection
        TCPClient client = new TCPClient("127.0.0.1", 21000);

        NetworkStream stream = client.GetStream();

        // Send the file to the server
        stream.Write(buffer, 0, buffer.Length);
        stream.Flush();

        // Receive a single string response from the server
        if (stream.CanRead) {

            StreamReader input = new StreamReader(stream);
            inputString = input.ReadLine();

        }

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

    }

    catch (IOException e) {

        print ("Error: " + e);

    }

    // Return the response message string
    return inputString;

}

I have also tried implementing the code above using a StreamWriter rather than writing directly from the NetworkStream itself. Unfortunately the StreamWriter class doesn't have a method for sending a byte array, (only a char array). I'm wondering whether the problem is being caused by the fact I'm calling the Write method of the NetworkStream directly rather than using a StreamWriter.

If anybody has any idea why the code above isn't working then please let me know. Alternatively, if you have a different solution that would allow me to send a file (byte array) and receive back a string message using the same TCPClient connection then please also feel free to mention it.

Regards,

Midavi.

4
StreamReader.ReadLine expects a Line Feed, Carriage Return or a combination. Does the server sends this? - Simon Mourier

4 Answers

1
votes

The readline hangs because it will only return when it has successfully read a line from the server, this is the disadvantage of using blocking sockets. Please make sure your server is accually sending a line( string ending with "\n"

1
votes

Is your stream terminated with an end of line? Readline will block until the stream ends or receives the end of line character. If your uploader doesn't terminate the string it could act like you're saying.

0
votes

Checkout networkComms.net, an open source C# network communication library. A short example demonstrating the most basic functionality here, hopefully not overly complex! Most of the problems you might come across will already have been solved and it might save you some time.

0
votes

Readline wait for end of line \r\n but in java the end of line is by default \n only. This can be the difference that can block your comunication, but to be sure you need to snoop the network activity or you can use a different method to read data.

You can try to use the read method:

byte[] myReadBuffer = new byte[512];
int numberOfBytesRead = 0;

// Incoming message may be larger than the buffer size.
do{
    numberOfBytesRead = stream.Read(myReadBuffer, 0, myReadBuffer.Length);  
    // do something with data in myReadBuffer
}
while(stream.DataAvailable);