Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Network; import Email.emailSender; import java.io.IOException; import java.io.InputStreamReader; import java.io.Writer; import java.net.Socket; import java.nio.channels.Channels; import java.nio.charset.StandardCharsets; import java.util.Observable; import java.util.Observer; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import org.json.simple.parser.ParseException; /** * * @author Bryden */ public class CEmailHandler implements Observer { @Override public void update(Observable o, Object arg) { Socket objSocket = (Socket) arg; int intSeq = -1; try { JSONParser jsonParser = new JSONParser(); JSONObject objJSON = (JSONObject) jsonParser.parse(new InputStreamReader(objSocket.getInputStream())); int intTempSeq = Integer.parseInt(objJSON.get("id").toString()); emailSender.getEmailsToBeSent((JSONArray) objJSON.get("messagesToBeSent")); emailSender.dispatchEmail(); intSeq = intTempSeq; } catch (IOException | ParseException | NumberFormatException ex) { System.out.println(ex); } finally { try (Writer objWriter = Channels.newWriter(Channels.newChannel(objSocket.getOutputStream()), StandardCharsets.US_ASCII.name())) { objWriter.write(intSeq + ""); objWriter.flush(); } catch (IOException ex) { System.out.println(ex); } } } }