Results 1 to 5 of 5
  1. #1
    Felic is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Threads per Connection or Threads per Request

    Hey

    I'm currently going on my 2nd year at University, and we have been given a code for an ATM. One of the key assignments is describing the theory behind the program in terms of a distributed system.

    We're trying to figure out the program currently creates a thread per connection or per request. Anyone who could tell me the difference here?

    This is the TCP Class which we use on the server.

    Java Code:
    package comm.serverside;
    
    import java.io.EOFException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.net.ServerSocket;
    import java.net.Socket;
    
    import comm.clientandserverside.BankDBMsg;
    
    public class TCPServer extends Server {
    	
    	public TCPServer(){
    		super();
    		System.out.println("TCP server startet");
    	}
    
    	public void inOut(BankDatabaseServerProxy bankDataBaseServerProxy) {
    		ServerSocket listenSocket = null;
    
    		try {
    			listenSocket = new ServerSocket(55555);
    			// create socket at agreed port
    			byte[] buffer;
    			while (true) {
    				transactionCounter++;
    				System.err.println("Waiting for a client...");
    				Socket clientSocket = listenSocket.accept();
    				System.err
    						.println("Just accepted a connection on clientSocket");
    				Connection c = new Connection(bankDataBaseServerProxy,
    						clientSocket);
    			}
    		} catch (IOException e) {
    			System.out.println("Listen :" + e.getMessage());
    		}
    	}
    
    }
    
    class Connection extends Thread {
    	ObjectInputStream ois;
    	ObjectOutputStream oos;
    	Socket clientSocket;
    	BankDatabaseServerProxy bankDataBaseServerProxy;
    
    	public Connection(BankDatabaseServerProxy bankDataBaseServerProxy,
    			Socket aClientSocket) {
    		this.bankDataBaseServerProxy = bankDataBaseServerProxy;
    		try {
    			clientSocket = aClientSocket;
    			ois = new ObjectInputStream(clientSocket.getInputStream());
    			oos = new ObjectOutputStream(clientSocket.getOutputStream());
    			this.start(); // Nu er denne thread RUNNABLE
    		} catch (IOException e) {
    			System.out.println("Connection:" + e.getMessage());
    		}
    	}
    
    	public void run() {
    		while (true) {
    			try {
    				System.err.println("Waiting to read data...");
    				BankDBMsg bankDBM;
    				try {
    					bankDBM = (BankDBMsg) ois.readObject();
    					BankDBMsg bankDBMsgReply = bankDataBaseServerProxy
    							.receive(bankDBM);
    					oos.writeObject(bankDBMsgReply);
    				} catch (ClassNotFoundException e) {
    					e.printStackTrace();
    				}
    			} catch (EOFException e) {
    				System.out.println("EOF:" + e.getMessage());
    			} catch (IOException e) {
    				System.out.println("IO:" + e.getMessage());
    			} finally {
    				try {
    					clientSocket.close();
    				} catch (IOException e) {/* close failed */
    				}
    			}
    		}
    
    	}
    
    }
    TCPClient class looks like

    Java Code:
    package comm.clientside;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.OutputStream;
    import java.net.InetAddress;
    import java.net.Socket;
    import java.net.SocketException;
    
    import comm.clientandserverside.BankDBMsg;
    import comm.clientandserverside.CommUtil;
    
    public class TCPClient extends Client {
    
    	public TCPClient(String theServerHostAdr) {
    		super(theServerHostAdr);
    		System.out.println("TCP klient startet");
        }
    
    	// TCP
    	public byte[] send(byte[] bytesToSend) {
    		byte[] bytesReceived = null;
    		Socket tcpSocket = null;
    		try {
    			InetAddress aHost = InetAddress.getByName(host);
    			int tcpServerPort = 55555;
    			tcpSocket = new Socket(aHost, tcpServerPort);
    			System.err.println("Local port:" + tcpSocket.getPort());
    
    			OutputStream oStream = tcpSocket.getOutputStream();
    			ObjectOutputStream ooStream = new ObjectOutputStream(oStream);
    			ooStream.writeObject((BankDBMsg) CommUtil.unSerialize(bytesToSend)); // send
    																					// serilized
    																					// payload
    
    			// be ready to accept answer from server
    			InputStream is = tcpSocket.getInputStream();
    
    			ObjectInputStream ois = new ObjectInputStream(is);
    			try {
    				Object o = ois.readObject();
    				bytesReceived = CommUtil.serialize(o);
    			} catch (ClassNotFoundException e) {
    				e.printStackTrace();
    			}
    		} catch (SocketException e) {
    			System.out.println("Socket: " + e.getMessage());
    		} catch (IOException e) {
    			System.out.println("IO: " + e.getMessage());
    		} finally {
    			if (tcpSocket != null)
    				try {
    					tcpSocket.close(); // 
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    		}
    		return bytesReceived;
    	}
    
    }
    Anything would be helpful. Thanks. I can upload the entire code as a file or give you more code if needed.

    David
    Last edited by Felic; 11-22-2011 at 06:38 PM.

  2. #2
    Felic is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Re: Threads per Connection or Threads per Request

    Oh and if someone could tell me how to perhaps insert the code in some kind of box? Im sure there must be some kind of tag or something to make it scrollable and a bit more neat to look at.

    Java Code:
    test
    Found it :)
    Last edited by Felic; 11-22-2011 at 06:26 PM.

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

    Default Re: Threads per Connection or Threads per Request

    Anyone who could tell me the difference here?
    That seems logical to make a connection for a request.
    Can you explain what your problem is?

  4. #4
    Felic is offline Member
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default Re: Threads per Connection or Threads per Request

    Quote Originally Posted by Norm View Post
    That seems logical to make a connection for a request.
    Can you explain what your problem is?
    Yeah, I see I might have missed a word or two in my previous post.

    I'm trying to understand what the difference between a thread being made per connection, or a thread being made per request. I'm trying to understand the code, since it seems like it actually doesn't close the thread(s) after connection is closed.

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

    Default Re: Threads per Connection or Threads per Request

    A new thread implies that the code in that thread can run in parallel with code in other threads.
    Can requests be run in parallel? It would depend on the application. For banking you'd want your deposit to finish before any other operations.
    A connection sort of implies a different customer. Then you'd expect each customer's operations to be independent of any other customer's. Again, it could depend on your app.
    For a customer to continue using the same connection for more requests seems reasonable.

Similar Threads

  1. Threads!!!
    By smartino in forum New To Java
    Replies: 67
    Last Post: 11-13-2011, 06:24 PM
  2. Help with Threads
    By gicp89 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-01-2011, 10:00 PM
  3. GUI and Threads
    By rp181 in forum Threads and Synchronization
    Replies: 1
    Last Post: 10-10-2009, 09:39 PM
  4. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 11:25 AM
  5. 2 threads sharing a data base connection
    By Ed in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 05:41 AM

Posting Permissions

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