• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Client-server message exchange

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey all. I'm a beginner beginner in Java programming and all the following code is purely based on information and lines of code found online so any tips/comments are welcomed, please!

What I have to do is to implement a client-server message exchange. The client, based on the answer received from server, has to 'know' what to do next. I know the code looks suspicious; it is a course homework! *also my rant, I wasn't thought any programming for this yet they somehow assume I know it already*

Based on the code I also have a couple of questions:

- How to use while(value) so that it executes as long as the value is not null? In C++, if i remember correctly, you would just write while(value).
- What does bw.flush actually do?

I'm sure there are lines missing and also lines extra. I also do not 100% understand all the lines. If the question is inappropriate, I apologize and hope the question can be (re)moved.

Thank you!


 
Rancher
Posts: 43081
77
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How to use while(value) so that it executes as long as the value is not null?


while (value != null) {
...
}

What does bw.flush actually do?


It causes all the data that you have written to the Writer to be sent to its destination. A BufferedWriter buffers things, so whatever you write to it may not get sent to its destination right away - flush makes sure it does.

And one important thing: Strings are compared using the equals method, not using the "==" or "!=" operators. You need to change those 3 lines of code.
 
Radu Popescu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:

How to use while(value) so that it executes as long as the value is not null?


while (value != null) {
...
}

What does bw.flush actually do?


It causes all the data that you have written to the Writer to be sent to its destination. A BufferedWriter buffers things, so whatever you write to it may not get sent to its destination right away - flush makes sure it does.

And one important thing: Strings are compared using the equals method, not using the "==" or "!=" operators. You need to change those 3 lines of code.



Thank you for the quick answer! So about the equals, should e.g. become ? It seems to be right after googling and no error in NetBeans.
Other than that, is the code ok in your opinion?

General question: How would one test the program on his own computer? I also have the Server.java file I've been working on in parallel. Is it as simple as compiling each .java in a different project and then running them, server one first?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I haven't gone through the code in detail, so there may be other problems. But yes - you can test the client and server by running both on the same machine. If you haven't done so yet, work through http://docs.oracle.com/javase/tutorial/networking/sockets/index.html - it shows a working client/server pair in action.
 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Radu,

One more small suggestion, if you are going to use more options inside the "while(message!="0")", please use switch case for easy understanding of the program. I hope you know about switch case else here is the link for you

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html
 
Radu Popescu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have reorganized the code based on your suggestions yet the Client side (at least) doesn't seem to work. I tried running them both, Server first but the Client doesn't print anything and also doesn't seem to end therefore I assume there are basic mistakes. Can you point out the problem(s)?



 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make sure you're not running into this problem: Don't println to a Socket. Using "readLine" with a socket falls into the same category.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Radu

Looking at your code, it would be advisable to take the logic in your main method and move it into separate methods belonging to the Client class.

Ideally, the main method should be very short i.e. create a Client object and invoke an entry level method on it.

Refactoring like this will lead to better OO code, making it more maintainable and reusable.
 
Radu Popescu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the answers! Unfortunately, I had to change my project completely because it looks like I misunderstood the task. From now on, I'll try to limit my posts to particular questions. My first one is:

I receive messages from server in bytes. I use to read that stream and then the following code to convert to string:



Is there any way to declare the byte array (or to rewrite the code above) such that it only has the length of the message? So far I used and after receiving message from server, the rest of the array is filled with 0's. I need to get rid of those 'added' zeroes. The messages from the server are of changing length.
 
Sheriff
Posts: 28368
99
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest way is for the message sender to first send the number of bytes (in an int, for example) and then send the bytes. The receiver would receive the number of bytes, create an array of that size, and then receive the bytes into that array.
 
Radu Popescu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:The easiest way is for the message sender to first send the number of bytes (in an int, for example) and then send the bytes. The receiver would receive the number of bytes, create an array of that size, and then receive the bytes into that array.



That's brilliant! How didn't I think of that. The first byte of the message is in fact the number of bytes of the whole message. God, and I thought that's irrelevant.
 
Radu Popescu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've solved everything, more or else. Thank you for helping! Topic can be closed.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic