• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 388
  • Last Modified:

2 Clients, 1 Server in Java

I'm writing a card game, similar to poker which is for 2 people.  So I need 2 clients and 1 server program.  Is this possible without threads?  I find threads very difficult and would like to focus on getting it done.  What I am trying to do now is create 2 server sockets in my server program and then have one client connect to one of each of the server sockets.   That doesn't seem right though.  Help?
0
jpking72
Asked:
jpking72
  • 3
  • 3
  • 2
  • +1
1 Solution
 
for_yanCommented:
In poker, as far as I know, the players are playing sequentially - so you probably can
 do it without threads.
Maybe it sounds a little crazy, but you can set up
RMI servers on both machines, and when one player
plays, it will call a method on another client's machine and
it will request the response from the second player
and vice versa.
You can maintain the data on the cards in database
accesible from both or on one of the RMI servers or pass
it together with the each move.
This is not scalable for more than 2 players, but will
work for just 2 players and RMI is easy to implement and is rather
lightweight. And I guess there is no reason why RMI
server should not be connected to visual front end.

Maybe your idea with sockets is more narural after all,
but then i guess, the client will need to poll the server
to know if the other player played, if we don't want to use
threads.
0
 
jpking72Author Commented:
how about node.js?
0
 
for_yanCommented:
And what  is node.js?

I guess you can have it something as web server with javascript.

Java is closer to my mind though than web and javascript
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
CEHJCommented:
>>So I need 2 clients and 1 server program.  Is this possible without threads?  

Yes. Make the server run two ServerSocket instances on different ports. Each client will connect to its own port
0
 
jpking72Author Commented:
Thanks CEHJ,

A follow-up question:

How do I dynamically determine if socket1 has been bound?

I was thinking something like:

//check if port 5000 is already connected:
connection = new Socket(InetAddress.getByName("localhost"), 5000);
//else:
connection = new Socket(InetAddress.getByName("localhost"), 5010);


0
 
CEHJCommented:
>>How do I dynamically determine if socket1 has been bound?

Why would you do that?
0
 
jpking72Author Commented:
i have one client to connect to port 6000
and the other to connect to port 6010.  

Or do i just hard code it and make two different client files, one with 6000 and one with 6010
0
 
CEHJCommented:
>>Or do i just hard code it and make two different client files, one with 6000 and one with 6010

You could,  yes
0
 
objectsCommented:
you only want one server socket. opening a server socket for every player is a bad idea and will just cause you problems in the long run
and it won't work without using threads anyway so is just going to waste your time
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now