| Author |
Looking to be able to send data to ONE Connection
|
john price
Ranch Hand
Joined: Feb 24, 2011
Posts: 495
|
|
I am looking to be able to send data to ONE connection. Here is what I have (it works fine) to send to all connected clients...
I am looking to find how to make a separate method on how to send to one. If you could help, that would be great... I need to know how to do the comment below...
Thanks,
cc11rocks
|
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” (Mosher's Law of Software Engineering)
“If debugging is the process of removing bugs, then programming must be the process of putting them in.” (Edsger Dijkstra)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
You need a mapping from client addresses to PrintWriters. I'm guessing that right know, clientOutputStreams is a Collection, List or Set. Perhaps a Map<SocketAddress,PrintWriter> is a better idea. Line 2 of your first method requires only minimal change:
Line 3 of the second method becomes the following:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
john price
Ranch Hand
Joined: Feb 24, 2011
Posts: 495
|
|
clientOutputStreams currently is an ArrayList...
Your answer seems so simple. Still trying to figure it out lol :P
cc11rocks
|
 |
john price
Ranch Hand
Joined: Feb 24, 2011
Posts: 495
|
|
Been thinking about it...If I could get this to work (Maybe you could suggest something?) :
I just have to find a way to get the array variable right... If you would like me to post the full server code, I can and will. It's less than 200 lines, including GUI code, etc. I can probably shorten it (to show you what I am doing) to 100 or less.
Thanks,
cc11rocks
EDIT: Got this working. Basically they can message themselves from this. I just need to make the ArrayList global (I know there is no global in Java, but I can't think of the word)? Then, remove everything from the ArrayList and add the one I want? This seems inefficienct. If this isn't do-able, go ahead and tell me. Thank you!
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19214
|
|
|
You lost me there. Can you please add your new code, as it's not too long?
|
 |
john price
Ranch Hand
Joined: Feb 24, 2011
Posts: 495
|
|
Took out the Unnecessary stuff...
So if I could figure out someway to do this NOT in local variables, I would be able to do the PM's.
EDIT : Guess I should add the method tellUser...
|
 |
john price
Ranch Hand
Joined: Feb 24, 2011
Posts: 495
|
|
So basically, I've got half of the PM done. I would send the message back to the "host" of the message. And the other person. I/we have to figure out the other person. I have no clue. I've tried to think this out, but idk. I don't use lists very much and I'm not very familiar with maps, etc so maybe this is why I'm having trouble. There has to be a solution to this...
cc11rocks
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16479
|
|
john price wrote: I don't use lists very much and I'm not very familiar with maps, etc so maybe this is why I'm having trouble. There has to be a solution to this...
You seem to have identified the problem quite clearly. The solution should be equally clear, then. Learn about lists and maps. Here's a link to the Collections tutorial.
|
 |
john price
Ranch Hand
Joined: Feb 24, 2011
Posts: 495
|
|
The articles were very confusing for me. I am under the impression I am supposed to use a Map? And do you see the solution?
cc11rocks
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16479
|
|
john price wrote:The articles were very confusing for me.
Then keep reading until you aren't confused. This is a basic Java topic so you really should master it before trying to write non-trivial programs.
I am under the impression I am supposed to use a Map?
That's what Rob Spoor suggested, I haven't read the thread in detail but I would go along with that.
|
 |
john price
Ranch Hand
Joined: Feb 24, 2011
Posts: 495
|
|
Now that I've read the articles and seen his response, it makes sense. He's already given me the answer :P. I will read more though.
cc11rocks
|
 |
john price
Ranch Hand
Joined: Feb 24, 2011
Posts: 495
|
|
Think I got it now...
At the beginning of the program :
In the thread, in the run() method BEFORE the try statement...
To make this user friendly, could I add a Map to it. Something like :
Later...
Would this work? How would I connect the HashMaps? Would I call the values() method? Then I use the get method for the string. Then, I use the values and then get method for the PrintWriter. Is there a better way to do this?
Thanks,
cc11rocks
|
 |
john price
Ranch Hand
Joined: Feb 24, 2011
Posts: 495
|
|
Believe I figured it out...
PM(String sockaddr, String message) {}
To send the PM back to the user that sent it, call the tellUser() method with the "PM : " + message. It's actually not that hard... Thanks for helping guys! I really appreciate it!
cc11rocks
|
 |
john price
Ranch Hand
Joined: Feb 24, 2011
Posts: 495
|
|
I held off resolving this until I had tested it on at least 4 computers and at least 3 internet connections. Today, I have and it works beautifully (I MAY have changed it since then). Anyway, thank you guys for your help!
John Price
EDIT : Now that I think back, I did change it. I made it user friendly ("person@message"). I also made it send back to the person who sent it. I also formatted the message differently, but the general concept is the same, involving pretty much the same method.
|
 |
 |
|
|
subject: Looking to be able to send data to ONE Connection
|
|
|