Regarding socket programming

Hi guys,

I want to ask : I have a program that acts as a server. It uses a single thread to listen for others to so called connect to it. After which it will create a thread for each connection (do whatever they want). The question is can this server handle thousands (or more) PCs trying to connect to it?

I am working on something like a IP server to keep track of IP of connecting PCs. (where later one can get the IP of another to make a connection directly. Any suggestions? Please …

Looking forward for suggestions and ideas! Thanks!  :slight_smile:

it seems like you try to reinvent the wheel.

In combination with jme have a look onto darkfrog's JGN.

It offers you both, a lot of basic networking funcionality (connection/messagehandling) and a lot of freedom to build upon this.

This should be possible…  You’d need to build server-side functionality into all of your client code, as I’m sure you know…

If you’re talking about thousands of IP’s, I’d consider what kind of hardware/connection you have access to.  Creating thousands of threads that just stay running wouldn’t be the way to go about it IMO…  You might want to set some sort of IDLE timeout that will close the thread but keep its different attributes stored.  Not really too sure what you’re going for to be honest, so this is kind of a shot in the dark to get a dialogue rolling

.:emp...imm0|82:. said:

it seems like you try to reinvent the wheel.
In combination with jme have a look onto darkfrog's JGN.
It offers you both, a lot of basic networking funcionality (connection/messagehandling) and a lot of freedom to build upon this.

aye, he makes an excellent point..  Is there something specific you're looking to do?

Hey thanks for the replies. I have found ideas like multithreading … I think can solve that. Another question that I would like to ask :

Game server for instance, I have to maintain thousands of java objects all the time. Maybe lets say I run the server on my PC and maintaining thousands of objects in the memory all the time (adding and removing new objects). Will there be a problem for this - e.g. PC getting slower and slower ?

It would slow your computer… Like any program, but not really because you have more objects, but searching for the right ones and all the extra network transferring etc… This is why it is IMPERATIVE that you ONLY handle objects that are necessary (ie: in sight etc…).

People suggested using JGN as DarkFrog developed his gaming network… This might help you get things running quicker for you:

OK thanks!

Another question is lets say many clients connecting to my server (have a socket connection with each client so I can exchange data between server and client). Can I maintain the connection - store them in a hashmap? So, later if I have a client trying to connect to another client, he has to pass data to the server and with the client id he intended to send to, the server gets the connection and pass the data over to the client for him.

I am using a hashmap to store them … any problems you guys can forsee? Any suggestions would be much appreciated! :slight_smile:

i just can repeat my point…

… you try to reinvent the wheel.

have a look at JGN, where message- and connectionhandling is allready implemented. So you are allready able to call a sendMessage(toPlayer) method.

As i get it from your post, you don't want to have a peer to peer connection. But you want to have the server responsible for the handling of all connections.

This is what JGNServer and JGNClient are allready doing.

So I can use it to handle thousands of connections right simultanuosly?

If so, I will really try using it. Thanks alot guys!

Yes, use JGN or improve it.

I don't see anything wrong with your Hasmaph idea… If the keys are too similar it could take some time to find the objects, but prolly not an issue.

Ok thanks … going from there … I want to ask another question … haha … now lets say I have a web server (e.g. created using Java ee). Any suggestion that how can my webserver connect with IP server? For instance when user access a view connection page, then behind the scene the webserver retrieves the IP list to the user. Inside the webserver are the EJBs … what can I use to connect and retrieve the IPs from the IP server … how can they communicate? anyone has experience working on this before? Or ideas … highly appreciated!

i would not suggest to spread the ips of all clients to a new one, ecxept you would like to implement some peer to peer connections between clients.

A client send his messages allways to the server, who is responsible to forward it to the according client (with ID#5). As mentioned befor JGN has methods to call sentToPlayer( id5, myMessage) and the message will be sent to the server, who forwards it to client #5.

You are able to implement you own messageClasses to send whatever information you'd like.

Just check the sourcecode out of the repository and have a look at the testclasses and the javadoc…

Thread per connection is too much i would say take a look at java NIO  and nonbloking sockets (SocketChanel)

If you have 100 user that means you have 100 threads and some server threads and you need to sync them all and that is not easy (to do it right).

And processors has to switch between Threads so that will be hit to your server preformace

If you use old io you'll need 2 threads per connection. One to read and one to write. Otherwise one slow client can hang the server. Synchronizing it is very easy. Just put the messages into synchronized queues. This works great if the number of connections are low. But for hundreds or thousands of threads you're probably better off using nio.

Yes … thanks for the replies… I already working with NIO now … but is glad to receive comments like that … coz I am more sure and confident about the solution I am working with now … Haha thanks guys!!!

I don't want to be killed by a batrachian but he could use Mina which is not difficult to use and is very scalable. There is also Grizzly but I think the project is dead (?).

Mina? cool … thanks thanks! I will take a look at it!! Thanks guys! :wink: