Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing Whole Strings Issue  RSS feed

 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there! I am building a networking chat program and i came across a strange bug that's been  irritating me for a few hours no and i can't seem to find the source of it.
Below i will post 3 scripts that my program is using for printing out the string i want and then i will explain my issue a bit more in depth :

Client.java script



ClientWindow.java



Server.java



So, my problem is that when the client wants to disconnect from the server by clicking on the close icon of the clientWindow GUI,a disconnection package is sent to the server and then the server handles that in order to disconnect him.The whole disconnection process seems to work just fine but after this happens i am printing into the console a string named message inside the disconnect method in the Server.java script so that i get all the details about the client that has been disconnected.Oddly enough,the program seems to ignore this part of the string : " (" + c.getID() + ") @ " + c.address.toString() + " : " + c.port + " disconnected from the server." and it only prints out : Client (then the name of the client).I checked for errors but there are none and i could not find any possible reasons in order for this to happen.I checked if i split that actual message string somewhere or print a substring of it but didn't find any of these.If you could help me figure it out,it would be amazing.

(I bet it's a really silly mistake but i cannot detect it).
Thanks in advance everybody !
 
Ron McLeod
Saloon Keeper
Posts: 1600
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the console output - including other console output as well, not just the truncated line.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If using the System.out.println method with data that has some low binary values like 0, the data after the binary 0 may go missing.
 
Fred Kleinschmidt
Bartender
Posts: 560
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the size of the screen? Is it possible that it just isn't wide enough to show the entire message?

As a side note, what happens in disconnect() if the loop terminates without finding the client? The value of the ServerClient variable c will still be null, and then you immediately try to use it by calling c.name, which will throw a NullPointerException.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Kleinschmidt wrote:What is the size of the screen? Is it possible that it just isn't wide enough to show the entire message?

As a side note, what happens in disconnect() if the loop terminates without finding the client? The value of the ServerClient variable c will still be null, and then you immediately try to use it by calling c.name, which will throw a NullPointerException.


The size is more than enough :P and how could the for loop terminate without finding a client since this method will only be called when a client that wants to disconnect calls it?(sorry if i am wrong here)
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:Please post the console output - including other console output as well, not just the truncated line.


Here it is :

Server started on port: 7777
Client ID : 531
Stelios
Client Stelios
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:If using the System.out.println method with data that has some low binary values like 0, the data after the binary 0 may go missing.


Is there a way i can handle that ?
 
Paul Clapham
Sheriff
Posts: 22472
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
stelios papamichael wrote:
Norm Radder wrote:If using the System.out.println method with data that has some low binary values like 0, the data after the binary 0 may go missing.


Is there a way i can handle that ?


Yeah -- don't use System.out.println if you expect to have non-text data in your String. In fact don't even use a String object if the data you're putting into it isn't text.

However I don't think it's necessary to worry about this now, you haven't even made a connection yet as far as I can tell. Work on that part first.
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code can't be compiled for testing because the ServerClient class is missing.

Also there are no main methods for executing the code.  Do you have a test driver with a main?
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:The code can't be compiled for testing because the ServerClient class is missing.

Also there are no main methods for executing the code.  Do you have a test driver with a main?


Everything can be compiled and is running smoothly i just did not include every single script in this post because my problem is not if the project can run but that when it runs a string is not printed out like it should
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way i can handle that ?

Yes.  Find where the low value is being assigned to a String and change the code so that it doesn't happen.

i just did not include every single script in this post because my problem is not if the project can run but that when it runs a string is not printed out like it should

That is ok.  You will have to do the debugging yourself if we can't execute the code to see where the problem is.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
Is there a way i can handle that ?

Yes.  Find where the low value is being assigned to a String and change the code so that it doesn't happen.

i just did not include every single script in this post because my problem is not if the project can run but that when it runs a string is not printed out like it should

That is ok.  You will have to do the debugging yourself if we can't execute the code to see where the problem is.


Here is the ServerClient class, let me know if you need to check any other script :

 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you found if there is a low binary value in a String?  What String is it in?  How did that value get in the String? 
When you find the variable with the bad value, track back in the code to see where it got that bad value.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Have you found if there is a low binary value in a String?  What String is it in?  How did that value get in the String? 
When you find the variable with the bad value, track back in the code to see where it got that bad value.


IDK what all this binary value stuff is, i am kinda of a begginer so that seems greek to me :P . The string is the message inside the disconnect method like i mentioned above but i do not understand why it does not print the whole string .
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what all this binary value stuff is
It is a non-printable value in a character that is part of a String.  Some non-printable characters terminate the rest of the printed line.
For example:

Note the "re<" are not printed because the 0 char stopped the output.

The string is the message

The variable named: message  is built from other Strings.  Print all of them one by one to see if any have the bad data in them.
Be sure to have delimiters on the String in the print statement so you can see if the print statement was successful.  For example:
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
what all this binary value stuff is
It is a non-printable value in a character that is part of a String.  Some non-printable characters terminate the rest of the printed line.
For example:

Note the "re<" are not printed because the 0 char stopped the output.

The string is the message

The variable named: message  is built from other Strings.  Print all of them one by one to see if any have the bad data in them.
Be sure to have delimiters on the String in the print statement so you can see if the print statement was successful.  For example:


Thanks a lot for the explanation and i found out the cause :



this is the code i used and the console printed out :

name: Stelios
and all the other system.out.println() commands were just fine.So i can conclude that the c.getID() contains the binary value right?Is there a way to fix this binary value or just use this kind of format for my console logging?

Thanks a lot btw for your help
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the console printed out :

name: Stelios 

Note the < is missing following Stelios.  That means the bad values are in the name String.

The next step is to print the length of the name String to see how many bad values there are.  If the String only contained: "Stelios" the length should be 7.  Print the length and see.


all the other system.out.println() commands were just fine.

Does that mean that all the lines that printed ended with the < character?  In that case those Strings were ok.
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
the console printed out :

name: Stelios 

Note the < is missing following Stelios.  That means the bad values are in the name String.

The next step is to print the length of the name String to see how many bad values there are.  If the String only contained: "Stelios" the length should be 7.  Print the length and see.


all the other system.out.println() commands were just fine.

Does that mean that all the lines that printed ended with the < character?  In that case those Strings were ok.


WOW, it's length is 1021 lol, maybe it fills the byte array with 0s ?

And yeah the rest of the system.out.printlns did end with an < so they are fine !
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WOW, it's length is 1021 lol,

Ok, where does that value assigned to the name variable come from?   Are you sure the length was 1021 and not 1024?
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
WOW, it's length is 1021 lol,

Ok, where does that value assigned to the name variable come from?   Are you sure the length was 1021 and not 1024?


yeah it is 1021 and not 1024 and the value from what i can see is assigned inside the ServerClient.java file which does not contain that much code,i am going to try something out too btw
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
inside the ServerClient.java

That's not the source of the value in name.  It just holds the data for easy reference.
Where is the original source?
 
stelios papamichael
Ranch Hand
Posts: 93
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
inside the ServerClient.java

That's not the source of the value in name.  It just holds the data for easy reference.
Where is the original source?


OK, i managed to fix it.The source was inside the process method,i added the /e/ prefix and then splited using the /c/ and /e/ prefixes as regexes if i am correct ! + Fixed 2 more major bugs.
Thanks a lot m8 and thanks to everyone for helping me out ;) !
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i managed to fix it

You didn't say exactly how you fixed it.  The problem was the byte array used to create the variable: string on line 109 included the full buffer including the 0s at the end of the buffer.
The call to the String class's constructor should use the length of the data in the packet's buffer as the number of bytes to include in the String being built with an offset of 0.
public String(byte[] bytes,  int offset,      int length)

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!