aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes Client-server message exchange Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Client-server message exchange" Watch "Client-server message exchange" New topic
Author

Client-server message exchange

Radu Popescu
Greenhorn

Joined: Oct 08, 2013
Posts: 6
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!


Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
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

Joined: Oct 08, 2013
Posts: 6
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
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
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.
Partheban Udayakumar
Ranch Hand

Joined: Jul 04, 2013
Posts: 267

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


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."
--- Martin Fowler
Radu Popescu
Greenhorn

Joined: Oct 08, 2013
Posts: 6
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
Marshal

Joined: Mar 22, 2005
Posts: 42929
    
  68
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.
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1031
    
    5

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

Joined: Oct 08, 2013
Posts: 6
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.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18989
    
    8

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

Joined: Oct 08, 2013
Posts: 6
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

Joined: Oct 08, 2013
Posts: 6
I've solved everything, more or else. Thank you for helping! Topic can be closed.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Client-server message exchange