Results 1 to 16 of 16
- 11-21-2008, 05:12 AM #1
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
- 11-21-2008, 05:25 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Actually multiple clients are connected to the server, while server listen through it's socket.
Anyway, this is a forum not a code bank to just give codes away. You have to make an attempt and if you stuck with something ask your questions, lots of members are here ready to help you.
- 11-21-2008, 05:40 AM #3
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
thanx a lot for ur suggestions.........
- 11-21-2008, 05:42 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 11-22-2008, 01:52 PM #5
Member
- Join Date
- Nov 2008
- Posts
- 2
- Rep Power
- 0
The first you need to create a ServerSocket object. This object will wait for connections. When a client connect to this server, a new connection is generated by
The code so long. You can refer here java2s.com/Code/Java/Network-Protocol/Agenericframeworkforaflexiblemultithreadedserver.h tmJava Code:ServerSocket ss = new ServerSocket(2008); while(true) { Socket s = ss.accept(); //You handle socket s here, for example, you create a new Thread which will operate this socket } ///
- 12-16-2008, 11:40 AM #6
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Also, you probably look at avoiding creating a brand new Thread for each connection, either for performance if you have a moderate number of requests per second, or just to control the number of concurrent threads. Take a look at ThreadPoolExecutor class (link is to a web page of mine with an example in case its useful).
If you have a high volume of requests (say, more than 100 simultaneous connections), look at Selector class in the NIO package, which allows you to interleave connection processing in a single thread.Neil Coffey
Javamex - Java tutorials and performance info
- 12-17-2008, 03:18 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
But I don't think working on with multiple threads is a good solution. If you can schedule on single worker thread it's much better. Major reason I've see on multiple threads is, thread killing, in case of large number of client. How about millions of customers at a second, like Skype does....
- 12-17-2008, 03:37 AM #8
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
The major reasons for the thread-per-connection paradigm are:
- historical -- before the widespread availability of readiness selection, that's just "how it was done";
- nowadays, ease of programming -- conceptually, having a thread per connection is "the thing you want to do"; it's just unfortunately not very scalable.
Actually, a thread per connection isn't so bad if you pool threads and know that you won't have too many simultaneous connections. Most web servers running in the world today are boring old thread-per-connection servers. On a reasonable-spec server, you can serve hundreds of simultaneous connections that way.
As I say, a Selector will get you up to thousands of simultaneous connections on a single server.
Peer-to-peer services like Skype handle a huge volume of connections, but not from a single server!!!Last edited by neilcoffey; 12-17-2008 at 03:38 AM. Reason: typo
Neil Coffey
Javamex - Java tutorials and performance info
- 12-17-2008, 03:52 AM #9
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
I agreed with you. But I have a little doubt here, how the selector select a thread you talking about. Just forget about multiple servers, respect to one?
- 12-17-2008, 10:02 PM #10
Member
- Join Date
- Dec 2008
- Posts
- 1
- Rep Power
- 0
Hi, I am looking for any links that can help me out. I have searched for the last few days and cant figure this out. I was able to connect 1 server and 1 client but cant get multiple clients. Here is exactly what I am trying to do.
-1 server, 10 clients
-each client enters a name
-there are 10 trials
-for each trial, each client enters a number, that number is sent to the server.
-Once all the numbers for the trial have been recieved, the server generates a random number and then selects the winner based on the clients numbers. It then sends the name of the winner to all the clients
-then the next trial begins and we repeat
I was having trouble determining which client the number was from and also in the examples ive seen I cant figure out if u can leave the connection open for a while or should you close it. Is 5 minutes too long?
Thanks!
- 12-17-2008, 11:56 PM #11
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
- To get multiple clients, as mentioned now in this thread, the easiest way is to swpan one thread per client. So your "main" thread sits waiting for connections; when one comes in, it spawns a new wone to handle the connection. Off t'top of my head, something like this:
- to know which client a connection/request comes from, just allocate a number from your server the first time you hear from a given client and pass it up to the client; whenever a client connects, it has to tell you its previously-allocated number (or say "I have no number -- please tell me my allocation");Java Code:ServerSocket ss = new ServerSocket(portNo); while (!shutDownRequested) { Socket s = ss.accept(); Thread thr = new HandlerThread(s); thr.start(); } ... private class HandlerThread extends Thread { private Socket s; HandlerThread(Socket s) { this.s = s; } public void run() { // handle connection, reading and writing to s } }
- in a real production environment, you'd have to worry about clients not being able to guess each others' numbers, but I guess that doesn't matter too much for you now (if it does, a starting point is SecureRandom over an HTTPS connection);
- you probably want to allocate each game a number too, or at least one to each client/game pair -- then a client can ask the server "what was the result of game X, for which I am client Y" and the server can respond with "it was Z", or "sorry, I don't recognise that game number" etc;
- you can TRY and keep the connection open, but you shouldn't RELY on it; when you want to keep the connection open it's good practice for the server to periodically send the client a "PING" message, and the client reply with some response to say "I'm still alive" -- that way, the the connection doesn't time out
- but either way, you should assume that a connection can randomly get closed at any minute for some spurious unknown reason -- that is what the real Internet is like...Neil Coffey
Javamex - Java tutorials and performance info
- 02-05-2009, 06:46 PM #12
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
thaks for giving source code
- 02-16-2009, 06:40 AM #13
Member
- Join Date
- Feb 2009
- Posts
- 2
- Rep Power
- 0
IS any one can tell me that how to count the serverside response through Socket programming.
- 02-16-2009, 04:18 PM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
What you mean counting response from the socket? You want to count number of response in ordinary ways, or mean something different?
- 03-22-2010, 12:14 PM #15
Member
- Join Date
- Mar 2010
- Posts
- 1
- Rep Power
- 0
multiple client connect to single server?
hi. the clients are connecting to the server but the server interact with the first client which connect ........ so how can i pass the message it the same time to all the client which get connected.......
- 03-22-2010, 12:54 PM #16
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
First of all, welcome to our community. :)
Please don't interact with un-related thread next time. I didn't see any relation with the original question. Anyway, did you know about synchronization and stuff?
Similar Threads
-
SSL Server with authentication of clients
By zoltan in forum NetworkingReplies: 0Last Post: 10-14-2008, 11:27 PM -
Connect Mobile Application and Web Server Using php and Java
By satheeshkumar in forum NetworkingReplies: 0Last Post: 08-25-2008, 02:13 PM -
Server not able to connect client
By Kapil Gupta in forum New To JavaReplies: 15Last Post: 07-22-2008, 04:09 AM -
blocked between server and clients
By ibtehal in forum NetworkingReplies: 6Last Post: 07-17-2008, 12:30 AM -
connect to web server from mobile using java app.
By jankrishnan in forum Advanced JavaReplies: 1Last Post: 07-15-2008, 04:12 PM


LinkBack URL
About LinkBacks


Bookmarks