如何在多线程套接字服务器的线程之间同步arraylist

时间:2019-04-12 23:23:45

标签: multithreading sockets arraylist server synchronization

我正在尝试构建没有并发问题的客户端服务器应用程序。目前,我正在从每个客户端接收数据流,并将数据存储在数组列表中,然后在每个serverthread中,循环遍历数组列表并将信息中继回客户端。然后,客户端根据中继回它们的服务器信息(它们的名称与目标)来匹配值。是否有更好的套接字之间同步信息共享方法,我正在尝试使用服务器作为集中点,每个客户端与每个客户端共享大约7个变量。

public class Server {


    public static String StatusServer = "idle";
    public static ArrayList<String> AccountDetails = new ArrayList<>();

    public static void main(String args[]){

        Socket s=null;
        ServerSocket ss2=null;
        System.out.println("Server Listening......");
        try{
            ss2 = new ServerSocket(4445); // can also use static final PORT_NUM , when defined

        }
        catch(IOException e){
            e.printStackTrace();
            System.out.println("Server error");

        }

        while(true){
            try{
                s= ss2.accept();
                System.out.println("connection Established");
                ServerThread st=new ServerThread(s);
                st.start();
            }
            catch(Exception e){
                e.printStackTrace();
                System.out.println("Connection Error");

            }
        }

    }

}

class ServerThread extends Thread{

    String line=null;
    String line1;
    BufferedReader  is = null;
    PrintWriter os=null;
    Socket s=null;

    public ServerThread(Socket s){
        this.s=s;
    }

    public void run() {
        try{
            is= new BufferedReader(new InputStreamReader(s.getInputStream()));
            os=new PrintWriter(s.getOutputStream());

        }catch(IOException e){
            System.out.println("IO error in server thread");
        }

        try {
            line=is.readLine();
            if (line != null) {
                Server.AccountDetails.add(line);
            }
            while(line.compareTo("QUIT")!=0){
                line1 = line;
                int position = 0;
                for (String curVal : Server.AccountDetails){
                    String curVal1 = curVal.substring(0, 20);
                    String searchString =  line.substring(0, 6);
                    if (curVal1.contains(searchString)){
                        Server.AccountDetails.set(position, line);
                    }
                    position++;
                }
                os.println(Server.AccountDetails);
                os.flush();
                line=is.readLine();
                try
                {
                    Thread.sleep(250);//2 seconds
                }
                catch(InterruptedException ie){
                    ie.printStackTrace();
                }
            }
        } catch (IOException e) {

            System.out.println("IO Error/ Clientterminated abruptly");
        }
        catch(NullPointerException e){
            System.out.println("Client Closed");
        }

        finally{
            try{
                System.out.println("Connection Closing..");
                if(Server.AccountDetails.size() >0) {
                    int position = 0;
                    for (String curVal : Server.AccountDetails){
                        System.out.println(this.getName() + ": " +curVal);
                        String curVal1 = curVal.substring(0, 20);
                        System.out.println("Curval1 " +curVal1);
                        System.out.println("Line: "+line1);
                        String searchString =  line1.substring(0, 8);
                        System.out.println("Search String "+searchString);
                        if (curVal1.contains(searchString)){
                            System.out.println("Removing "+ Server.AccountDetails.get(position));
                            Server.AccountDetails.remove(position);
                        }
                        position++;
                    }
                }
                try
                {
                    Thread.sleep(250);//2 seconds
                }
                catch(InterruptedException ie){
                    ie.printStackTrace();
                }
                if (is!=null){
                    is.close();
                    System.out.println(" Socket Input Stream Closed");
                }

                if(os!=null){
                    os.close();
                    System.out.println("Socket Out Closed");
                }
                if (s!=null){
                    s.close();
                    System.out.println("Socket Closed");
                }

            }
            catch(IOException ie){
                System.out.println("Socket Close Error");
            }
        }//end finally
    }
}

0 个答案:

没有答案