• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Client-Server Problem

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is the sent message displayed?

I'll try your code later.

Jason.
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ROFL! That's the first implementation of the HTCPCP protocol I've seen. What kind of hardware do you have it hooked up to?
 
John Bartlett
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Always a good read (and applicable to all socket traffic, not just on Macs): Don't println to a socket!.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic