• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sending String over Socket

 
Ardel Grider
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys. I am trying to pass a string thru a socket so that the program knows what to send back to me. Despite my trying I can not seem to make progress with it. I am not getting any errors and I am following the examples I am finding to the best of my ability. Would you guys mind taking a look and seeing what I am doing wrong?

Here is my server sockets:


And here is the code for the application requesting the data:

 
Paul Clapham
Sheriff
Posts: 21991
36
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two comments about that. First of all it would be better if you posted an SSCCE (<= follow that link). As it is you have a lot of code which has nothing to do with sending strings over sockets, which is at best distracting.

And second, it would help if you included a description of the problem. So far all we know is that your code compiles correctly.
 
Dave Tolls
Ranch Hand
Posts: 2390
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see no logging in there so you have nothing to tell you where calls and responses have got to on either the client or the server, or what has been sent/received at either end.

Staring at code hoping to spot a bug is a pot luck exercise.  Having logs that let you narrow down where your code has gone wrong helps immensely.


ETA: Actually, scratch that.  I've just seen the println's.

OK, what output are you getting?
 
Ardel Grider
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@paul- Thanks for the link. I posted so much code because I was not sure where the problem was but was pretty sure it was some where in there. I will try to cull some out of it and post as a reply here in a little bit.

@Dave I am not getting any output which is really weird. Even my else statement in my server is not doing anything. I know it is connecting because if I close the server side it says the connection is reset. And if I remove the string passing components then it passes data perfectly fine.
 
Dave Tolls
Ranch Hand
Posts: 2390
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So where are those println calls going?

I'd sort that out first.

How are you running these?
 
Ardel Grider
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a repost of my code after culling out some and adding some comments. Thanks for the advice.
Server:


Client side:

Thanks again!
 
Ardel Grider
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:So where are those println calls going?

I'd sort that out first.

How are you running these?


What I just did was I went thru the code and put println("test") to see the furthest down each code printed. The results say that client side is not printing after "in = new ObjectInputStream( connectToServer.getInputStream());"
The Server side does not print after the while statement ends. Also what I find odd is that printing inside the while statement only prints once.
 
Dave Tolls
Ranch Hand
Posts: 2390
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, are you getting any exceptions printed out?

Are you hiding exceptions somewhere in your code?

I'm going to guess on the client that it is hanging waiting for a response, but I would have put printlns for each event so you can check that.
I would do the same for the server.

 
Ardel Grider
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not getting any exceptions printed out.

I am not sure what you mean by hiding exceptions.

All the println's are showing that it is hanging up after the while statement. And the while statement runs once but doesn't seem to loop.
 
Dave Tolls
Ranch Hand
Posts: 2390
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

that is hiding an exception.

What I mean about your debugging is that there are not enough statements to track where exactly it goes wrong.
You don't print anything until you think you've read all the input in.
On the server I would expect a println before the second while and then something inside that loop (it's not as if it's a long message).

Also, which while statement only runs once?
If it's the while (accepting) (note, no '== true')  then what changes the value of accepting?
 
Ardel Grider
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the exceptions I have them set up to print stack traces catch (IOException e) {e.printStackTrace();} Is there a better way to do that?

I see sorry about that I forget about that loop. I was talking about the while((length = input.read(buffer)) != -1)
 
Dave Tolls
Ranch Hand
Posts: 2390
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Printing the stack trace is good, as then you get the full information on an exception.

So...what happens when you add more println calls?
 
Ardel Grider
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More print calls inside the Client shows that it prints before line in = new ObjectInputStream( connectToServer.getInputStream()); but not after.

On the server side it prints once inside the while((length = input.read(buffer)) != -1) loop but doesn't print again and does not print after. I believe this is where it is getting hung up.
 
Dave Tolls
Ranch Hand
Posts: 2390
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so what does it print on the server?
What has it actually succeeded in reading, and how does that compare with what has been sent?
 
Norm Radder
Rancher
Posts: 1664
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you make a small, complete program that compiles, executes and shows the problem?
I find it useful to run the server and client(s) in the same JVM so that the order of the debug print outs is shown.

Here is a driver I've used for testing:

 
Ardel Grider
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:OK, so what does it print on the server?
What has it actually succeeded in reading, and how does that compare with what has been sent?


Here is the process and results I am testing with.
Start the server:

It prints:
Sever test 1

Then I start the Client:

This prints for Client side:
Client test 1
Client test 2
Client test 3


And server side prints:
Sever test 2
Sever test 3
HLS MSG test
Sever test 4


What I have gotten from this is that by adding length = buffer.length; I am now printing the msg that I am trying to send but only if I print it inside the while loop. It does not seem to be executing any code after the while loop.

 
Dave Tolls
Ranch Hand
Posts: 2390
25
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since it doesn't print after the while loop that tells you it isn't exiting the loop.
Since it only prints the stuff in the while loop once, that tells you that the read() is blocking the second time.

And the reason it blocks is because there is nothing there to read.
The client has finished sending.

However, you are now in a situation where the client is sat waiting for the server and the server is sat waiting for the client...
 
Norm Radder
Rancher
Posts: 1664
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you make a small, complete program that compiles, executes and shows the problem?
 
Ardel Grider
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Dave - I removed the while look and changed the length to = input.read(buffer) and it worked perfectly. Thanks for all the help man. Btw the while loop was part of the sample code that I was basing this off of. Do you know why it was in there if it works better with out it?

@Norm - I think we have her figured out man. Thanks for trying help
 
Dave Tolls
Ranch Hand
Posts: 2390
25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you were sending lots of data (eg a file) then the server would need a loop to ensure it got it all.
In which case you would need a protocol for the sender (the client) to say it has finished.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!