This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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...
“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)
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:
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.
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!
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...
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 :
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?
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!
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.