wood burning stoves 2.0*
The moose likes Sockets and Internet Protocols and the fly likes Client-Server Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Client-Server Problem" Watch "Client-Server Problem" New topic
Author

Client-Server Problem

John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Hi folks,

Just started workin on a Client Server program in the hopes of gainin better understanding of Socket programming but have come up against a brick wall that i cant understand.

Basically i have set up the client and server, everything appears to work fine when i send information to the server from the client but when i try to respond to the client everything just locks up....

Here are the class' i am using. (Client only included main) Hopefully from these someone will b able to spot where i am going wrong.

Client stuff:



My Server is multiThreaded.

Server stuff:



ServerThread stuff:




I am not sure what would cause this, If anyone has ne ideas i would be really greatful.

Thanks,

John
[ March 06, 2006: Message edited by: John Bartlett ]
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Hi John,

Your problem is that in the ServerThread you are using the PrintStream object. You need to call the flush() method to send the information back to the client or use the PrintStream constructor with the autoflush parameter.

Your code is sending the message to the server because your are using the DataOutputStream.

Jason.
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Hey,

Changed my code to match wat u said:

Now in my ServerThread it is:



Unfortunatly it made no difference. the minute you try to send something back to the client frm the server they both just lock up.

Any other suggestions?

Thank you,

John
Jason Moors
Ranch Hand

Joined: Dec 04, 2001
Posts: 188
Is the sent message displayed?

I'll try your code later.

Jason.
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
Ok i been messing around with the code and have found that when i dont use the while loops it works ok (alas only reads in one line tho which is pointless), so thinkin must be a problem with that.

When i changed the code to this:

Client



ServerThread:



The server class remained the same.


General interaction so far:

Client Send: GET + "/" + pot + " HTCPCP /0.1 \r\n
Server Displays: GET + "/" + pot + " HTCPCP /0.1
Message Sent
Server Sends back: "message Recieved: " + (received string above);
Client displays: above message

My last test on the while loops before i found that removing them and replacing them with just:



showed me that i could send strings bk from the Server to the client aslong as the client had the above code and i sent it back from the server before the while loop (if it was put after it would lock)

Also that if i use this code in the Server:



it prints out: GET + "/" + pot + " HTCPCP /0.1

and then just sits there like it waiting for more info from the client and locks them both up, not sure y this is? dont know if that helps at all?

Thanks

John
[ March 07, 2006: Message edited by: John Bartlett ]
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
Well, that's because it is waiting on more input.
There has not been any kind of "EOF" or "EOT" reached.
Your server either needs to react (read do something) with
every line that it receives, or your client must send some
keyword to indicate that its request is finished (http Get
request does this with a blank line after the get line) and
then the server must react (read do something) after that
or your client must close its outputstream (the servers
inputstream) so that the server finishes reading and can
start processing.
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
ok, how do i go about doing the EOT?

I thought that the while loop



would handle EOT as it only reads until the in.readLine returns a Null value?

Also i thougth my lines at the end closed the input and output streams?



Do i need them elsewhere 2?

Thanks for the reply,

John
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
Well, putting in further close() statements is not really what you want
to do as you wish to "speak" back and forth. I would do as follows:

replace:

//sending first message to server
out.println("GET " + "/" + pot + " HTCPCP /0.1 \r\n");


with:

//sending first message to server
out.println("GET " + "/" + pot + " HTCPCP /0.1 \r\n\r\n");


and replace:

while ((inputLine = in.readLine()) != null)
{
input += inputLine;
}


with:

while ((inputLine = in.readLine()) != null) {
if (inputLine.equals("")) {
//call a subroutine to process input passing "input" as parameter
input = "";
} else {
input += inputLine;
}
}

if (!input.equals("")) {
// call a subroutine to process input passing "input" as parameter again
// inorder to catch any last statement that may not have included an exra
// blank line after it and before the close() statement in the client
}


and in the serverthread place your response code in the subroutine instead
of after the while loop. You can also, in your while loop, look for the
"thank you" line explicitly and respond to it in a different way than the
rest in the same way that you check for the extra blank line.
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
ok my client code is now lookin like this:

Client:



wasnt sure wot u ment by:
in order to catch any last statement that may not have included an exra
blank line after it and before the close() statement in the client?


What changes should i make to the ServerThread? Just put the code i have used to respond to the client within a seperate method and call that from the main do u mean?

Thank you,

John
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41173
    
  45
ROFL! That's the first implementation of the HTCPCP protocol I've seen. What kind of hardware do you have it hooked up to?


Ping & DNS - my free Android networking tools app
John Bartlett
Ranch Hand

Joined: Jan 25, 2006
Posts: 116
hehe, it is just part of an assignment i am currently workin one. Just used to get experience with sockets and interacting between them. pretty good laugh!

Excellent now the interaction is working properly. Altho it still doesnt seem to display more than one input, e.g.

if i send more than one thing from the client to the server:



Only the first message gets displayed and returned in the Server, it is like the others dont get sent. does neone know y this is? have i got to flush the outputstream or somin?

Thank u,

John
[ March 07, 2006: Message edited by: John Bartlett ]
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
No. Al you really need to do is add the "\r\n" to the end of your other
out.println statements in the client and that should take care of it. Also,
the extra "\r\n" was not really needed on the "get" line. I didn't really
pay attention to the fact that it was a println instead of a print. On that
line one "\r\n" was enough, it was just that the server was not reacting to
that blank line, and the if statement inside the while statement took care
of that.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41173
    
  45
Always a good read (and applicable to all socket traffic, not just on Macs): Don't println to a socket!.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Client-Server Problem
 
Similar Threads
Reading a binary file from a client to server
client / server (communication problem...)
SocketException is not throwing in Windows Vista while network lost.
two server and many client....
not able to connect...