






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Two different codes for a DCCN lab assessment. The first code is a UDP based server code that calculates the age of a client based on their date of birth. The second code is a client-server model for file transfer, which includes HTTPS for uploading files to an FTP server, encryption and decryption of file data with security algorithms, and code for generating a key for encryption and decryption using TCP/IP client server sockets. sample code and output for both scenarios.
Typology: Assignments
1 / 12
This page cannot be seen from the preview
Don't miss anything!
Ans. CODE : import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; public class AgeCalculator { private static Age calculateAge(Date birthDate)
//if month difference is in negative then reduce years by one //and calculate the number of months. if (months < 0) { years--; months = 12 - birthMonth + currMonth; if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) months--; } else if (months == 0 && now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) { years--; months = 11; } //Calculate the days if (now.get(Calendar.DATE) > birthDay.get(Calendar.DATE)) days = now.get(Calendar.DATE) - birthDay.get(Calendar.DATE); else if (now.get(Calendar.DATE) < birthDay.get(Calendar.DATE)) {
int today = now.get(Calendar.DAY_OF_MONTH); now.add(Calendar.MONTH, - 1); days = now.getActualMaximum(Calendar.DAY_OF_MONTH)
Server-side program: import java.io.; import java.net.; class ManagerServer { public static void main(String args[]) throws Exception { ServerSocket ss = new ServerSocket(1080); Socket s = ss.accept(); InetAddress IA = InetAddress.getByName("localhost");
File file = new File("Clienttext.java"); FileInputStream fis = new FileInputStream(file); BufferedInputStream bis = new BufferedInputStream(fis); OutputStream o = s.getOutputStream(); byte[] contents; long fileLength = file.length(); long current = 0; long start = System.nanoTime(); while(current!=fileLength) { int size = 10000; if(fileLength-current>=size) { current+=size; } else { size =(int)(fileLength-current); current = fileLength; } contents = new byte[size]; bis.read(contents,0,size); o.write(contents);
while((bytesread = is.read(contents))!=-1) { bos.write(contents,0,bytesread); } bos.flush(); s.close(); System.out.println("File saved successfully"); } } Output: