2

I am making a .NET application which is supposed to communicate with a Java application over the sockets. Here is the .NET code:

string str = "  MSG1234";
TcpClient client = new TcpClient();
client.Connect("127.0.0.1", 8080);
byte[] msg = Encoding.UTF8.GetBytes(str);
client.Client.Send(msg);
StreamWriter sw = new StreamWriter(client.GetStream());
StreamReader sr = new StreamReader(client.GetStream());
while (true)
{
     Console.WriteLine(sr.Read());
}

The problem is that the Java application wouldn't send the answer. The application is a 3rd party piece of software and I can't change it, however I have downloaded a decompiler and found the relevant piece of code in the Java class:

try {
   while (this.is.available() <= 0);
   body = new byte[this.is.available()];
   this.is.readFully(body);
   System.out.println("Message received");
} catch (Exception e) {
   System.out.println("Error: " + body);
}

"is" in this case is a DataInputStream. Here is what happens: 1. I create a socket and send the message 2. I wait for response, nothing happens 3. I close the app manually - it causes the socket to expire 4. Suddenly the whole message appears in the Java application's log. That means the exception happened and there actually is something in the body.

Can you perhaps point me out where the error could be? I believe the this.is.readFully(body) line shouldn't be there, but I'm not sure. Perhaps I should send an EOF from the C# code manually, but I wasn't able to find out how.

Another, minor problem with that code is, that the first two characters of the message are stripped away, that's why I have included the two space in front of the actual message.

Thank you in advance.

EDIT

So I have tried to fill the socket with some random data and I've finally got the answer:

for (int i = 0; i < 600; i++) 
{
    sw.Write("some long random string"); 
    sw.Flush(); 
}

This not a good solution though, because the message I send has to be exact. Then I have tried to close the socket after relevant data has been sent by soc.Disconnect(false); which causes the Java application's log to fill with proper debug information. Is there any way to send EOL to the socket so the Java app would stop listening and would start sending the data? Thank you.

EDIT 2 I have tried to create a Java client to connect to the server, the same thing has happened.

Then I have created a dummy server to listen on the same port as the Java app I'm connecting too, it has also behaved the way as the Java app should and it was working.

Now I feel that my only chance is to send EOT or EOF command to the stream, but I have no idea how to do it on .NET and I wasn't able to find the answer on the internet either.

4
  • I think the problem might be assuming a byte written on one side will appear instantaneously on the other side. Strong recommendation: read Beej's Guide to Sockets Programming Commented Feb 25, 2013 at 23:22
  • 1
    Have you tried calling the <code>Flush</code> method after the Send? I think you may be fighting Nagle's algorithm if the chunk of data you're sending is quite small. Commented Feb 25, 2013 at 23:25
  • If the other application doesn't answer yours, chances are you're doing something wrong. What protocol does the application use, are you sending a valid message? How did you decide what to send, how do you indicate you're done sending? Commented Feb 25, 2013 at 23:40
  • I have tried StreamWriter with either WriteLine or Write and flushing after, it did not help either. What I have tried however is this: for (int i = 0; i < 600; i++) { sw.Write("some long random string"); sw.Flush(); } and it provokes a response from the server. I have to probably fill the whole buffer or something. It's really weird. Commented Feb 26, 2013 at 6:02

2 Answers 2

1

If the Java application is from a third party, chances are that you're doing something wrong. The DataInputStream.readFully(byte[]) function block the application until it has read the number of bytes that the byte array can hold, so the snippet of code you have is from the read operation.

I also see that you use the Socket.Send(byte[]) function to communicate with the Java application, I recommend you to use something higher level like StreamWriter or BinaryWriter, more less like this:

StreamWriter = new BinaryWriter(client.GetStream());
StreamWriter.Write(msg);
StreamWriter.Flush();
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried StreamWriter with either WriteLine or Write and flushing after, it did not help either. What I have tried however is this: for (int i = 0; i < 600; i++) { sw.Write("some long random string"); sw.Flush(); } and it provokes a response from the server. I have to probably fill the whole buffer or something. It's really weird.
0

Can you try to flush both the StreamWriter (so it writes its buffer to the stream object) and the actual NetworkStream you got from client.GetStream() (so it sends a packet, despite the packet not being "full" yet)?

NetworkStream ns = client.GetStream();
StreamWriter sw = new StreamWriter(ns);
StreamReader sr = new StreamReader(ns);
// ...
sw.Flush();
ns.Flush();

Most likely, you are not flushing all that you need to flush. Have you tried looking at the network communication with e.g. wireshark - is the data actually going out? If not, the problem is in your .NET code.

4 Comments

I have tried rawcap since it happens on the loopback and yes, the data is actually being transferred properly. I have also tried the NetworkStream flush and it didn't help.
Then try writing a custom Java listener next, and see if that works.
I have done that. First I have created a Java client for the server, it showed the same issue. Then I have created a Java server and connected with the same .NET code and it was working.
So, bad news is that apparently the server has a bug that is a mess to work around...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.