K.S.R.

COLLEGE OF ENGINEERING TIRUCHENGODE-637 215

ANNA UNIVERSITY: COIMBATORE (REGULATION: 2008)

(FOR III YEAR B.E CSE, FIFTH SEMESTER)

STAFF INCHARGE

HOD

1

COMPUTER NETWORKS LAB LIST OF PROGRAMS

S.no Name of the Program 1 Socket programming 2 3 4 5 6 Data grams TCP SMTP FTP Implementation of any two congestion control algorithms

Page no

2

Algorithm 1. No 1 Program for Echo Client and Echo Server Aim To write a program in c/ java to implement echo client and echo server. Program Client //echoclient.true).io. Get any string from the client side console 5.getOutputStream(). Create a socket connection from client to the server with corresponding IP address and port number 4.9999). String line.*. BufferedReader con = new BufferedReader (new InputStreamReader(System.println(line). Pass this string to the server which echoes back to the client 6. 7. public class echoclient { public static void main(String args[]) { try { Socket s=new Socket("localhost".readLine().*. import java. do { line=con. BufferedReader r = new BufferedReader(new InputStreamReader(s.SOCKET PROGRAMMING Ex.in)). if(line!=null) { w. Start 2. PrintWriter w=new PrintWriter(s.getInputStream())). } 3 . Display the output string that is echoed back to the client.java import java. Create necessary input and output streams 3.net.rline. Read the same string from the client side socket’s input stream.

System. } while (! line.getInputStream())).println("Welcome to java echoserver").accept().println("Error:"+err).equals("bye")). 4 .getOutputStream().exit(0). import java.equals("bye")).*.out.equals("bye")) System.println("Echo from server"+rline).out. System. } catch(Exception err) { System. if(line. client. if(line!=null) w.io.net. System.trim().println("error"+err).readLine (). String line. } } } Server // echoServer. } } catch(Exception err) { System. do { line =r. PrintWriter w=new PrintWriter(client.rline=r.out. public class echoServer { public static void main (String args[]) { try { ServerSocket ser = new ServerSocket(9999). }while(!line.trim().java import java.true). while(true) { Socket client = ser.*.println (line).close(). BufferedReader r=new BufferedReader(new InputStreamReader(client.out.out.readLine().println("SERVER:"+line).

} } } Output 5 .

naming.length != 1) { System.directory.UnknownHostException. } try { InetAddress inetAddress. public class DNSLookup { public static void main(String args[]) { // explain what program does and how to use it if (args.split("[.naming. import java.NamingException.isDigit(args[0]. Start 2.net.println("USAGE: java DNSLookup domainName| domainAddress").err. import javax. import javax. No 2 Aim To Write a program to implement the Domain Name System using UDP Algorithm 1.]").InetAddress. 6.NamingEnumeration. Display the ip address corresponding to the hostname.charAt(0))) { // convert address from string representation to byte array byte[] b = new byte[4].err. String[] bytes = args[0]. // if first character is a digit then assume is an address if (Character. import java. System. import javax. Program to Implement DNS using UDP 6 .Attributes.directory.println("Print out DNS Record for an Internet Address").net.RESULT Thus a program is written to implement the echo client and echo server Ex. System. Declare the necessary variables for the predefined structures hostent and utsname 3.naming. Get the hostname from the user through console 4. get the ip address of the hostname using the function gethostbyname() 5. Stop Program // Print out DNS Record for an Internet Address import javax.InitialDirContext.naming.exit(-1).

length.next()).for (int i = 0. } catch (UnknownHostException exception) { System.getHostName() + "/" + inetAddress.out. } attributeEnumeration.getByName(args[0]). } } } Output 7 .println("" + attributeEnumeration. } // get Internet Address of this host address inetAddress = InetAddress.err.DNS INFORMATION --"). i < bytes. System. } // show the Internet Address as name/address System.close().getAll(). // get the DNS records for inetAddress Attributes attributes = iDirC. } else { // get Internet Address of this host name inetAddress = InetAddress.getAttributes("dns:/" + inetAddress. while (attributeEnumeration. } catch (NamingException exception) { System. i++) { b[i] = new Integer(bytes[i]).byteValue(). // get the default initial Directory Context InitialDirContext iDirC = new InitialDirContext().println(inetAddress.out. // get an enumeration of the attributes and print them out NamingEnumeration attributeEnumeration = attributes.println("ERROR: No DNS record for '" + args[0] + "'").println("ERROR: No Internet Address for '" + args[0] + "'").hasMore()) { System.err.getHostAddress()).out.println("-.getByAddress(b).getHostName()).

} 8 . Create a socket connection from user1 to the user2 with corresponding IP address and port number 4. sentence=in. Socket cs=new Socket("10.out. newsentence=inp. DataOutputStream out=new DataOutputStream(cs.*. No 3 TCP programs a. Get any string from the user1 side console 5.in).readLine().println("User2:"+newsentence). if(sentence.6789).*.compareTo("exit")!=0) { out. Pass this string to the user1 which prints in the console of user1 8.Result Thus a program is written to implement the Domain Name System. DataInputStream inp=new DataInputStream(cs. Program to Implement Chatting Application Aim To Write a C++ program to prepare Students Report using Simple Class with primitive and array data types Algorithm 1.1. Stop Program User1 import java.getInputStream()).1.getOutputStream()). do { System.net.writeBytes(sentence+'\n'). DataInputStream in=new DataInputStream(System.print("User1:").204". Pass this string to the user2 which prints in the console of user2 6. class user1 { public static void main(String arg[]) throws Exception { String sentence. Ex. import java. Read some other string from the user2 side socket’s input stream. Create necessary input and output streams 3.out. Repeat the same until “exit” command 9. newsentence. Start 2. System.io. 7.readLine().

accept(). ServerSocket ss=new ServerSocket(6789).*.getInputStream()). DataInputStream in=new DataInputStream(System.}while(sentence. if(sentence. System.in). do { sentence=inp. out. Socket s=ss.close(). class user2 { public static void main(String arg[]) throws Exception { String sentence.readLine().compareTo("exit")!=0). } } User 2 import java.out. newsentence. newsentence=in.compareTo("exit")!=0) { System.io. cs. } } Output 9 .net.writeBytes(newsentence+'\n').out.readLine(). import java.println("User1:"+sentence). } }while(sentence.*.print("User2:"). DataInputStream inp=new DataInputStream(s. DataOutputStream out=new DataOutputStream(s.getOutputStream()).compareTo("exit")!=0).

readLine(). Accept the connection requisition from the client side systems inside the thread.newsentence1. Run any number of clients in parallel communicating the same server. newsentence=in.out. Program to Implement Concurrent Server Aim To Write a program to implement the concept of concurrent server.getOutputStream()). 6.readLine(). Create the necessary sockets in the client and server side systems 3.getInputStream()). Stop Program Client 1/ Client 2 / any clients import java. sentence=inp.RESULT Thus a program is written to implement the chatting application using TCP sockets.readLine(). 7.out.close(). System.writeBytes(newsentence+'\n'). class Clientcon { public static void main(String arg[]) throws Exception { String sentence. The server will respond all the clients at the same time 8. DataOutputStream out=new DataOutputStream(cs.*. Start 2.in).*. newsentence1=inp.net. 10 . Algorithm 1. import java. cs. newsentence. DataInputStream inp=new DataInputStream(cs. 5.6789).io. b.*.io. Socket cs=new Socket("pgcse220".println("From Server:"+newsentence1).println(sentence). out. } } Server import java. Initialize necessary input and output streams for the accepted connection inside the thread. Initialize a thread in the server side system using runnable interface 4. DataInputStream in=new DataInputStream(System. System.

accept(). DataOutputStream out=new DataOutputStream(s. conserver(Socket s1. s=s1. class Server { public static void main(String arg[]) throws Exception { int count=0. count). ServerSocket ss=new ServerSocket(6789).currentThread().start().int count) { t=new Thread(this. count++. String sentence="Enter the String:". out.writeBytes(sentence+'\n'). newsentence=inp. while(true) { try { Socket s=ss. Socket s. int c. } catch(Exception e) {} } } } class conserver implements Runnable { Thread t.net. "Demo"). t. } public void run() { try { DataInputStream inp=new DataInputStream(s.*. 11 .getInputStream()). conserver c =new conserver(s.getOutputStream()).readLine(). String newsentence.import java. Thread t= Thread. c=count.

println("From Client"+c+":"+newsentence). } catch(Exception e) {} } } Output Result Thus a program is written to implement the concurrent server 12 . out.//Thread. System.out.writeBytes(newsentence+'\n').sleep(10000).

PrintStream ps = null . import java. No 4 Program for simple mail transfer protocol Aim To Write a program to implement the concept of SMTP server. } public static void ServiceClient (Socket s) throws Exception { DataInputStream dis = null .*. 13 .io.*. ServiceClient(s).accept().net.Ex. class smtpServer { public static void main (String args[]) throws Exception { ServerSocket ss = new ServerSocket(8080). Socket s = ss. Algorithm PROGRAM //SMTP Server import java.

write("\nMessage: " + msg + "\n" ) . String to = dis.getInputStream()).io. PrintStream ps = new PrintStream(s. class smtpClient { public static void main (String args[]) throws Exception { Socket s=new Socket("localhost".dis = new DataInputStream (s. ps.out. DataInputStream in = new DataInputStream(System.in).println ("Ready signal Received from client") .8080). f.*.readLine().println(strFrom).out.getOutputStream()).readLine().write("Subject:" + sub + "\n"). String msg = dis.write("To:" + to + "\n"). f. ps. String strFrom=in.println("Enter the From address:"). import java.println("Ready"). } } // SMTP Client import java.readLine(). DataInputStream dis = new DataInputStream(s. f. FileWriter f = new FileWriter ("TestMail.readLine()). if(tel. String from = dis.readLine().getOutputStream()).net. String sub = dis.println("Enter the Subject:").println (dis.println("Enter the Message:").getInputStream()) .println("Enter the To address:"). ps.*.eml") .close().write("From:" +from+ "\n") . System. String tel=dis. ps. f. ps. ps = new PrintStream (s.readLine(). ps.equals("Ready")) System. f.readLine() . 14 .

readLine(). System.out.co. String strTo=in.System.println(strTo). if(msg.out. ps. String strSub=in.com Enter the To address: crathish@yahoo.readLine()).println(msg).readLine()).println(strSub). } } } } OUTPUT Z:\Programs\SMTP>javac smtpServer.readLine().println (dis. ps.java Z:\Programs\SMTP>java smtpServer Ready signal Received from client Z:\Programs\SMTP> Z:\Programs\SMTP>java smtpClient Enter the From address: crathish@gmail.println (dis. while(true) { String msg=in.out.java Z:\Programs\SMTP>javac smtpClient.println (dis.out. break.equals("Quit")) { System.readLine(). System.in Enter the Subject: 15 . ps.readLine()).println ("Message is delivered to server and client quits").

this mail is generated as part of the SMTP program testing! Quit Message is delivered to server and client quits Z:\Programs\SMTP> // Generated Mail Result 16 .Test Mail Enter the Message: Hello.

out.out. class user { public static void main(String arg[]) throws Exception { long end. DOWNLOAD"). Get the option from the user to UPLOAD/ DOWNLOAD and the file name to be transferred 6. Program to Implement File Transfer 17 . Create necessary file streams 5. UPLOAD").print("ENTER THE OPTION:").out. if(option. import java.out. No 5 Aim To write a program in c/ java to implement file transfer between client and file server. System.println("Enter the file name:"). read each byte from the file in the client side and write it into the output stream of the socket 7.1.*. DataInputStream inp=new DataInputStream(cs. option=in. String option.f1name. Create a socket connection from client to the server with corresponding IP address and port number 4.getInputStream()). Start 2. In the server side.println("A.net.writeBytes(option+"\n").io.out.204". DataOutputStream out=new DataOutputStream(cs. If UPLOAD.getOutputStream()).compareTo("A")==0) { System.println("B.readLine(). System. read the bytes from the input stream and write it into the file Program Client import java. String fname. DataInputStream in=new DataInputStream(System. System.in). int size=0.6789).*. Create necessary input and output streams 3.1. Socket cs=new Socket("10.Thus a program is written to implement the SMTP client and SMTP server Ex. Algorithm 1.

close().*. fname=in. 18 .println("UPLOADED!!!!!").close().println("Enter Valid Option"). option.write(b). out.out. System. DataInputStream in=new DataInputStream(System.write(b).writeBytes(fname+'\n').read(b).readLine(). f1name=in. System. fo.println("Enter the file name :"). inp. fo. size=fi.println("Enter the file name to be saved :"). import java. } cs. end = System. fi.*. System.writeBytes(f1name+'\n').read(b).out.net.readLine().in). f1name.compareTo("B")==0) { System. out. class FileServer { public static void main(String arg[]) throws Exception { String fname. out. } else { System.readLine(). FileOutputStream fo=new FileOutputStream(f1name). fi.fname=in. } else if(option. byte b[]=new byte[400].out. byte b[]=new byte[size]. FileInputStream fi=new FileInputStream(fname). File f=new File(f1name).out. } } File Server import java.close().out. File f=new File(fname).readLine().println("DOWNLOADED!!!!!").currentTimeMillis(). System.available().println("Enter the file name to be saved in Server:").out. f1name=in.io.

while(true) { Socket s=ss.accept().out. fo. File Not Present".readLine().println("File "+f1name+" UPLOADED!!!!!"). DataOutputStream out=new DataOutputStream(s.println("Accessed file :" + fname). try { File f=new File(fname).read(b). InputStream fi=new FileInputStream(f). out. System.write(b). } catch(FileNotFoundException e) { String excep = "Sorry.close().println(excep). byte b[]=new byte[size].readLine(). System. try { File f=new File(f1name).close().readLine(). } catch(FileNotFoundException e) { String excep = "Sorry.out.out. byte b[]=new byte[400].. fo. inp.. File Not Present". } } else if(option.available().getOutputStream()). int size=fi. 19 .getInputStream()).println("Successfully send").println(excep). FileOutputStream fo=new FileOutputStream(f). if(option.out. break.compareTo("B")==0) { fname=inp. System. DataInputStream inp=new DataInputStream(s. option=inp.compareTo("A")==0) { f1name=inp. fi.write(b).ServerSocket ss=new ServerSocket(6789)...read(b). System.out. fi. System.

break.println("Enter Valid Option"). } } else { System.out. } } Output } } RESULT Thus a program is written to perform functions with default arguments 20 .

Master your semester with Scribd & The New York Times

Special offer for students: Only $4.99/month.

Master your semester with Scribd & The New York Times

Cancel anytime.