Results 1 to 1 of 1
- 03-07-2011, 10:20 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 9
- Rep Power
- 0
Simple question about multi-threading
I'm trying to understand multi-threading. Can someone explain how this works:
When you stream info back and forth from client and server, is the order of that data as important as the following snippet (full code below) would seem to indicate?
Full code for the server:Java Code://snippet DataInputStream inputFromClient = new DataInputStream(socket.getInputStream()); DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream()); while(true) { double interestRate = inputFromClient.readDouble(); //so does the client therefore HAVE to send the interestRate value first? int years = inputFromClient.readInt(); double amount = inputFromClient.readDouble();
Thanks for any tips!Java Code:import java.io.*; import java.net.*; import java.util.*; import java.awt.*; import javax.swing.*; public class Q303 extends JFrame { private JTextArea jta = new JTextArea(); public static void main(String args[]) { new Q303(); } public Q303() { setLayout(new BorderLayout()); add(new JScrollPane(jta), BorderLayout.CENTER); setTitle("Server"); setSize(500,300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); try { ServerSocket serverSocket = new ServerSocket(8000); jta.append("Server started at " + new Date() + '\n'); int clientNo = 1; while(true) { Socket socket = serverSocket.accept(); jta.append("starting thread for client " + clientNo + " at " + new Date() + '\n'); InetAddress inetAddress = socket.getInetAddress(); jta.append("Client " + clientNo + "'s host name is " + inetAddress.getHostName() + '\n'); jta.append("Client " + clientNo + "'s IP address is " + inetAddress.getHostAddress() + '\n'); HandleAClient task = new HandleAClient(socket); new Thread(task).start(); clientNo++; } } catch(IOException ex) { System.out.println(ex); } } class HandleAClient implements Runnable { private Socket socket; public HandleAClient(Socket socket) { this.socket = socket; } public void run() { try { DataInputStream inputFromClient = new DataInputStream(socket.getInputStream()); DataOutputStream outputToClient = new DataOutputStream(socket.getOutputStream()); while(true) { double interestRate = inputFromClient.readDouble(); int years = inputFromClient.readInt(); double amount = inputFromClient.readDouble(); double loanTotal = amount * interestRate; double monthlyPayment = loanTotal / (12 * years); outputToClient.writeDouble(monthlyPayment); jta.append("Interest Rate received from client: " + interestRate + '\n' ); jta.append("Years received from client: " + years + '\n' ); jta.append("Amount received from client: " + amount + '\n' ); jta.append("Monthly payment caluclated: " + monthlyPayment + '\n' ); jta.append("Sending calculation to client..."); } } catch(IOException e) { System.err.println(e); } } } }
Similar Threads
-
Need Help! Multi-Threading question!
By pinkette in forum New To JavaReplies: 8Last Post: 01-13-2011, 07:08 PM -
null pointer exception in multi threading
By niteangell21 in forum New To JavaReplies: 5Last Post: 10-20-2010, 05:48 PM -
Problem in Multi threading.
By Chetans in forum Advanced JavaReplies: 3Last Post: 03-23-2010, 04:42 PM -
Newbie to multi-threading please direct me :)
By kminev in forum Threads and SynchronizationReplies: 8Last Post: 11-18-2009, 06:11 PM -
question about Multi threading in Java
By fred in forum Advanced JavaReplies: 1Last Post: 07-24-2007, 01:55 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks