• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Client and Server Sockets with Threads

 
Greenhorn
Posts: 10
jQuery Ruby Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Jorge Baroudi
Greenhorn
Posts: 10
jQuery Ruby Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
jQuery Ruby Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Please do not shoot the fish in this barrel. But you can shoot at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic