dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Handle multiple threads with one server

  1. #1
    Join Date
    Dec 2018
    Posts
    2

    Handle multiple threads with one server

    I made a server to manage multiple clients. I create a new thread for each of them as you can see in my initModel(), and then all the threads goes in the run() method. The problem is that each client crashes after an undefined number of operation. The only error I get is this:
    Code:
    java[1133:134799] unrecognized type is 4294967295
    02
        java[1133:134799] *** Assertion failure in -[NSEvent _initWithCGEvent:eventRef:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1671.10.106/AppKit.subproj/NSEvent.m:1969
    03
        java[1133:134799] unrecognized type is 4294967295
    04
        java[1133:134799] *** Assertion failure in -[NSEvent _initWithCGEvent:eventRef:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit-1671.10.106/AppKit.subproj/NSEvent.m:1969
    05
        java[1133:134799] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid parameter not satisfying: _type > 0 && _type <= kCGSLastEventType'
    06
    *** First throw call stack:
    07
    (
    08
        0   CoreFoundation                      0x00007fff37e04e65 __exceptionPreprocess + 256
    09
        1   libobjc.A.dylib                     0x00007fff63e5b720 objc_exception_throw + 48
    10
        2   CoreFoundation                      0x00007fff37e1fab2 +[NSException raise:format:arguments:] + 98
    11
        3   Foundation                          0x00007fff3a1c3d1d -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 194
    12
        4   AppKit                              0x00007fff352ac029 -[NSEvent _initWithCGEvent:eventRef:] + 3272
    13
        5   AppKit                              0x00007fff355ff74c +[NSEvent eventWithCGEvent:] + 120
    14
        6   libglass.dylib                      0x000000011c0086fb listenTouchEvents + 59
    15
        7   SkyLight                            0x00007fff5df1141e _ZL19processEventTapDataPvjjjPhj + 148
    16
        8   SkyLight                            0x00007fff5de0fc9e _XPostEventTapData + 278
    17
        9   SkyLight                            0x00007fff5df1132c _ZL22eventTapMessageHandlerP12__CFMachPortPvlS1_ + 132
    18
        10  CoreFoundation                      0x00007fff37d657cf __CFMachPortPerform + 282
    19
        11  CoreFoundation                      0x00007fff37d656a9 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41
    20
        12  CoreFoundation                      0x00007fff37d65607 __CFRunLoopDoSource1 + 527
    21
        13  CoreFoundation                      0x00007fff37d4d689 __CFRunLoopRun + 2574
    22
        14  CoreFoundation                      0x00007fff37d4ca28 CFRunLoopRunSpecific + 463
    23
        15  HIToolbox                           0x00007fff36fe5b35 RunCurrentEventLoopInMode + 293
    24
        16  HIToolbox                           0x00007fff36fe5774 ReceiveNextEventCommon + 371
    25
        17  HIToolbox                           0x00007fff36fe55e8 _BlockUntilNextEventMatchingListInModeWithFilter + 64
    26
        18  AppKit                              0x00007fff352a1eb7 _DPSNextEvent + 997
    27
        19  AppKit                              0x00007fff352a0c56 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 1362
    28
        20  libglass.dylib                      0x000000011bff646c +[GlassApplication enterNestedEventLoopWithEnv:] + 172
    29
        21  libglass.dylib                      0x000000011bff6eaa Java_com_sun_glass_ui_mac_MacApplication__1enterNestedEventLoopImpl + 74
    30
        22  ???                                 0x0000000106546667 0x0 + 4401161831
    31
        23  ???                                 0x0000000106536040 0x0 + 4401094720
    32
    )
    33
    libc++abi.dylib: terminating with uncaught exception of type NSException
    34
    Java Result: 134
    This is the code of my server:
    Code:
    public class ServerController {
    002
     
    003
    @FXML
    004
    private TextArea textarea;
    005
     
    006
    private StringBuilder contenutoTextArea = new StringBuilder("");
    007
     
    008
    private ServerSocket s;
    009
     
    010
    public void initModel() throws IOException {
    011
        contenutoTextArea.append("Waiting for connections\n");
    012
        textarea.setText(contenutoTextArea.toString());
    013
        s = new ServerSocket(5000);
    014
        new Thread() {
    015
            @Override
    016
            public void run() {
    017
                while (true) {
    018
                    try {
    019
                        new ThreadedEchoHandler(s);
    020
                    } catch (IOException ex) {
    021
                        Thread.currentThread().interrupt();
    022
                        return;
    023
                    }
    024
                }
    025
            }
    026
        }.start();
    027
    }
    028
     
    029
    class ThreadedEchoHandler implements Runnable {
    030
     
    031
        private Socket incoming;
    032
     
    033
        private String nomeAccount = "";
    034
     
    035
        ThreadedEchoHandler(ServerSocket serv) throws IOException {
    036
            incoming = serv.accept();
    037
            new Thread(this).start();
    038
        }
    039
     
    040
        public void run() {
    041
            contenutoTextArea.append("Connected from: " + incoming.getLocalAddress() + "\n");
    042
            textarea.setText(contenutoTextArea.toString());
    043
            try {
    044
                //PHASE 1: The server receives the email
    045
                try {
    046
                    BufferedReader in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
    047
                    nomeAccount = in.readLine();
    048
                } catch (IOException ex) {
    049
                    System.out.println("Not works");
    050
                }
    051
     
    052
                //PHASE 2: I'm getting all the emails from the files
    053
                File dir = new File("src/server/" + nomeAccount);
    054
                String[] tmp = new String[100];
    055
                int i = 0;
    056
                for (File file : dir.listFiles()) {
    057
                    if (file.isFile() && !(file.getName().equals(".DS_Store"))) {
    058
                        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
    059
                            String line;
    060
                            while ((line = br.readLine()) != null) {
    061
                                tmp[i++] = line;
    062
                            }
    063
                            br.close();
    064
                        } catch (IOException ex) {
    065
                            System.out.println("Cannot read from file");
    066
                        }
    067
                    }
    068
                }
    069
     
    070
                //PHASE 3: The server sends the ArrayList to the client
    071
                PrintWriter out = new PrintWriter(incoming.getOutputStream(), true);
    072
                for (int j = 0; j < i; j++) {
    073
                    out.println(tmp[j]); // send the strings to the client
    074
                }
    075
                out.println("Fine");
    076
     
    077
            } catch (IOException ex) {
    078
                System.out.println("Cannot send the strings to the client");
    079
            }
    080
     
    081
            //PHASE 4: Here I loop and wait for the client choise
    082
            BufferedReader in;
    083
            String op;
    084
            boolean exit = false;
    085
            try {
    086
                in = new BufferedReader(new InputStreamReader(incoming.getInputStream()));
    087
                while (!exit && ((op = in.readLine()) != null)) {
    088
                    if (op.equals("Fine")) {
    089
                        exit = true;
    090
                        contenutoTextArea.append("Connection closed.\n");
    091
                        textarea.setText(contenutoTextArea.toString());
    092
                    } else {
    093
                        try {
    094
                            clientChoise(in, op);
    095
     
    096
                        } catch (IOException ex) {
    097
                            System.out.println("Wrong user input");
    098
                        }
    099
                    }
    100
                }
    101
            } catch (IOException ex) {
    102
                System.out.println("Unable to read messages");
    103
            } finally {
    104
                try {
    105
                    incoming.close();
    106
                    Thread.currentThread().interrupt();
    107
                    return;
    108
                } catch (IOException ex) {
    109
                    System.out.println("Cannot close the socket");
    110
                }
    111
            }
    112
        }
    In particular, the communication between the server and the clients is focused on the PHASE 4, where the server waits for the inputs from the client. I've added the printStackTrace and on the Server I didn't get any error, but on the client each time I pressed a button or I did any operation I got these errors:
    Code:
    java[1133:134799] unrecognized type is 429496729502
    2
    java[1133:134799] *** Assertion failure in -[NSEvent _initWithCGEvent:eventRef:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/AppKit/AppKit1671.10.106/AppKit.subproj/NSEvent.m:1969
    And if I try to close the client manually I also get this one:
    Code:
    java.net.SocketException: Socket closed
    02
        at java.net.SocketInputStream.socketRead0(Native Method)
    03
        at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
    04
        at java.net.SocketInputStream.read(SocketInputStream.java:171)
    05
        at java.net.SocketInputStream.read(SocketInputStream.java:141)
    06
        at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
    07
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
    08
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
    09
        at java.io.InputStreamReader.read(InputStreamReader.java:184)
    10
        at java.io.BufferedReader.fill(BufferedReader.java:161)
    11
        at java.io.BufferedReader.readLine(BufferedReader.java:324)
    12
        at java.io.BufferedReader.readLine(BufferedReader.java:389)
    13
        at mailbox.DataModel.askNumbOfEmail(DataModel.java:188)
    14
        at mailbox.ListController$2.run(ListController.java:65)
    This is the code of the DataModel of the client, where all the operations and communications with the server are done:
    Code:
    public class DataModel {
    002
     
    003
        private final String account = "alessandro@gmail.com";
    004
     
    005
        private ObservableList<Email> emailList = FXCollections.observableArrayList(email
    006
                -> new Observable[]{email.IDProperty(), email.MittenteProperty()});
    007
     
    008
        private final ObjectProperty<Email> currentEmail = new SimpleObjectProperty<>(null);
    009
     
    010
        public ObjectProperty<Email> currentEmailProperty() {
    011
            return currentEmail;
    012
        }
    013
     
    014
        public final Email getCurrentEmail() {
    015
            return currentEmailProperty().get();
    016
        }
    017
     
    018
        public final String getAccountName() {
    019
            return account;
    020
        }
    021
     
    022
        public final void setCurrentEmail(Email email) {
    023
            currentEmailProperty().set(email);
    024
        }
    025
     
    026
        public ObservableList<Email> getEmailList() {
    027
            return emailList;
    028
        }
    029
     
    030
        private Socket s;
    031
     
    032
        public void connect() throws IOException {
    033
            try {
    034
                s = new Socket("127.0.0.1", 5000);
    035
            } catch (IOException io) {}
    036
        }
    037
     
    038
        public Socket getSocket() {
    039
            return s;
    040
        }
    041
     
    042
        private int numberOfEmail = 0;
    043
     
    044
        public int getNumberOfEmail() {
    045
            return numberOfEmail;
    046
        }
    047
     
    048
        public void loadData() throws IOException, ClassNotFoundException, ParseException {
    049
     
    050
            try {
    051
                if (s == null || !isConnected()) {
    052
                    connect();
    053
                }
    054
                ArrayList<Email> email = new ArrayList<Email>();
    055
                DateFormat format = new SimpleDateFormat("dd/MM/yyyy", new Locale("it", "IT"));
    056
                Date data;
    057
     
    058
                /* PHASE 1: The client sends a string to the server */
    059
                try {
    060
                    PrintWriter out = new PrintWriter(s.getOutputStream(), true);
    061
                    out.println(account + "\n"); // send the account name to server
    062
     
    063
                    /* PHASE 2: The client receives the ArrayList with the emails */
    064
                    BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    065
                    String line;
    066
                    String message[] = new String[5];
    067
                    for (int j = 0; ((line = in.readLine()) != null) && (!line.equals("Fine"));)/> {
    068
                        message[j++] = line;
    069
                        if (j == 5) {
    070
                            data = format.parse(message[3]);
    071
                            email.add(new Email((Integer.parseInt(message[0])), message[1], account, message[2], message[4], data));
    072
                            numberOfEmail++;
    073
                            j = 0;
    074
                        }
    075
                    }
    076
     
    077
                    //Casting the arrayList
    078
                    emailList = FXCollections.observableArrayList(email);
    079
     
    080
                    //Sorting the emails
    081
                    Collections.sort(emailList, (Email o1, Email o2) -> {
    082
                        if (o1.getData() == null || o2.getData() == null) {
    083
                            return 0;
    084
                        }
    085
                        return o1.getData().compareTo(o2.getData());
    086
                    });
    087
                } finally {
    088
                    System.out.println("Data Loaded");
    089
                }
    090
            } catch (SocketException se) {
    091
                se.printStackTrace();
    092
                emailList.setAll(null, null);
    093
            }
    094
        }
    095
     
    096
        public void reLoadData() throws ParseException, IOException {
    097
            PrintWriter out = new PrintWriter(s.getOutputStream(), true);
    098
            out.println("Reload");
    099
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    100
            String line;
    101
            String message[] = new String[5];
    102
            ArrayList<Email> email = new ArrayList<Email>();
    103
            DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    104
            Date data;
    105
            for (int j = 0; ((line = in.readLine()) != null) && (!line.equals("Fine"));)/> {
    106
                message[j++] = line;
    107
                if (j == 5) {
    108
                    data = format.parse(message[3]);
    109
                    email.add(new Email((Integer.parseInt(message[0])), message[1], account, message[2], message[4], data));
    110
                    j = 0;
    111
                }
    112
            }
    113
     
    114
            //Casting the arrayList
    115
            emailList = FXCollections.observableArrayList(email);
    116
     
    117
            //Sorting the emails
    118
            Collections.sort(emailList, (Email o1, Email o2) -> {
    119
                if (o1.getData() == null || o2.getData() == null) {
    120
                    return 0;
    121
                }
    122
                return o1.getData().compareTo(o2.getData());
    123
            });
    124
        }
    125
     
    126
        public void deleteMail(Email da_elim) throws IOException {
    127
            Socket p = getSocket();
    128
            int id_del = da_elim.getID();
    129
            emailList.remove(da_elim);
    130
            PrintWriter out = new PrintWriter(p.getOutputStream(), true);
    131
            out.println("Elimina");
    132
            out.println(Integer.toString(id_del));
    133
        }
    134
     
    135
        public void closeConnection() throws IOException {
    136
            Socket p = getSocket();
    137
            PrintWriter out = new PrintWriter(p.getOutputStream(), true);
    138
            out.println("Fine");
    139
            p.close();
    140
        }
    141
     
    142
        public void writeMail(String[] mail) throws IOException {
    143
            Socket p = getSocket();
    144
            PrintWriter out = new PrintWriter(p.getOutputStream(), true);
    145
            out.println("Scrivi");
    146
            for (int j = 0; j < mail.length; j++) {
    147
                out.println(mail[j]); // sends the mail to the server
    148
            }
    149
            out.println("Stop");
    150
     
    151
        }
    152
     
    153
        public int askNumbOfEmail() throws IOException {
    154
            Socket p = getSocket();
    155
            PrintWriter out = new PrintWriter(p.getOutputStream(), true);
    156
            out.println("Numb");
    157
            BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
    158
            return Integer.parseInt(in.readLine());
    159
     
    160
        }
    161
     
    162
        public boolean isConnected() {
    163
            return getSocket()==null || getSocket().isConnected();
    164
        }
    165
    }
    And this is the controller of my list view (of the client). Here I have a thread which checks continuously if there are new emails:
    Code:
    public class ListController {
    02
     
    03
        @FXML
    04
        private ListView<Email> listView;
    05
        private DataModel model;
    06
        public int emailForClient;
    07
     
    08
        public void initModel(DataModel model) throws IOException, ClassNotFoundException, ParseException {
    09
            if (this.model != null) {
    10
                throw new IllegalStateException("Model can only be initialized once");
    11
            }
    12
            this.model = model;
    13
     
    14
            model.loadData();
    15
     
    16
            listView.setItems(model.getEmailList());
    17
     
    18
            listView.getSelectionModel().selectedItemProperty().addListener((obs, oldSelection, newSelection)
    19
                    -> model.setCurrentEmail(newSelection));
    20
            model.currentEmailProperty().addListener((obs, oldEmail, newEmail) -> {
    21
                if (newEmail == null) {
    22
                    listView.getSelectionModel().clearSelection();
    23
                } else {
    24
                    listView.getSelectionModel().select(newEmail);
    25
                }
    26
            });
    27
     
    28
            listView.setCellFactory(lv -> new ListCell<Email>() {
    29
                @Override
    30
                public void updateItem(Email mail, boolean empty) {
    31
                    super.updateItem(mail, empty);
    32
                    if (empty) {
    33
                        setText(null);
    34
                    } else if (mail != null) {
    35
                        setText(mail.getMittente());
    36
                    }
    37
                }
    38
            });
    39
     
    40
            emailForClient = model.getNumberOfEmail();
    41
            new Thread() {
    42
                @Override
    43
                public void run() {
    44
                    while (true) {
    45
                        try {
    46
                            int currentOnServer = model.askNumbOfEmail();
    47
                            if (emailForClient != currentOnServer) {
    48
                                model.reLoadData();
    49
                                final int currentEmail = emailForClient;
    50
                                Platform.runLater(() -> {
    51
                                    listView.setItems(model.getEmailList());
    52
                                    if (currentOnServer > currentEmail) {
    53
                                        new Alert(Alert.AlertType.INFORMATION, "You just received an email!").showAndWait();
    54
                                    }
    55
                                });
    56
                                emailForClient = currentOnServer;
    57
                            }
    58
                        } catch (IOException ex) {
    59
                            ex.printStackTrace();
    60
                            Thread.currentThread().interrupt();
    61
                            return;
    62
                        } catch (ParseException ex) {
    63
                            ex.printStackTrace();
    64
                            System.out.println("ParseException ERROR!");
    65
                        }
    66
                    }
    67
                }
    68
            }.start();
    69
        }
    70
    }
    I've also noticed that if I leave the server runs, the fans of my computer start. What could cause that?

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,853

    Re: Handle multiple threads with one server

    fans of my computer start
    Do you have a task manager that would show if the java program is using too many resources?
    Norm

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)