Results 1 to 4 of 4

Thread: thread problem

  1. #1
    Harris68 is offline Member
    Join Date
    Dec 2009
    Posts
    16
    Rep Power
    0

    Default thread problem

    this is my server code, the problem that I am facing is that I can my ArrayList is not accessble in for loop as you can see in my code "student is not recogized. how can I solve this problem? how can I access ArrayList in for loops to search for student grade?

    SERVER CODE

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package distributedsystem;
    
    /**
     *
     * @author Harris
     */
    
    import java.io.*;
    import java.net.*;
    import java.util.ArrayList;
    
    public class ServerThread {
    
            // the socket used by the server
            private ServerSocket serverSocket;
    
            // server constructor
            ServerThread(int port) {
    
                    /* create socket server and wait for connection requests */
                    try
                    {
                            serverSocket = new ServerSocket(port);
                            System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());
    
                            while(true)
                            {
                                    Socket socket = serverSocket.accept();  // accept connection
                                    TcpThread thread = new TcpThread(socket);    // make a thread of it
                                    thread.start();
                            }
                    }
                    catch (IOException e) {
                            System.out.println("Exception on new ServerSocket: " + e);
                    }
            }
    
    //        you must "run" server to have the server run as a console application
            public static void main(String[] arg) {
    
                  //Creating an ArrayList of Students with thier Fname, Lname, ID and Grade
                 ArrayList<Students> [COLOR="red"]students[/COLOR] = new ArrayList<Students>();
                 [COLOR="red"]students[/COLOR].add(new Students("Harris Bahazada", "w55555", "Grade A"));
                 [COLOR="red"]students[/COLOR].add(new Students("Shakib Ahmad", "w12212", "Grade B"));
                 [COLOR="red"]students[/COLOR].add(new Students("David John", "w54321", "Grade C"));
    
                // start server on port 1500
                    new ServerThread(8187);
            }
    
            /** One instance of this thread will run for each client */
            class TcpThread extends Thread {
                    // the socket where to listen/talk
                    Socket socket;
                    DataInputStream inp;
                    PrintStream outp;
                    String StudentID;
                    String grade;                
                    TcpThread(Socket socket) {
                            this.socket = socket;
                    }
                    public void run() {
                            /* Creating both Data Stream */
                            //System.out.println("Thread trying to create Object Input/Output Streams");
                            try
                            {
    
                                     //Stream to recieve clients data.
                                     inp = new DataInputStream(socket.getInputStream());
                                     //Stream to write data to client.
                                     outp = new PrintStream(socket.getOutputStream());
    
    
                                    String WelcomeNote= "You are connetcted to ths Server, Welcome!";
                                    outp.println(WelcomeNote);
    
                                     //Get StudentI from clientt. Read from client's stream.
                                     StudentID = inp.readLine();
                                     System.out.println(StudentID);
    
                                     boolean found = false;
    
                                     for(int i = 0; i<[COLOR="red"]students[/COLOR].size() && !found; i++)
                                     {
                                        if([COLOR="red"]students[/COLOR].get(i).getId().equalsIgnoreCase(StudentID))
                                        found = true;
                                        //if the StudentID matches in DataBase, then take the grade for corresponding StudentID
                                        grade = ("Grade for StudentID " + StudentID +" is : "+[COLOR="Red"]students[/COLOR].get(i).getGrade());
                                     }
    
                                         if(found)
                                          {
                                             outp.println(grade);
                                            }
                                         else
                                           {
                                              outp.println("StudentID Not Valid");
                                            }
    
                            }
                            catch (IOException e) {
                                    System.out.println("Exception creating new Input/output Streams: " + e);
                                    return;
                            }
    
                            finally {
                                    try {
                                            outp.close();
                                            inp.close();
                                    }
                                    catch (Exception e) {
                                    }
                            }
                    }
            }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you declare a variable in the main method (or in any method), it will only be visible in that method. So declare it somewhere else where it will have a greater scope.

  3. #3
    Harris68 is offline Member
    Join Date
    Dec 2009
    Posts
    16
    Rep Power
    0

    Default

    I tired it this way now the code is running but the issu I am facing now is that in both case if the SudentID is correct or incorrect the server always send the message " StudentID not Valid" .

    any suggestions please?

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package distributedsystem;
    
    /**
     *
     * @author Harris
     */
    
    import java.io.*;
    import java.net.*;
    import java.util.ArrayList;
    
    public class ServerThread {
    
            // the socket used by the server
            private ServerSocket serverSocket;
    
            // server constructor
            ServerThread(int port) {
    
                    /* create socket server and wait for connection requests */
                    try
                    {
                            serverSocket = new ServerSocket(port);
                            System.out.println("Server waiting for client on port " + serverSocket.getLocalPort());
    
                            while(true)
                            {
                                    Socket socket = serverSocket.accept();  // accept connection
                                    TcpThread thread = new TcpThread(socket);    // make a thread of it
                                    thread.start();
                            }
                    }
                    catch (IOException e) {
                            System.out.println("Exception on new ServerSocket: " + e);
                    }
            }
    
    //        you must "run" server to have the server run as a console application
            public static void main(String[] arg) {
    
                  //Creating an ArrayList of Students with thier Fname, Lname, ID and Grade
    
    
                // start server on port 1500
                    new ServerThread(8187);
            }
    
            /** One instance of this thread will run for each client */
            class TcpThread extends Thread {
                    // the socket where to listen/talk
                    
                    Socket socket;
                    DataInputStream inp;
                    PrintStream outp;
                    String StudentID;
                    String grade;                
                    TcpThread(Socket socket) {
                            this.socket = socket;
                    }
                    public void run() {
                            /* Creating both Data Stream */
                            //System.out.println("Thread trying to create Object Input/Output Streams");
                            try
                            {
    
                               ArrayList<Students> students = new ArrayList<Students>();
                               students.add(new Students("Harris Bahazada", "w55555", "Grade A"));
                               students.add(new Students("Shakib Ahmad", "w12212", "Grade B"));
                               students.add(new Students("David John", "w54321", "Grade C"));
    
    
    
                                     //Stream to recieve clients data.
                                     inp = new DataInputStream(socket.getInputStream());
                                     //Stream to write data to client.
                                     outp = new PrintStream(socket.getOutputStream());
    
    
                                    String WelcomeNote= "You are connetcted to ths Server, Welcome!";
                                    outp.println(WelcomeNote);
    
                                     //Get StudentI from clientt. Read from client's stream.
                                     StudentID = inp.readLine();
                                     System.out.println(StudentID);
    
                                     boolean found = false;
    
                                     for(int i = 0; i<students.size() && !found; i++)
                                     {
                                        if(students.get(i).getId().equalsIgnoreCase(StudentID))
                                        found = true;
                                        //if the StudentID matches in DataBase, then take the grade for corresponding StudentID
                                        grade = ("Grade for StudentID " + StudentID +" is : "+students.get(i).getGrade());
                                     }
    
                                         if(found)
                                          {
                                             outp.println(grade);
                                            }
                                         else
                                           {
                                              outp.println("StudentID Not Valid");
                                            }
    
                            }
                            catch (IOException e) {
                                    System.out.println("Exception creating new Input/output Streams: " + e);
                                    return;
                            }
    
                            finally {
                                    try {
                                            outp.close();
                                            inp.close();
                                    }
                                    catch (Exception e) {
                                    }
                            }
                    }
            }
    }
    in my server without thread its working correctly but in mulithread I am facing this problem. in my server I put the arraylist in main().

  4. #4
    Harris68 is offline Member
    Join Date
    Dec 2009
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Problem with a thread
    By dotabyss in forum Threads and Synchronization
    Replies: 3
    Last Post: 02-14-2010, 10:56 PM
  2. [SOLVED] Can't figure out my thread/awt problem
    By Zosden in forum Advanced Java
    Replies: 7
    Last Post: 07-29-2008, 05:13 PM
  3. [SOLVED] Thread in GUI Problem
    By terox13 in forum Threads and Synchronization
    Replies: 3
    Last Post: 05-27-2008, 08:32 PM
  4. [SOLVED] HELP! Thread Problem
    By nvidia in forum NetBeans
    Replies: 2
    Last Post: 05-24-2008, 04:56 AM
  5. Problem using thread +rmi in my homework
    By IbrahimAbbas in forum Threads and Synchronization
    Replies: 10
    Last Post: 04-14-2008, 09:24 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
  •