| Author |
Sending data from client to Server
|
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 802
|
|
ok i've been looking at a bit of code. and I still don't know when or how the server recognizes what data was sent to it. does a serversocket have a inputstream and output stream too? or does it just capture that client, and extract the contents from its input and output stream? Thanks, Justin Fox
|
You down with OOP? Yeah you know me!
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
The ServerSocket does something semi-magical when it accepts a connection. It creates a new socket and converses with the client over the new one. So the server code looks like: More realistically, a typical server moves the client conversation onto another thread so it can accept the next client call in good time. See the Sun Concurrency Tutorial for all the good details.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 802
|
|
cool, that makes it a lot more clear now. now for another question. is the conversation between a client and server synchronized? by that i mean, when the client writes to its output stream, will the server wait until its wrote to, before extracting the data from the client's input stream? if not, could I get some pseudo code illustrating how this happens? p.s i saw something about flush() for the streams... does this play a part? Thanks for the replies, Justin Fox
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
I've always done things synchronously ... the client sends a message and closes the socket, the server reads to end of stream which ends because of the close. Then the server responds and closes its stream, and the client reads to end of stream. I think there's nothing to prevent both ends from reading and writing at the same time. You could try making a server that echoes one byte a time back to the client. See if there's such a thing in the socket tutorial.
|
 |
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 802
|
|
if you close the socket, that closes the connection right? Justin Fox
|
 |
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 802
|
|
I have a new question, i have the client sending a message to the server, and the server gets it and prints to the screen, now, how do i send the same message back to the client that sent the message to the server? Thanks, Justin Fox
|
 |
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 802
|
|
I have the sending done, but it seems that my chatting has a delay for awhile but then it will start working just fine. for example: client1 will say "hey" , but client2 will not recieve it. then client2 will say "hey2", then client2 recieves client1's msg, and then client1 recieves client2's message just fine, then the conversation is carried out normally, ived tried to think of wat could cause that. but I'm stumped. Thanks for the help, Justin Fox
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
|
Are client1 & client2 using client sockets? Is there a server somewhere?
|
 |
Justin Fox
Ranch Hand
Joined: Jan 24, 2006
Posts: 802
|
|
I fixed it, what was happening is i have a snippet of code like so: //(pseudocode) so it wouldn't start the listen thread until that client actually typed something and/or hit enter. I've seen threadGroups, is that more efficient to keep track of the clients, or do i need to just use a socket array? if the threadgroup is better, how do i sift thought the threads and pick one out? Thanks again, Justin Fox
|
 |
 |
|
|
subject: Sending data from client to Server
|
|
|