0

I am making a simple online tic tac toe game (multiplayer). where the client side is java (android) server side is python (linux on a shared hosting server)

My question is about the server side: I was first thinking of having one socket (because I am only allowed to use one port on the server), then it waits for 2 users to connect and pairs them together, start a new thread to deal with them, then waits for another 2 users and so on. But after reading here alot about multithreading I found out that the server can handle at most 20 threads. So I tried using processes instead of threads but I got the same result. Moreover, I found out that the socket can handle at most 50 connections.

Any Ideas? Thanks

user3571278
  • 721
  • 5
  • 15
  • Where did you find the server can handle only 20 threads .. I think this is wrong assumption .. – Karthikeyan Mar 15 '15 at 03:46
  • I feel the greatest part of the problem is because of how sockets work, that I have to keep the client connected through out the game. If there was an option to just send a string of data without establishing a "connection" that would fix the problem. – user3571278 Mar 15 '15 at 03:47
  • 1
    @Karthikeyan I have just tried it on my server right now, I use host gator (the cheapest package) I think even if I buy a more expensive hosting, I will still be very limited by the amount of threads I can have. What if I got thousands of players for example? – user3571278 Mar 15 '15 at 03:48
  • I would suggest you to use a standard linux node, for this, rather than a shared hosting .. – Karthikeyan Mar 15 '15 at 03:50
  • And limitation on number of threads is dependent on the system resources and the nature of the tasks, like cpu bound or io bound .. – Karthikeyan Mar 15 '15 at 03:51
  • http://stackoverflow.com/questions/344203/maximum-number-of-threads-per-process-in-linux – Karthikeyan Mar 15 '15 at 03:52
  • Thank you that's a useful piece of information I tried it and found out it's a big number but because it's a shared hosting I only have a very limited amount of threads This is why my question is what's an alternative to threads – user3571278 Mar 15 '15 at 03:54
  • 1
    If you want to scale to thousands+ of users, simply *don't write your own server* - pick a technology stack and implement your app on top of that stack. The details of exactly how each stack works varies wildly (ie choosing two at random, compare LAMP with node.js, two completely different approaches) – roippi Mar 15 '15 at 03:57

1 Answers1

1

To scale without bounds, if you control the client code (so you know people aren't cheating -- which at tic-tac-toe they'd be unlikely to:-), you could have the client open and offer a listening socket in order to connect -- when an odd client connects, just respond with a "please wait" message; when the even client connects to match it up, respond to both clients with the listening-socket info about each other, and get out of the way.

That won't work for clients who can't open, and listen on, a new socket (e.g., ones sequestered behind some NAT arrangement). In such a situation, you could switch the clients (for their follow-on interactions with one another) to UDP to/from your server -- UDP, not being connection oriented, can serve arbitrarily high numbers of clients (client pairs, in your case!) on a single socket (but then you're responsible, cooperatively on the client and server sides!, for checking/acknowledging packets and ensuring their good ordering, which TCP, being connection-oriented, handles on your behalf:-).

I'm not sure where exactly all of your constraints come from in the first place, or what other constraints (such as clients being unable to open, communicate, and listen to, new sockets...) might apply.

But one way or another, once you fully understand and tell us about all the applicable constraints, some solution can always be found (perhaps with new-fangled conniptions such as pub-sub, e.g https://cloud.google.com/pubsub/docs -- as fast as new constraints arise, or faster!, clever guys are always figuring out work-arounds...!-)

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Thank you that was really helpful. I was unaware of that difference between TCP and UDP. now I know what to google. Unfortunately, I don't think the clients can listen on a socket because I can't tell every client to open a port on his router for the game. – user3571278 Mar 15 '15 at 05:44
  • I'm really sorry, I have upvoted it now. Thank you I really appreciate it. – user3571278 Mar 15 '15 at 06:12