0% found this document useful (0 votes)
103 views14 pages

Tcp/Ip Lab: List of Experiments

The document outlines a list of experiments for a TCP/IP lab covering topics like getting the IP address from the hostname and vice versa, writing client and server programs to capitalize strings, implement chat clients and servers, programs to send and receive time/date, programs to handle multiple clients, programs to send and receive files between clients and servers, and writing a basic web server program. The programs provided implement these experiments using Java socket programming and networking APIs.

Uploaded by

Raveesh Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views14 pages

Tcp/Ip Lab: List of Experiments

The document outlines a list of experiments for a TCP/IP lab covering topics like getting the IP address from the hostname and vice versa, writing client and server programs to capitalize strings, implement chat clients and servers, programs to send and receive time/date, programs to handle multiple clients, programs to send and receive files between clients and servers, and writing a basic web server program. The programs provided implement these experiments using Java socket programming and networking APIs.

Uploaded by

Raveesh Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 14

TCP/IP Lab

List of Experiments
1. Program to get the IP address by giving hostname of any node in network & vice versa. 2 (A). Write a Server program that get string from the client side and capitalized it and send it back to the client. (B). Write a Client program that send String to the Server and get capitalized String. 3 (A). Write a Server program that work as Chat Server. (B). Write a Client program that work as Chat Client. 4 (A). Write a Server program that send Time and Day to Client. (B). Write a Client program that receives Time and Day from Server. 5 (A). Write a Server program, which handle multiple clients. (B). Write a Client program, which takes input from client and sends it to server. 6(A) Write a Server Program that receives file from client and displays the data. (B) Write a Client Program that sends a file to the server 7(A). Write a program of web server.

Extra assignments (beyond Syllabus)


Internal Component: each assignment consists 10 marks. 1.(A). Write a Server program that get string from the client side and capitalized it and send it back to the client(UDP). (B). Write a Client program that send String to the Server and get capitalized String(UDP). 2.(A). Write a Server program that work as Chat Server(UDP). (B). Write a Client program that work as Chat Client(UDP).

Program No - 1
Object: -Program to get the IP address by giving hostname of any node in network
& vice versa.

Program Code: import java.net.*; class InetAddressTest { public static void main(String args[]) throws UnknownHostException { InetAddress Address = InetAddress.getLocalHost(); System.out.println(Address); Address = InetAddress.getByName("rediff.com"); System.out.println(Address); Address = InetAddress.getByName("www.google.co.in"); System.out.println(Address); InetAddress inet = InetAddress.getByName("169.254.242.72"); System.out.println ("Host: " + inet.getHostName()); Address = InetAddress.getByName("www.yahoo.co.in"); System.out.println(Address); InetAddress SW[] = InetAddress.getAllByName("www.veeras.com"); for(int i=0; i<SW.length; i++) System.out.println(SW[i]); } }

Program No 2(A)
Object: - Write a Server program that get string from the client and capitalized it
and send it to the client.

Program Code: import java.io.*; import java.net.*; class TCPServer { public static void main (String argv[]) throws Exception { String clientSentence; String capitalizedSentence; ServerSocket welcomeSocket = new ServerSocket(5555); while(true) { Socket connectionSocket = welcomeSocket.accept(); BufferedReader inFromClient = new BufferedReader (newInputStreamReader(connectionSocket.getInputStream())); DataOutputStream outToClient=newDataOutputStream (connectionSocket.getOutputStream()); clientSentence = inFromClient.readLine(); capitalizedSentence = clientSentence.toUpperCase() + '\n'; outToClient.writeBytes(capitalizedSentence); } } }

Program No 2(B)
Object: - Write a Client program that send String to the Server and get
capitalized String.

Program Code: import java.io.*; import java.net.*; class TCPClient { public static void main (String argv[]) throws Exception { String sentence; String modifiedSentence; BufferedReader inFromUser = new BufferedReader (new InputStreamReader(System.in)); Socket clientSocket = new Socket("localhost",5555); DataOutputStream outToServer = new DataOutputStream (clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); sentence = inFromUser.readLine(); outToServer.writeBytes(sentence + '\n'); modifiedSentence = inFromServer.readLine(); System.out.println("FROM SERVER: "+modifiedSentence); clientSocket.close(); } }

Program No 3(A)
Object: - Write a Server program that work as Chat Server.

Program Code: import java.net.*; import java.io.*; import java.util.*; public class ChatServer { public static void main(String args[]) { try { ServerSocket SS = new ServerSocket(8000); Socket S = SS.accept(); DataInputStream dis = new DataInputStream(S.getInputStream()); DataOutputStream dos = new DataOutputStream(S.getOutputStream()); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader (isr); while (true) { System.out.println("Enter Ur Message"); dos.writeUTF(br.readLine()); System.out.println("Client:" + dis.readUTF()); } } catch(Exception e) { System.out.println(e); } } }

Program No 3(B)
Object: - Write a Client program that work as Chat Client.

Program Code: import java.net.*; import java.io.*; import java.util.*; public class ChatClient { public static void main(String args[]) { try { Socket S = new Socket("localhost", 8000); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader (isr); DataInputStream dis = new DataInputStream(S.getInputStream()); DataOutputStream dos = new DataOutputStream(S.getOutputStream()); while (true) { System.out.println("Server:" + dis.readUTF()); System.out.println("Enter Ur Message"); dos.writeUTF(br.readLine()); } } catch(Exception e) { System.out.println(e); } } }

Program No 4(A)
Object: - Write a Server program that send Time and Day to Client.

Program Code: import java.net.*; import java.io.*; import java.util.*; public class ChatServer { public static void main(String args[]) { try { ServerSocket SS = new ServerSocket(8000); Socket S = SS.accept(); DataInputStream dis = new DataInputStream(S.getInputStream()); DataOutputStream dos = new DataOutputStream(S.getOutputStream()); InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader (isr); while (true) { System.out.println("Enter Ur Message"); dos.writeUTF(br.readLine()); System.out.println("Client:" + dis.readUTF()); } } catch(Exception e) { System.out.println(e); } } }

Program No 4(B)
Object: - Write a Client program that receive Time and Day from Server.

Program Code: import java.net.*; import java.io.*; import java.util.*; public class DaytimeClient { public static void main(String[] args) { String hostname = "localhost"; if (args.length > 0) { hostname = "args[0]"; } else { System.out.println("Hostname is Desired"); } try { Socket S = new Socket(hostname, 13); InputStream is = S.getInputStream(); StringBuffer time = new StringBuffer(); int c; while ((c = is.read()) != -1) time.append((char) c); String timeString = time.toString().trim(); System.out.println("It is " + timeString + " at " + hostname); } // end try catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } } // end main } // end DaytimeClient

Program No 5(A)
Object: - Write a Server program, which handle multiple clients. Program Code: import java.io.*; import java.net.*; import java.util.*; class serverthread { public static void main(String args[]) { try { int i; ServerSocket ss=new ServerSocket(6666); System.out.println("server is waiting for client"); i=1; while(true) { Socket s=ss.accept(); new ThreadHandler(s,i).start(); i++; } } catch(Eeception e) { System.out.println(e); } } } class ThreadHandler extends Thread{ Socket cs; ThreadHandler(Socket s,int i) { cs=s; System.out.println("no of client ="+i); } public void run() { try { DataInputStream dis=new DataInputStream(cs.getInputStream()); System.out.println(dis.readUTF()); } catch(Exception e) { System.out.print(e); } }}

Program No 5(B)
Object: - Write a Client program, which takes input from client and sends it to
server.

Program Code: import java.io.*; import java.net.*; import java.util.*; class clientthread { public static void main(String args[]) { try { Socket s=new Socket("localhost",6666); DataOutputStream dos=new DataOutputStream(s.getOutputStream()); DataInputStream dis=new DataInputStream(s.getInputStream()); BufferedReader br=new BufferedReader(newInputStreamReader(System.in)); dos.writeUTF(br.readLine()); } catch(Exception e2) { System.out.println(e2); } } }

Program No 6(A)
Object:-Write a Server Program that receives file from client and displays the
data.

Program Code: import java.io.*; import java.net.*; class fserver { public static DatagramSocket ds1; public static byte buffer[]=new byte[1024]; public static void main (String args[]) throws Exception { ds1= new DatagramSocket(2000); DatagramPacket p = new DatagramPacket(buffer,buffer.length); ds1.receive(p); System.out.println(" "+(new String(p.getData(), 0, p.getLength()))); FileOutputStream f1=new FileOutputStream(args[0]); f1.write(p.getData(), 0, p.getLength()); f1.close(); } }

Program No 6(B)
Object:-Write a Client Program that sends a file to the server.
.

Program Code: import java.io.*; import java.net.*; class clientf { public static DatagramSocket ds; public static byte buffer[] = new byte[1024]; public static void main(String args[]) throws Exception{ ds = new DatagramSocket(2001); int i, n; FileInputStream f1 = new FileInputStream(args[0]); n=f1.available(); f1.read(buffer,0,n); for(i=0; i<n; i++) System.out.println((char) buffer[i]); ds.send(new DatagramPacket(buffer,n,InetAddress.getLocalHost(),2000)); } }

Program No 7
Object: - Write a Web Server program. Program Code: import java.io.*; import java.net.*; import java.util.*; class webserver { public static void main(String argv[]) throws Exception { String requestmessageline; String filename; ServerSocket listenSocket=new ServerSocket(5555); Socket connectionsocket=listenSocket.accept(); BufferedReader infromclient=new BufferedReader(new InputStreamReader(connectionsocket.getInputStream())); DataOutputStream outtoclient=new DataOutputStream(connectionsocket.getOutputStream()); requestmessageline=infromclient.readLine(); StringTokenizer tokenizedline=new StringTokenizer(requestmessageline); if(tokenizedline.nextToken().equals("GET")) { filename=tokenizedline.nextToken(); if(filename.startsWith("/")==true) filename=filename.substring(1); File file=new File(filename); int numofbytes=(int)file.length(); FileInputStream infile=new FileInputStream(filename); byte[] fileinbytes=new byte[numofbytes];

infile.read(fileinbytes); outtoclient.writeBytes("HTTP/10 200 document follows \r\n"); if(filename.endsWith(".jpg")) outtoclient.writeBytes("content-type : image/jpg \r\n"); if(filename.endsWith(".gif")) outtoclient.writeBytes("content-type : image/gif \r\n"); outtoclient.writeBytes("content - length:"+numofbytes+"\r\n"); outtoclient.writeBytes("\r\n"); outtoclient.write(fileinbytes,0,numofbytes); connectionsocket.close(); } else System.out.println("Bad Request Message"); } }

You might also like