Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

multithreaded two port server

 
kartik rathore
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

i would be grateful if some information can be provided to me for my problem below :

developing a two port multi-threaded server which lets clients connect on two different ports. the server can handle multiple clients.

At the moment i have been able to develop the the server which reads and writes on the two ports with multiple clients , however it only connects to the second client if the
first client has connected on the 1st port and vice versa.

i am using sockets for the development. however, using sockets, the program waits at sockets.accept() method for a connection to be made.

Could anyone tell me what would be the other way round this ?

thanks
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ServerSocket.accept returns as soon as a connection has been made. So it blocks if there is no connection. Do you have a Thread for each ServerSocket?
Otherwise your call to the second ServerSocket will occur only once the first has made a connection.
Could you show some code to explain your problem?
 
kartik rathore
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Server Class

 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've a couple of comments about your code:

Almost everything is in one function. Try to divide it into classes and methods. An OOP approach.
Never swallow exceptions. At least log/output them. Otherwise you'll never know that it went wrong.
You're calling accept on both the ServerSockets within one thread (main). So ss2.accept will be called
when ss1 got a connection.

You're using a ExecutorService but also starting Thread on your own.

Try to start a ServerSocket in it's own Thread.
 
kartik rathore
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could you please adive me more on elaborating with some code please
thanks
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Something like this:
 
kartik rathore
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks

The program runs properly now

Much appreciated.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!