wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Client and Server Sockets with Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Client and Server Sockets with Threads" Watch "Client and Server Sockets with Threads" New topic
Author

Client and Server Sockets with Threads

Jorge Baroudi
Greenhorn

Joined: Nov 01, 2005
Posts: 10

Hi everybody!

"Desperate SchoolWorkingGuy" wrote:

Altough this question is related to my homework i want to ask you something simple, yet, very important for me to understand, I have to implement a Message server for Swing-like cell phones, wich everyone in my class will have to design according to their own cellphones, but just the messagin' service part.

The question is about the client and server and the way the client can receive messages "at the same time" its comunicating to the server to send messages too.

Let me make myself clearer...

/*** STARTS JAVA CODE ***/

/*** ENDS JAVA CODE ***/

This is the Client-Server, i have no problem with the client side, it connects perfectly to the server and registers its telephone ( method registrarCliente ) wich is saved in an ArrayList, and also i can connect and get a list of all the client connected ( method listarClientes ). The problem is when i add a server... if you check my constructor, i have a an instance of the inner class Conexion

conexion = new Conexion( this.socketCliente, this.puertoServidorLocal );

but after starting it, the client can't keep with the registration process ( again the "registrarCliente" method ). All i see in the server is that a client is trying to connect and i believe then the server part of the client ( the above code ) is started due to the threading and because i have a while ( true ) it stays there waiting to receive messages...

So in conclusion, my questions are,

1) Can i have the server and client part in the above code like i'm trying to do it?

2) If it is so, how can i manage both?

3) should i take away the while ( true ) from the client-server side?, but then how do i manage to handle several messages... well, there will be only one client for my client-server code wich is the message server.

If i haven't make myself clear please let me know so.

Many thanks in advance for your help.

P.S. Feel free to ask all you want about translation to English from my Spanish code

[ November 15, 2005: Message edited by: Jorge Baroudi ]

[ November 15, 2005: Message edited by: Jorge Baroudi ]
[ November 15, 2005: Message edited by: Jorge Baroudi ]

Technology is not an image of the world but a way of operating on reality. The nihilism of technology lies not only in the fact that it is the most perfect expression of the will to power ... but also in the fact that it lacks meaning.<br /> [Octavio Paz, Alternating Current, 1967]
Jorge Baroudi
Greenhorn

Joined: Nov 01, 2005
Posts: 10

Okey, now, i have read the "HowToAskQuestionsOnJavaRanch!" and hopefully you'll see that what i'm asking is not quite how to code it but some few ideas on how to actually manage the whole concept, wich is, the client socket of a messaging server with one subclass that handles the message receiving service.

This is not the part that the teacher wants, its just the first part, and im contributing to give the tool for all my schoolmates, actually, the important part for him will be the swing view of the cellphone

So my questions where related not actually to the homework but the Threading part of the server for all the clients (in this case, cellphones).

One good example is Deitel's NonBlockingMessengerCaseStudy wich implements a chat but i don't wnat that everyone gets the message just the specific client address by a telephone wich i save when they register the first time (method registrarCliente) in the server in an ArrayList of Client class (a class with properties name, telephone, ip address and port).

Finally, the idea of some test is coming after reading the whole forum and do some googling... i will try to test some late creation of the server that receives messages in the clients... still, don't know if this is okey, i believe it should be a little easier than that... so after registering the client with the message server, the client itself will have to establish its own server, therefore, i will have the registered client in the server and the client running, as simple as that...

So Late Server Creation... in order to let the client register first without the threading problem i have already...

I'll let you know what happened !

P.S. Yes i'm a newbie in Threads but hopefully i'll get this done before the semester finish

Regards
Jorge Baroudi
Greenhorn

Joined: Nov 01, 2005
Posts: 10

Got it running and working...

Finally...

So, heres where i got some few ideas

http://java.sun.com/products/jfc/tsc/articles/threads/threads2.html

Even though i used the SwingWorker class i had to open more threads in order to acomplish the right behavior, not just to open the server but to accept client connections too...

the code looks like this:



So, If someone was thinking how to do it, hope this is helpful.

Regards.

P.S. Forgot to say that there was another place where i got many more ideas... http://www.javaranch.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Client and Server Sockets with Threads