0

I want to send a TCPMessage from my Java TCP-Client to my Visual Basic server.

The server uses the BinaryReader.ReadString() function.

http://msdn.microsoft.com/en-US/library/system.io.binaryreader.readstring.aspx

The ReadString function expects a 7bit-length prefix of the size of the string.

Currently this is my code, but the server doesn't recognize the message.

public void sendMessage(String message) { 
     if (out != null &&  !out.checkError()) {
         out.print(message.length());
         out.println(message);
         out.flush(); 
     } 
 }

And I can't change the whole server architecture to make it recognize the string e.g. From ReadLine.

Hope that somebody can help, trying it now for hours.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
mademyday
  • 83
  • 2
  • 9
  • You can't just use Java's binary serialization with .NET, that's the main problem I'm seeing with what you have in mind. Is there any reason you're trying to to use a BinaryReader? – Selali Adobor Sep 25 '14 at 23:11
  • It's an async server sample which can handle disconnected clients. I always had problems on my own server with handling disconnects or reconnects, it always crashed. But this sample i found works and now i already added some more code, which is the reason why i don't want to start from scratch again. And the person i got the sample from was using the binary reader to manage it. I already fixed the problem to send data from java client to VB-server. Now im trying to figure out how to evade using the bufferedreader.readline method to receive data from VB-server. For more see comments below – mademyday Sep 25 '14 at 23:38
  • I am afraid your interpretation of the .net documentation is wrong. See this [other answer](http://stackoverflow.com/q/19710688/697630). – Edwin Dalorzo Sep 26 '14 at 00:05

1 Answers1

0

Your out is probably a PrintWriter, which translates data to text. For instance, out.print() writes the value's length as a string, while ReadString reads the string's length as binary data. For starters, you need to change out to an OutputStream (which writes binary data) rather than a PrintWriter. Another thing is that you need to make sure the character encoding of the string written by Java matches the encoding used by BinaryReader (the default is UTF-8). Note also that ReadString expects the string's length to be encoded in a special format as described in the Write7BitEncodedInt method.

Finally, ReadString doesn't expect a line break to appear at the end of the string (println implicitly adds a line break).

Peter O.
  • 32,158
  • 14
  • 82
  • 96
  • Ah okay, i really appreciate your help mate! I did it now by using the write function 2 times. First: writing the length of the message Second: writing the message itself Now i have one last issue: When im sending data from my VB-Server, it has to end with the .NET Constant vbCrlf (\n) in Java. How can i manage to do it without vbCrlf? The problem is that i need to distinguish the different clients, to send java clients the + Vbcrlf and normal VB clients the message only. – mademyday Sep 25 '14 at 23:15
  • So far i tried it like this, but nothing seems to be received :/. int value=0; // reads to the end of the stream while((value = br.read()) != -1) { // converts int to character char c = (char)value; // prints character System.out.println(c); } – mademyday Sep 25 '14 at 23:23
  • I have edited my answer. By the way, the code you just gave just reads data from Java. I thought you were intending to only send data from Java? – Peter O. Sep 25 '14 at 23:31
  • It's sending the data to VB TCP-Server. Now i need to figure out to receive data from the server without using the readline method, cuz in this case i always need to send vbCrlf from VB to make the Java Client recognize the answer. See previous post how i tried to receive data without using Readline. Btw. "in" is a BufferedReader. – mademyday Sep 25 '14 at 23:34
  • That's a new question that you didn't ask in this page. You should post a new question using the "Ask Question" link at the top of the page. – Peter O. Sep 25 '14 at 23:40
  • Okay, i'll do :). Thanks anyways!! – mademyday Sep 25 '14 at 23:40