Results 1 to 13 of 13
  1. #1
    ra78 is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default 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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by ra78 View Post
    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
    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

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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.

  4. #4
    ra78 is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    How does the server identify a client on the first connect?
    just create a socket.
    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

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    how server can recognize what client want
    Don't understand what the problem is if:
    on second call just need to save his information to database.
    Can you explain where the problem is?

    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.

  6. #6
    ra78 is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    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
    yes that's ok
    Can you explain where the problem is?
    But problem is here: have two threads for two tasks.

    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();
    		}

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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.

  8. #8
    ra78 is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    Why not create one each time there is a connection?
    do you think i need to have one thread which do everything?
    What are the two tasks for?
    one: returns data from db
    second: to recive data from client

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    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.
    ...

  10. #10
    ra78 is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    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 09:14 PM.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    But how to server recognize what clients wants
    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.

    How many thread class i need to hava
    You create a new one for each connection:

    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

  12. #12
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by ra78 View Post
    But how to server recognize what clients wants?

    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 11:54 AM.

  13. #13
    ra78 is offline Member
    Join Date
    Jun 2010
    Posts
    6
    Rep Power
    0

    Default

    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
    public synchronized void handle(int ID, String input) {
    if (input.equals(".bye")) {
    clients[findClient(ID)].send(".bye");
    remove(ID);
    Thanks guys! It was not clear to me how it works. Now it's ok!
    Regards

Similar Threads

  1. Requirements for building a SSL VPN
    By adityag in forum New To Java
    Replies: 0
    Last Post: 01-19-2010, 05:46 PM
  2. New to Java.. What are the requirements?
    By konn in forum New To Java
    Replies: 10
    Last Post: 03-27-2009, 01:50 PM
  3. External DTD requirements
    By jwilley44 in forum XML
    Replies: 0
    Last Post: 03-06-2009, 10:25 PM
  4. VOIP Server Hardware Requirements
    By asifsolkar in forum Networking
    Replies: 12
    Last Post: 05-21-2008, 05:23 AM
  5. Minimum system requirements
    By coco in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:19 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •