Download as pdf
Download as pdf
You are on page 1of 13
SUE ssc @ ® Anna Sultan~ Dashboard > My courses > FCIM.PCDI61 > Examen > Atestarea 2 Started on State Completed on Time taken Grade Wednesday, 30 November 2022, 7:33 PM Finished Wednesday, 30 November 2022, 8:43 PM ‘hour 10 mins 10 out of 10 (100%) | ELSE Question 1 Complete Mark 1 out of 1 Care este codul numeric prin care Serverul indica c& fisierul nu a fost gasit? Kaknm scnoabim Konom Cepsep ykasbisaer, uTo cbaiin He Gbin HalineH? class Server! public static void main(String{] args} try ServerSocket ss = new ServerSocket(Integer.parseint(args[0])); while (true) new HttpConnect(ss.accept()); }eatch(ArraylndexOutOfBoundsException ae){ ‘System.err.printin("Usage: Server port"); System.exit(0); Joatch(lOException e){ ‘system.out printin(e); dh class HttpConnect extends Thread{ private Socket sock; HttpConnect(Socket s) { sock setPriority(NORM_PRIORITY - 1); start 0; } public void run0{ tryt Printwriter pw = new PrintWriter(new OutputStreamWriter( sock.getOutputStream()}, true); BufferedReader br = new BufferedReader(new InputStreamReader( sock.getInputStream() ) ) ; String req = brreadLine(); ‘System.out.printin("Request: " + req); StringTokenizer st = new StringTokenizer(req); if ((st.countTokens() >= 2) && st.nextToken().equals("POST"){ if (req = st-nextToken().endsWith("/") Il req.equals("")) req += “index.html; try File f = new File(req); BufferedReader bfr = new BufferedReader(new FileReader(f); char{] data = new char[(int)f.lengthO]; bfr.read(data); pwoprintln("HTTP/11 200 OK\n"); pwawrite(data); pwelushQ; Joatch(FileNotFoundexception fe){ pwoprintln("HTTP/11 404 Not FoundXn"); Joatch(lOException ioe){ ‘system.out printin(ioe); } Jelse pwprintin("HTTP/L| 400 Bad RequestW); sock.close(); Jeatch(lOException e)f ‘System .out println(); vy} Answer: 404 Question 2 Complete Mark 1 out of 1 In care rind a codului de program se ceaza firul de executie la cererea clientului? B kakoii cTpoke nporpamMHoro Kola cosqaeTcA NoTOK ANA KAMeHTcKOrO sanpoca? 1) public static final int PORT = 8100; 2) private static ServerSocket serverSocket = null; 3) private Socket clientSocket = null; 4) public void run { 5) String cerere, raspuns; 6) yf 7) — BufferedReader Ms 8) _PrintWriter out = new PrintWriter(clientSocket.getOutputStream() ); \ew BufferedReader(new InputStreamReader(clientSocket.getinputStream() 9) cerere = in.readLine(; 10) raspuns = “hello " + cerere; 11) out.printin(raspuns); 12) outflush(); } 13) catch (IOException e) { 14) System.err.printin("Eroare de citire/scriere \n" + e);} 15) finally { 16) try{ 17) clientSocket.close();} 18) catch (IOException e) {System.err.printin("Socketul nu poate fi inchis \n" + e):}}} 19) public SimpleServer() throws IOException { 20) serverSocket = new ServerSocket(PORT); 2) try 22) clientSocket = serverSocket.accept(); 23) new Thread{(this).start();} 24) finally {serverSocket.close();}} 25) public static void main(String[] args) throws IOException { 26) SimpleServer server = new SimpleServer(); Answer: 23 Question 3 Complete Mark 1 out of in care rind a codului de program pentru gruparea mai multor clienti, se asteapta un pachet venit pe adresa grupului? B kako” cTpoke NporpaMMHoro Kona AANA PPYNMMPOBKM HECKONLKMK KIMEHTOB, OXUQAETCA NakeT NPHELBWAA no agpecy rpynnei? 1) public class MulticastClient { 2) public static void main(String{] args) throws IOException { 3) InetAddress group = InetAddress.get8yName("230.0.0." 4) int port=4444; 5) MulticastSocket socket = null; 6) byte buff]; 7 tryf 8) socket = new MulticastSocket(port); 9) socketjoinGroup(group); 10) buf = new byte[256]; 11) DatagramPacket packet = new DatagramPacket(buf, buf.length); 12) socket.receive(packet); 13) System.out printin(new String(packet.getData()));} 14) finally { 15) socket.leaveGroup(group); 16) socket.close(; M Answer: 12 Question 4 Complete Mark 1 out of 1 Acest protocol asigura stabilirea unei conexiuni permanente intre cele doua calculatoare pe parcursul comunicatiei ‘STor npoToKon rapaHTupyer, NTO YcTaHOBNeHo NOCTORHHO COSAUNEHKe MEKAy ABYMA KOMNBIOTepAMM BO epema o6uienne. Answer: TCP Question 5 Complete Mark out of 1 De la care rind a codului de program pina la care rind se afiseaza pachetul si distruge socketul? Raspunsul si fie de forma 12-18 (OF kakoit cTpoKn nporpamMHoro Kofa M0 KaKOi CTpOKM acpULIMpyeTCR naKeT M COKET YHAYTOMEH? Oreet AOMKeH GbITe B cbopMe 12-18 1) public class DatagramClient { 2) public static void main(String[] args) throws IOException { 3) InetAddress address = InetAddress.get8yName("127.0.0."); 4) int port=8200; 5) DatagramSocket socket = null; 6) DatagramPacket packet = null; 7) byte buff]; 8) tryt 9) socket = new DatagramSocket(); 10) bu "salut" getBytes(); 11) packet = new DatagramPacket(buf, buf.length, address, port); 12) socket.send(packet); 13) packet = new DatagramPacket(buf, buf.length); 14) socket.receive(packet); 15) System.out printin(new String(packet.getData()));} 16) finally {socket.close(); M Answer: | 15-16 Question 6 Complete Mark 1 out of 1 Care va fi rezultatul realizarii codul le program? public class ParseURL { public static void main(String[] args) throws Exception { URL aURL = new URL(“http://oslo.com’ System.out.printin( aURL.getHost(); Answer: oslo.com Question 7 Complete Mark 2 out of 2 in care rinduri a codului de program se creeaza cererea si raspunsul? de forma 12, 18 B kaxwx cTpokax nporpaMMHoro Koa coaqaeTcA sanpoc u oTBeT? GviTe 8 chopme 12, 18 1) public class DatagramServer { 2) public static final int PORT = 8200; 3) private DatagramSocket socket = null; 4) DatagramPacket cerere, raspuns = null; 5) public DatagramServer() throws IOException { 6) Socket = new DatagramSocket(PORT); 7) try 8) while (true) { 9) _ byte[] buf = new byte[256]; 10) cerere = new DatagramPacket(buf, buf.length); 11) socket.receive(cerere); 12) InetAddress adresa = cerere.getAddress(); 13) int port = cerere.getPort(); 14) buf = “Hello * + new String(cerere.getData())).getBytes(); 15) raspuns = new DatagramPacket(buf, buflength, adresa, port); 16) socket.send(raspuns);}} 17) finally {socket.close();}} 18) public static void main(String{] args) throws IOException { 19) new DatagramServer(); y Answer: 10, 15 Raspunsul sa fie Orper pomKer Question 8 Complete Mark 1 out of 1 Cele mai importante metode ale clasei Socket sunt prezentate mai jos: HavGonee BaxkHbie MeTone! Knacca Socket npeqcTasneHbl HiDKe: Genereaza output stream la socket. Output stream este conectat la input stream a remote socket. public OutputStream getOutputStream() ¥ CCoanacr auixonWoi noroK coxeTa, BbxonNoit NoTox nNonKMiONAeTCR k BxoaHomy noToKy yaneHHoro coxera, Inchide socket-ul, ceea ce face ca acest Socket object sa nu mai fie capabil sa se conecteze din nou la alt server. public void close() v Saxpsisaer coker, u3-2a vero aToT OBbeKT Socket Gonsive He MoxeT nopKniovaTeca K Apyromy cepBepy. Genereaza input stream la socket. Input stream este conectat la output stream a remote socket. public InputStream getinputStream() sv Cosnaert axon}oli notox coxera, BxopHoit noToK caRsaH © BLIKO}HEIM NOTOKOM yaneHHoro coKeTa, Question Complete Mark 1 out of 1 De la care rind a codului de program pina la care rind se trimite o cerere la server? A fie de forma 12-18, (OT kako cTpoKH nporpawmHoro Kona Ao KaKOit CTPOKM sanpoc oTnpaanseTcs Ha cepBep? OrBet AomKeH GtiTb 8 chopMe 12-18. 1) public class SimpleClient { 2) public static void main(String[] args) throws IOException { "127.0. 3) String serverAddres: a; 4) int PORT = 8100; 5) Socket clientSocket = null; 6) _PrintWriter out = null; 7) BufferedReader in = null; 8) String cerere, raspuns; 9) try { clientSocket = new Socket(serverAddress, PORT); 10) out = new PrintWriter(clientSocket.getOutputStream(), true); 11) in = new BufferedReader(new InputStreamReader(clientSocket.getinputStream())); 12) cerere = "duke"; 13) out.println(cerere); 14) raspuns = in.readLine(); 15) System.out println(raspuns);} Raspunsul 16) catch (UnknownHostException e) {System.err.printin("Serverul nu poate figasit \n" + e); 17) system.exit(1);} 18) finally { 19) if (out null) out.close(); 20) if (in != null) in.closeQ; 21) if (clientSocket!= null) clientSocket.close(); Answer: | 12-13 4 Atestarea 1 Jump to.. vo Test1h

You might also like