Results 1 to 13 of 13
- 06-26-2010, 02:19 PM #1
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
different requirements on the same server
Hello everyone,
have a beginner's question :)
Can somebody explain me how to realize this?
in eclipse i made client server communication, where client connects to the server, he accepts and gives him a thread, in thread client request some data from database. When thread done his task, back him result and closing connection.
this is ok! It works!
But need to make that client call server one more time and request to write some data to database. And now i am confused... if i make one more thread for this how server can recognize what client want (which thread)?
Everything is on localhost.
every answer will be helpful
Thanks
Regars
- 06-26-2010, 03:13 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,391
- Blog Entries
- 7
- Rep Power
- 17
Don't close the connection after the first client request but reuse it for further requests. If you close the connection the server doesn't know the client wants some more information, i.e. it has lost all connection information about that particular client.
kind regards,
Jos
- 06-26-2010, 03:22 PM #3
How does the server identify a client on the first connect?
In HTTP cookies are used to carry id from one connection to another.
In your case, can you add a field in the comm protocol you are designing to carry id information. then each connect/response can be independent and server will know who it's talking to.
- 06-26-2010, 05:04 PM #4
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
just create a socket.How does the server identify a client on the first connect?
I don't need specil identification for this, because on first call everybody receives same data, and on second call just need to save his information to database.
Don't need to keep session for the client.
Server just need to send data and to allow everyone to save it to db.
Has two tasks. Is that should work with two thread?
If yes, how to server ask what he want to do (which thread)?
this is a windows appl. -swing
Thanks so much
- 06-26-2010, 05:19 PM #5
Don't understand what the problem is if:how server can recognize what client want
Can you explain where the problem is?on second call just need to save his information to database.
Is this how it works:
Server waits for connect.
Client#1 connects and sends msg1
server replies.
Client#1 connects and sends msg2
server does something
Client#2 connects and sends msg3
server replies
Client#2 connects and sends msg4
server does something
Continues for x clients.
- 06-26-2010, 05:39 PM #6
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
yes that's okIs this how it works:
Server waits for connect.
Client#1 connects and sends msg1
server replies.
Client#1 connects and sends msg2
server does something
Client#2 connects and sends msg3
server replies
Client#2 connects and sends msg4
server does something
But problem is here: have two threads for two tasks.Can you explain where the problem is?
Java Code:public class ServerSide { private static final int TCP_PORT = 5609; public static void main(String[] args) { try { int clientCounter = 0; ServerSocket ss = new ServerSocket(TCP_PORT); System.out.println("Server running..."); Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con= DriverManager.getConnection("jdbc:odbc:mydb"); Statement queryStmt = null; queryStmt = con.createStatement(); while (true) { Socket newSoket = ss.accept(); System.out.println("Client accepted: "+ (++clientCounter)+ " " + newSoket.toString()); [B]ServerThread1 st = new ServerThread1(newSoket, clientCounter, queryStmt, con); //ServerThread2 stu = new ServerThread2(newSoket, queryStmt, con);[/B] } } catch (Exception e) { e.printStackTrace(); }
- 06-26-2010, 06:10 PM #7
Why the hardcoded threads? Why not create one each time there is a connection?
What are the two tasks for?
Each time a client connects, create a new thread to handle that transaction.
- 06-26-2010, 07:29 PM #8
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
do you think i need to have one thread which do everything?Why not create one each time there is a connection?
one: returns data from dbWhat are the two tasks for?
second: to recive data from client
- 06-26-2010, 07:35 PM #9
There will be as many threads as needed.
The first is the current thread that waits on a connection.
The next will be to handle the first connection
The next will be to handle the next connection.
The next will be to handle the next connection.
...
- 06-26-2010, 07:54 PM #10
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
Yea, each clients gets new thread. It's ok.
But how to server recognize what clients wants?
Need i write loop on server side for this or.... don't know how it works?
How many thread class i need to hava?
:confused:Last edited by ra78; 06-26-2010 at 08:14 PM.
- 06-26-2010, 08:45 PM #11
That's what I earlier referred to as "the protocol you are designing". The client has to tell the server what he wants by some data in the message he sends to the server.But how to server recognize what clients wants
You create a new one for each connection:How many thread class i need to hava
pseudo code:
forever loop
Wait on connection with server socket
when get a connect on socket
MyHandler mh = new MyHandler(socket); // MyHandler implements Runnable
Thread t = new Thread(mh);
t.start();
end loop
- 06-27-2010, 10:37 AM #12
inside your thread you need a handler that do something like that
Java Code:public synchronized void handle(int ID, String input) { if (input.equals(".bye")) { clients[findClient(ID)].send(".bye"); remove(ID);
the above handler check if the client send .bye and if the server close the connection by calling remove(ID). for your taks you don't need to check for ".bye" but perhaps your could check if the input-string is a "INSERT INTO YOURDB" and if you could call a method that inserts your data.Last edited by j2me64; 06-27-2010 at 10:54 AM.
- 06-28-2010, 05:50 PM #13
Member
- Join Date
- Jun 2010
- Posts
- 6
- Rep Power
- 0
pseudo code:
forever loop
Wait on connection with server socket
when get a connect on socket
MyHandler mh = new MyHandler(socket); // MyHandler implements Runnable
Thread t = new Thread(mh);
t.start();
end loopThanks guys! It was not clear to me how it works. Now it's ok!public synchronized void handle(int ID, String input) {
if (input.equals(".bye")) {
clients[findClient(ID)].send(".bye");
remove(ID);
Regards
Similar Threads
-
Requirements for building a SSL VPN
By adityag in forum New To JavaReplies: 0Last Post: 01-19-2010, 04:46 PM -
New to Java.. What are the requirements?
By konn in forum New To JavaReplies: 10Last Post: 03-27-2009, 12:50 PM -
External DTD requirements
By jwilley44 in forum XMLReplies: 0Last Post: 03-06-2009, 09:25 PM -
VOIP Server Hardware Requirements
By asifsolkar in forum NetworkingReplies: 12Last Post: 05-21-2008, 04:23 AM -
Minimum system requirements
By coco in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:19 PM


LinkBack URL
About LinkBacks


Bookmarks