简体   繁体   中英

C# <-> Java socket communication

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.

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();

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 eg wireshark - is the data actually going out ? If not, the problem is in your .NET code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM