Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Distinguish between different Threads

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Distinguish between different Threads

    Hi
    I making a program that a client connect to a server, then it's starts changing information throw the socket.
    the info is String.
    when connection with one open client everything is working great.
    the problem starts when I connect 2 or more clients simultaneously.
    the server doesn't know how to handle each request so it's send info to both the client info that is wrong.
    if I run several clients and then execute the last client that opens he will work fine the others will crush.
    on the server i'm getting connection reset.
    the problem i believe is with the closing socket and thread holding.
    what can be the solution?
    thanks


  2. #2
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Distinguish between different Threads

    Hi
    I making a program that a client connect to a server, then it's starts changing information throw the socket.
    the info is String.
    when connection with one open client everything is working great.
    the problem starts when I connect 2 or more clients simultaneously.
    the server doesn't know how to handle each request so it's send info to both the client info that is wrong.
    if I run several clients and then execute the last client that opens he will work fine the others will crush.
    on the server i'm getting connection reset.
    the problem i believe is with the closing socket and thread holding.
    what can be the solution?
    thanks

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Distinguish between different Threads

    what can be the solution?
    Find the bugs and fix them.
    Without seeing code that compiles, executes and shows the problem, it is hard to know what is wrong.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Distinguish between different Threads

    you are right. here is the Server Side of the code.


     
     
    import java.io.*;
    import java.net.*;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    import javax.swing.JOptionPane;
    public class ServerSide {
    	private static Scanner in ;
    	private static String pass;
    	public static String id;
    	private static FileWriter writer;
    	private Socket socket	= null;
    	private static PrintWriter printwriter;
    	private static String correct;
    	private static File file;
    	private static BufferedReader bufferedreader;
    	private static InputStreamReader inputstreamreader;
    	private String notExists = "Wrong ID user not Exsists";
    	private int count=0;
    	private boolean checkFile=true;
     
    	public static void main(String[] args) throws IOException, ClassNotFoundException {
    		// TODO Auto-generated method stub
    		new ServerSide();
     
    	}
    		public ServerSide() throws ClassNotFoundException{
    			socket	= null;
    			ServerSocket serverSocket = null;
    		try{
    		 serverSocket = new ServerSocket(8000);
    		 System.out.println("Server Started");
     
     
    		while(true){
    			socket = serverSocket.accept();
    			HandleAClient task = new HandleAClient(socket);
    			new Thread(task).start();
     
     
    		}
     
     
    		}
    		catch(UnknownHostException unhe){
    		      System.out.println("UnknownHostException: " + unhe.getMessage());
    		    }catch(InterruptedIOException intioe){
    		      System.out.println("Timeout while attempting to establish socket connection.");
    		    }catch(IOException ioe){
    		      System.out.println("IOException: " + ioe.getMessage());
    		    }finally{
    		      try{
    		        socket.close();
    		        serverSocket.close();
    		      }catch(IOException ioe){
    		        System.out.println("IOException: " + ioe.getMessage());
    		      }
    		    }
     
     
    	}
     
     
    	class HandleAClient implements Runnable{
     
    		private Socket socket;
     
    		public HandleAClient(Socket socket){
     
    			this.socket = socket;
     
    		}
     
    		@Override
     
    		public void run(){
     
    			try{
    				count++;
    				System.out.println("User number: " + count + " connected");
    				inputstreamreader = new InputStreamReader(socket.getInputStream());
    			    bufferedreader = new BufferedReader(inputstreamreader);
    			    printwriter = new PrintWriter(socket.getOutputStream(),true);
    				boolean cancel = true;
    				while(cancel){
    					String choiceUser = bufferedreader.readLine();
    					System.out.println(choiceUser);
    					id = bufferedreader.readLine();
    					if(Integer.parseInt(choiceUser) == 2){
     
    						//check file exists??
    						boolean checkFile = fileExists(id);
     
     
    						if(checkFile)
    						{
    							printwriter.println("Good To Work");
    							in = new Scanner(file);
    							correct = in.next();
    							printwriter.println(correct);
    						//	idChecker(id);
     
    							String answer = bufferedreader.readLine();
    							if(answer.equals("Continue")){
    							int change = Integer.parseInt(bufferedreader.readLine());
    							//System.out.println(change);
    							if(change==0)
    								newSalary();
    								else
    								{
    									runColculation();
    								}
     
     
    							cancel= false;
     
    							}
    							else
    								continue;
    						}
     
    						else
    							printwriter.println(notExists);
     
    					}
    					else
    						if(Integer.parseInt(choiceUser) == 1){
    							checkFile = fileExists(id);
    							//System.out.println(checkFile);
    							if(checkFile)
    							{
    								printwriter.println("OK");
    								idChecker(id);
    								String answer = bufferedreader.readLine();
    								if(answer.equals("Continue")){
    								int change = Integer.parseInt(bufferedreader.readLine());
    								System.out.println(change);
    								if(change==0)
    									newSalary();
    									else
    									{
    										runColculation();
    									}
     
     
    									cancel= false;
    								}
    								else
    									continue;
     
    							}
    							else
    							{
    								System.out.println(checkFile);
    								printwriter.println("New");
    								file.createNewFile();
    								pass = bufferedreader.readLine();
    								correct = pass;
    								newSalary();
     
    							}
     
     
    							cancel= false;
    						}
    						else
    							cancel = false;
     
     
     
     
     
    				}
    				  bufferedreader.close();
    			      inputstreamreader.close();
    			      printwriter.close();
    			}
    			catch(IOException ex)
    			{
    				System.err.println(ex);
    			}
     
    		}
     
     
    	}
     
    	//checks the file
     
    	public boolean fileExists(String id){
     
    		ServerSide.file = new File(id + ".txt");
    		if(ServerSide.file.exists())
    			return true;
    		else
    			return false;
     
    	}
     
    	//checks ID
     
    	public void idChecker(String id) throws IOException {
     
    		ServerSide.file = new File(id+".txt");
     
    		if(file.exists()){
    			in = new Scanner(file);
    			correct = in.next();
    			printwriter.println(correct);
    		}
    		else
    			file.createNewFile();
    }
     
    		//writes it down all the user input that is needed
    	public void newSalary() throws IOException{
    		Scanner in = new Scanner(file);
    		String everything="";
    		while(in.hasNext()){
    			everything =  everything + in.next() +" ";
    		}
    		in.close();
    		System.out.println(everything);
    		writer = new FileWriter(ServerSide.file); 
     
    		writer.write(correct + " ");
     
    		String choice = bufferedreader.readLine();
    		if(Integer.parseInt(choice)==3 && checkFile){
     
    			//System.out.println("FSDFSD");
    			writer.write(everything.substring(everything.indexOf(" "), everything.length()-1));
     
     
    		}
    		else
    			if(Integer.parseInt(choice)==3 && !checkFile){
     
    				writer.close();
    				boolean d = file.delete();
     
    				System.out.println(d);
     
    			}
    		else{
     
    		System.out.println("fdsf");
    		writer.write(choice + " ");
    		//System.out.println("fsdfsd");
     
    		String year = bufferedreader.readLine();
    		writer.write(year);
     
    		String Salary = bufferedreader.readLine();
    		writer.write(Salary);
     
    		String bus = bufferedreader.readLine();
    		writer.write(bus);
     
    		String eshel = bufferedreader.readLine();
    		writer.write(eshel);
     
    		String credit = bufferedreader.readLine();
    		writer.write(credit);
     
    	//	System.out.println(credit);
     
     
    		}
    		writer.close();
     
    	}
     
    	//get's the user details from the file
     
    	public void runColculation() throws FileNotFoundException{
     
    		in = new Scanner(file);
    		ArrayList<String> values = new ArrayList<String>();
    		while(in.hasNext()){
    			values.add(in.next());
    		}
     
    		int choice = Integer.parseInt(values.get(1));
    		int year = Integer.parseInt(values.get(2));
    		String salary = values.get(3);
    		String bus = (values.get(4));
    		String eshel = (values.get(5));
    		String credit = (values.get(6));
     
     
    		printwriter.println(choice);
    		printwriter.println(year);
    		printwriter.println(salary);
    		printwriter.println(bus);
    		printwriter.println(eshel);
    		printwriter.println(credit);
     
     
    	}
     
     
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Distinguish between different Threads

    How can the code be executed for testing?

    Is there a script for testing the code?

    One possible problem I see is the use of static variables. Their use prevents the thread for each client from being isolated from the other client's threads.
    Get rid of all static variables. Give a private one to each thread.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Distinguish between different Threads

    sorry about that. I new to the forum and java
    I'll try to remove the static one, thanks!
    i will upload the rest of the program later when get home..

    --- Update ---

    Server Side:
     
     
    import java.io.*;
    import java.net.*;
    import java.util.ArrayList;
    import java.util.Scanner;
     
    import javax.swing.JOptionPane;
    public class ServerSide {
    	private static Scanner in ;
    	private static String pass;
    	public static String id;
    	private static FileWriter writer;
    	private Socket socket	= null;
    	private static PrintWriter printwriter;
    	private static String correct;
    	private static File file;
    	private static BufferedReader bufferedreader;
    	private static InputStreamReader inputstreamreader;
    	private String notExists = "Wrong ID user not Exsists";
    	private int count=0;
    	private boolean checkFile=true;
     
    	public static void main(String[] args) throws IOException, ClassNotFoundException {
    		// TODO Auto-generated method stub
    		new ServerSide();
     
    	}
    		public ServerSide() throws ClassNotFoundException{
    			socket	= null;
    			ServerSocket serverSocket = null;
    		try{
    		 serverSocket = new ServerSocket(8000);
    		 System.out.println("Server Started");
     
     
    		while(true){
    			socket = serverSocket.accept();
    			HandleAClient task = new HandleAClient(socket);
    			new Thread(task).start();
     
     
    		}
     
     
    		}
    		catch(UnknownHostException unhe){
    		      System.out.println("UnknownHostException: " + unhe.getMessage());
    		    }catch(InterruptedIOException intioe){
    		      System.out.println("Timeout while attempting to establish socket connection.");
    		    }catch(IOException ioe){
    		      System.out.println("IOException: " + ioe.getMessage());
    		    }finally{
    		      try{
    		        socket.close();
    		        serverSocket.close();
    		      }catch(IOException ioe){
    		        System.out.println("IOException: " + ioe.getMessage());
    		      }
    		    }
     
     
    	}
     
     
    	class HandleAClient implements Runnable{
     
    		private Socket socket;
     
    		public HandleAClient(Socket socket){
     
    			this.socket = socket;
     
    		}
     
    		@Override
     
    		public void run(){
     
    			try{
    				count++;
    				System.out.println("User number: " + count + " connected");
    				inputstreamreader = new InputStreamReader(socket.getInputStream());
    			    bufferedreader = new BufferedReader(inputstreamreader);
    			    printwriter = new PrintWriter(socket.getOutputStream(),true);
    				boolean cancel = true;
    				while(cancel){
    					String choiceUser = bufferedreader.readLine();
    					System.out.println(choiceUser);
    					id = bufferedreader.readLine();
    					if(Integer.parseInt(choiceUser) == 2){
     
    						//check file exists??
    						boolean checkFile = fileExists(id);
     
     
    						if(checkFile)
    						{
    							printwriter.println("Good To Work");
    							in = new Scanner(file);
    							correct = in.next();
    							printwriter.println(correct);
    						//	idChecker(id);
     
    							String answer = bufferedreader.readLine();
    							if(answer.equals("Continue")){
    							int change = Integer.parseInt(bufferedreader.readLine());
    							//System.out.println(change);
    							if(change==0)
    								newSalary();
    								else
    								{
    									runColculation();
    								}
     
     
    							cancel= false;
     
    							}
    							else
    								continue;
    						}
     
    						else
    							printwriter.println(notExists);
     
    					}
    					else
    						if(Integer.parseInt(choiceUser) == 1){
    							checkFile = fileExists(id);
    							//System.out.println(checkFile);
    							if(checkFile)
    							{
    								printwriter.println("OK");
    								idChecker(id);
    								String answer = bufferedreader.readLine();
    								if(answer.equals("Continue")){
    								int change = Integer.parseInt(bufferedreader.readLine());
    								System.out.println(change);
    								if(change==0)
    									newSalary();
    									else
    									{
    										runColculation();
    									}
     
     
    									cancel= false;
    								}
    								else
    									continue;
     
    							}
    							else
    							{
    								System.out.println(checkFile);
    								printwriter.println("New");
    								file.createNewFile();
    								pass = bufferedreader.readLine();
    								correct = pass;
    								newSalary();
     
    							}
     
     
    							cancel= false;
    						}
    						else
    							cancel = false;
     
     
     
     
     
    				}
    				  bufferedreader.close();
    			      inputstreamreader.close();
    			      printwriter.close();
    			}
    			catch(IOException ex)
    			{
    				System.err.println(ex);
    			}
     
    		}
     
     
    	}
     
    	//checks the file
     
    	public boolean fileExists(String id){
     
    		ServerSide.file = new File(id + ".txt");
    		if(ServerSide.file.exists())
    			return true;
    		else
    			return false;
     
    	}
     
    	//checks ID
     
    	public void idChecker(String id) throws IOException {
     
    		ServerSide.file = new File(id+".txt");
     
    		if(file.exists()){
    			in = new Scanner(file);
    			correct = in.next();
    			printwriter.println(correct);
    		}
    		else
    			file.createNewFile();
    }
     
    		//writes it down all the user input that is needed
    	public void newSalary() throws IOException{
    		Scanner in = new Scanner(file);
    		String everything="";
    		while(in.hasNext()){
    			everything =  everything + in.next() +" ";
    		}
    		in.close();
    		System.out.println(everything);
    		writer = new FileWriter(ServerSide.file); 
     
    		writer.write(correct + " ");
     
    		String choice = bufferedreader.readLine();
    		if(Integer.parseInt(choice)==3 && checkFile){
     
    			//System.out.println("FSDFSD");
    			writer.write(everything.substring(everything.indexOf(" "), everything.length()-1));
     
     
    		}
    		else
    			if(Integer.parseInt(choice)==3 && !checkFile){
     
    				writer.close();
    				boolean d = file.delete();
     
    				System.out.println(d);
     
    			}
    		else{
     
    		System.out.println("fdsf");
    		writer.write(choice + " ");
    		//System.out.println("fsdfsd");
     
    		String year = bufferedreader.readLine();
    		writer.write(year);
     
    		String Salary = bufferedreader.readLine();
    		writer.write(Salary);
     
    		String bus = bufferedreader.readLine();
    		writer.write(bus);
     
    		String eshel = bufferedreader.readLine();
    		writer.write(eshel);
     
    		String credit = bufferedreader.readLine();
    		writer.write(credit);
     
    	//	System.out.println(credit);
     
     
    		}
    		writer.close();
     
    	}
     
    	//get's the user details from the file
     
    	public void runColculation() throws FileNotFoundException{
     
    		in = new Scanner(file);
    		ArrayList<String> values = new ArrayList<String>();
    		while(in.hasNext()){
    			values.add(in.next());
    		}
     
    		int choice = Integer.parseInt(values.get(1));
    		int year = Integer.parseInt(values.get(2));
    		String salary = values.get(3);
    		String bus = (values.get(4));
    		String eshel = (values.get(5));
    		String credit = (values.get(6));
     
     
    		printwriter.println(choice);
    		printwriter.println(year);
    		printwriter.println(salary);
    		printwriter.println(bus);
    		printwriter.println(eshel);
    		printwriter.println(credit);
     
     
    	}
     
     
    }

    here is the client side:

     
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.InterruptedIOException;
    import java.io.PrintWriter;
    import java.net.Socket;
    import java.net.UnknownHostException;
     
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;
    import javax.swing.JRadioButton;
    import javax.swing.JTextField;
    import javax.swing.border.TitledBorder;
     
     
    public class TestFrames extends JFrame{
     
    	private JButton jSubmit;
    	private JButton jNew;
    	private JTextField jUser;
    	private JPasswordField jPass;
    	private JLabel jlText = new JLabel("ID Number:");
    	private JLabel jlPass = new JLabel("Password:");
    	private JPanel jAll;
    	private String password="";
    	private static PrintWriter writer;
    	private static BufferedReader reader;
    	public static JFrame frame = new JFrame();
    	private Socket socket;
    	private static InputStreamReader inputstreamreader;
    	private static Salary obj;
    	private String id;
    	private JPanel jnew;
    	private JPanel jCenter;
    	private JPanel jLeft;
    	private boolean newUser = false;
     
    	public static void main(String[] args) throws IOException{
    		// TODO Auto-generated method stub
    		TestFrames	t= new TestFrames();
     
    	}
    	public TestFrames() throws IOException{
    		try{
    			connectServer();
    			setUser();
    		}catch(UnknownHostException unhe){
    		      System.out.println("UnknownHostException: " + unhe.getMessage());
    	    }catch(InterruptedIOException intioe){
    	      System.out.println("Timeout while attempting to establish socket connection.");
    	    }catch(IOException ioe){
    	      System.out.println("IOException: " + ioe.getMessage());
    	    }
    	}
     
    	private void connectServer() throws IOException, IOException {
    		// TODO Auto-generated method stub
    		socket = new Socket("localhost", 8000);
    		inputstreamreader = new InputStreamReader(socket.getInputStream());
    	    reader = new BufferedReader(inputstreamreader);
    	    writer = new PrintWriter(socket.getOutputStream(),true);
     
    	}
     
    	public void setUser (){	
    		jSubmit = new JButton("Submit");
    		jNew = new JButton("New User");
    		jUser = new JTextField();
    		jPass = new JPasswordField(10);
    		jlText.setLabelFor(jUser);
    		jlPass.setLabelFor(jPass);
    		jAll = new JPanel(new GridLayout(0,2));
    		JPanel jpbutton = new JPanel(new FlowLayout(FlowLayout.CENTER));
    		jLeft = new JPanel(new GridLayout(2,0));
    		jCenter = new JPanel(new GridLayout(2,2));
    		jpbutton.add(jSubmit);
    		jnew = new JPanel(new FlowLayout(FlowLayout.CENTER));
    		jnew.add(jNew);
    		jSubmit.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed (ActionEvent e) {
    				// TODO Auto-generated method stub
    				id = jUser.getText();
    				char[] pass = jPass.getPassword();
     
    				for(int i=0;i<pass.length;i++){
     
    					password = password + pass[i];
     
    				}
    				if(!id.matches("\\d{9}")){
    					JOptionPane.showMessageDialog(null, "Please enter correct ID of 9 Digits");
    					jUser.requestFocusInWindow();
    				}
    				else{
    					if(newUser)
    						writer.println("1");
    					else
    						writer.println("2");
    				writer.println(id);
    				if(newUser){
    					//JOptionPane.showMessageDialog(null,"?FS");
    					//check file already on server;
    					String ServerAnswer;
    					try {
    						ServerAnswer = reader.readLine();
    						System.out.println(ServerAnswer);
    						if(ServerAnswer.equals("OK"))
    						{
    							String correct = reader.readLine();
    							System.out.println(correct);
    							if(pass(correct,password)){
    								writer.println("Continue");
    								changes();
    								frame.dispose();
    							}
    							else
    								{
    										JOptionPane.showMessageDialog(null, "Wrong user name or Password");
    										writer.println("Wait");
    										password = "";
    								}
     
    						}
    						else
    							if(ServerAnswer.equals("New"))
    							{
    								writer.println(password);
    								obj = new Salary(writer,frame);
    							}
     
     
    					} catch (IOException e1) {
    						// TODO Auto-generated catch block
    						e1.printStackTrace();
    					}
     
    				}
    				else{
    				String ServerAnswer;
    				try {
    					ServerAnswer = reader.readLine();
    					System.out.println(ServerAnswer);
    					if(ServerAnswer.equals("Wrong ID user not Exsists"))
    					{
    						JOptionPane.showMessageDialog(null, "Wrong ID user not Exsists");
    						//System.exit(1);
     
    					}
    					else
    					{
    					String correct = reader.readLine();
    					System.out.println(correct);
    					if(pass(correct,password)){
    						writer.println("Continue");
    						changes();
    						frame.dispose();
    					}
    					else
    						{
    								JOptionPane.showMessageDialog(null, "Wrong user name or Password");
    								writer.println("Wait");
    								password = "";
    						}
    					}
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
     
    				}
     
    				}
    			}
     
    			});
     
    		jNew.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				// TODO Auto-generated method stub
    				newUser=true;
    				frame.getContentPane().removeAll();
    				jLeft.setBorder(new TitledBorder("New User Registration"));
    				frame.add(jLeft);
     
    				frame.setTitle("New User");
    				frame.setSize(300, 150);
    				frame.setLocationRelativeTo(frame);
    				frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    				frame.setVisible(true);
    				System.out.println(newUser);
    			}
    		});
     
    		jCenter.add(jlText);
    		jCenter.add(jUser);
    		jCenter.add(jlPass);
    		jCenter.add(jPass);
    		jLeft.setBorder(new TitledBorder("User Exsists"));
    		jLeft.add(jCenter);
    		jLeft.add(jpbutton);
    		jAll.add(jLeft);
    		jAll.add(jnew);
     
    		frame.add(jAll);
    		frame.setTitle("Checker For Worker");
    		frame.setSize(300, 150);
    		frame.setLocation(0, 0);
    		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    		frame.setVisible(true);
     
    		frame.addWindowListener(new WindowAdapter() {
     
    			 public void windowClosing(WindowEvent e) {
    	                int i = JOptionPane.showConfirmDialog(frame,
    	                        "Are you sure to exit?", "Closing dialog",
    	                        JOptionPane.YES_NO_OPTION);
    	                if (i == JOptionPane.YES_OPTION) {
    	                    System.exit(0);
    	                }
    	            }
     
    		});
     
    	}
     
    	public static boolean pass(String correct, String pass) throws IOException
    	{
     
     
    		if(correct.equals(pass))
     
    			return true;
     
    		else{
     
    			return false;
    		}
     
     
    	}
     
    	public static void changes() throws IOException
    	{
    		frame.getContentPane().removeAll();
    		JLabel labelBack = new JLabel("Welcome Back! Let's Continue");
    		JPanel panelBack = new JPanel(new FlowLayout());
    		panelBack.add(labelBack);
    		//JOptionPane.showMessageDialog(null, "Welcome Back! \n Let's Continue");
    		JLabel labelText = new JLabel ("Were their any changes from the last time you signed in?");
    		JPanel panelText = new JPanel(new FlowLayout());
    		panelText.add(labelText);
    		final JRadioButton yes = new JRadioButton("Yes");
    		JRadioButton no = new JRadioButton("No");
    		ButtonGroup group = new ButtonGroup();
    		group.add(yes);
    		group.add(no);
    		JPanel answer = new JPanel(new FlowLayout());
    		answer.add(yes);
    		answer.add(no);
     
    		JPanel all = new JPanel(new GridLayout(4,0));
    		all.add(panelBack);
    		all.add(panelText);
    		all.add(answer);
     
    		final JFrame frame1 = new JFrame("Changes?");
    		frame1.add(all);
    		frame1.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    		frame1.setLocationRelativeTo(frame);
    		frame1.setSize(320,200);
    		frame1.setVisible(true);
    		//int change = JOptionPane.showConfirmDialog(null, "Were their any changes from \n the last time you signed in?");
     
    		frame1.addWindowListener(new WindowAdapter() {
     
    			 public void windowClosing(WindowEvent e) {
    	                int i = JOptionPane.showConfirmDialog(frame1,
    	                        "Are you sure to exit?", "Closing dialog",
    	                        JOptionPane.YES_NO_OPTION);
    	                if (i == JOptionPane.YES_OPTION) {
    	                    System.exit(0);
    	                }
    	            }
     
    		});
    		no.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				writer.println("1");
     
    					try {
    						obj = new Salary(writer,reader,frame);
    					} catch (IOException e1) {
    						// TODO Auto-generated catch block
    						e1.printStackTrace();
    					}
     
    					// TODO Auto-generated catch block
    					frame1.dispose();
     
    			}
    		});
     
    		yes.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				// TODO Auto-generated method stub
    				writer.println("0");
    				try {
    					obj = new Salary(writer,frame);
    				} catch (IOException e1) {
    					// TODO Auto-generated catch block
    					e1.printStackTrace();
    				}
    				frame1.dispose();
    			}
    		});
     
     
    	}
     
     
     
     
     
    }

    their is a few more class and methods you will need them also?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Distinguish between different Threads

    Each of the client threads started in the server must have its own objects. One easy way to see what needs to be changed would be to make the HandleAClient class external so it can NOT share any variables.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Feb 2014
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Distinguish between different Threads

    I can't believe it was such a thing!!
    Thanks!!!! for now it's all working!

Similar Threads

  1. regarding threads
    By chinnu&manu in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 6th, 2013, 07:56 AM
  2. Threads
    By RABNAWAZ in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 17th, 2013, 12:56 PM
  3. threads
    By peter_parker in forum Java Theory & Questions
    Replies: 3
    Last Post: April 1st, 2013, 01:49 PM
  4. Problem with threads
    By Spidey1980 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 20th, 2011, 09:04 AM
  5. threads
    By crazed8s in forum Threads
    Replies: 2
    Last Post: December 14th, 2010, 05:33 AM