School of Information Technology and Engineering

Download as pdf or txt
Download as pdf or txt
You are on page 1of 35

SCHOOL OF INFORMATION TECHNOLOGY AND

ENGINEERING

WINTER SEMESTER 2020-21

SWE2002 – COMPUTER NETWORKS

Digital Assignment – III

Faculty – DR.C.NAVANEETHAN SIR


Slot: L59+L60
NAME:K.B.NEERAJ KUMAR
REG NO: 19MIS0349
1. Write a program to implement any one sorting algorithm using
TCP on Server application. Give Input on Client side and client
should get sorted output from server and display Sorted on input
side.

Program:
Server:
import java.io.*;
import java.net.*;
import java.util.*;
class Server
{
public static void main(String args[]) throws Exception
{
ServerSocket ss=new ServerSocket(7777); Socket s=ss.accept();
System.out.println("connected……. ");
DataInputStream din=new DataInputStream(s.getInputStream());
DataOutputStream dout=new DataOutputStream(s.getOutputStream()); int
r,i=0;
int n=din.readInt(); int a[]=new int[n];
System.out.println("data:"); int count=0;
System.out.println("Receiving Data….");
for(i=0;i<n;i++)
{
a[i]=din.readInt();
}
System.out.println("Data Received");
System.out.println("Sorting Data……….");
Arrays.sort(a); System.out.println("Data Sorted");
System.out.println("Sending Data………");
for(i=0;i<n;i++)
{
dout.writeInt(a[i]);
}
System.out.println("\nData Sent Successfully"); s.close();
ss.close();
}
}

Client:
import java.io.*;

import java.net.*;

import java.util.Scanner;

public class Client {

public static void main(String[] args) throws Exception

Socket s=new Socket("127.0.0.1",7777); if(s.isConnected())

System.out.println("Connected to server");

System.out.println("Enter size of array:"); Scanner scanner=new


Scanner(System.in);

int n=scanner.nextInt(); int a[]=new int[n];

System.out.println("Enter element to array:");

DataOutputStream dout=new DataOutputStream(s.getOutputStream());


dout.writeInt(n);

for(int i=0;i<n;i++)
{

int r=scanner.nextInt();; dout.writeInt(r);

System.out.println("Data Sent");

DataInputStream din=new DataInputStream(s.getInputStream()); int r;

System.out.println("Receiving Sorted Data……..");

for(int i=0;i<n;i++)

r=din.readInt(); System.out.print(r+" ");

s.close();

OUTPUT:
Server:

Client
2. Write a TCP /IP socket programming where client sends messages and
server checks for Palindrome and displayed on server side
Server:
import java.io.*; import java.net.*;

public class ServerCode2 { ServerSocket ss;

Socket socket;

BufferedReader sock_in,kdb_in; PrintWriter sock_out;

String str;

public Server2()

try{

ss=new ServerSocket(8978); System.out.println("Server is listening port 8765");


socket=ss.accept();
System.out.println("Connection established...");

kdb_in=new BufferedReader(new InputStreamReader(System.in));

sock_in=new BufferedReader(new InputStreamReader(socket.getInputStream()));


sock_out=new PrintWriter(socket.getOutputStream());

while(true)

System.out.println("Msg from client"); str=sock_in.readLine();

int k=str.length(); System.out.println(str);

int left=0,right=k-1;int flag=1; while(left<=right)

if(str.charAt(left)!=(str.charAt(right)))

flag=0; break;

else

left++;right--;

if(flag==1) str=" ... Palindrome. ";

else

str="....Not Palindrome ";

//System.out.println("Enter the msg");


//str=kdb_in.readLine(); sock_out.println(str); sock_out.flush();
if(str.equals("bye")) break;

}catch (Exception e) { }

public static void main(String arg[])

new ServerCode2();

Client:
import java.io.*;

import java.net.*;

public class Client2{

Socket socket;

BufferedReader sock_in,kdb_in; PrintWriter sock_out;

String str;

public ClientCode()

try{

Socket socket=new Socket("127.0.0.1",8765);

kdb_in=new BufferedReader(new InputStreamReader(System.in));


sock_in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
sock_out=new PrintWriter(socket.getOutputStream());

while(true)

System.out.println("Enter the msg"); str=kdb_in.readLine(); sock_out.println(str);


sock_out.flush(); System.out.println("Msg from Server"); str=sock_in.readLine();
System.out.println(str); if(str.equals("bye"))

break;

socket.close();

}catch (Exception e) { }

public static void main(String arg[])

new ClientCode();

OUTPUT:
Server:
Client:

3. Write a program for sorting TCP/IP protocol where client sends


numbers and server Responds in sorted order.

Server:
import java.io.*;

import java.net.*;

import java.util.*;

class server3

public static void main(String args[]) throws Exception

{
ServerSocket ss=new ServerSocket(7777); Socket s=ss.accept();
System.out.println("connected…….. ");

DataInputStream din=new DataInputStream(s.getInputStream());


DataOutputStream dout=new DataOutputStream(s.getOutputStream()); int r,i=0;

int n=din.readInt(); int a[]=new int[n];

System.out.println("data:"); int count=0;

System.out.println("Receiving Data…. ");

for(i=0;i<n;i++)

a[i]=din.readInt();

System.out.println("Data Received"); System.out.println("Sorting Data……… ");

Arrays.sort(a); System.out.println("Data Sorted");

System.out.println("Sending Data…….");

for(i=0;i<n;i++)

dout.writeInt(a[i]);

System.out.println("\nData Sent Successfully"); s.close();

ss.close();

}
Client:

import java.io.*;

import java.net.*;

import java.util.Scanner;

public class Client {

public static void main(String[] args) throws Exception

Socket s=new Socket("127.0.0.1",7777); if(s.isConnected())

System.out.println("Connected to server");

System.out.println("Enter size of array:"); Scanner scanner=new


Scanner(System.in);

int n=scanner.nextInt(); int a[]=new int[n];

System.out.println("Enter element to array:");

DataOutputStream dout=new DataOutputStream(s.getOutputStream());


dout.writeInt(n);

for(int i=0;i<n;i++)

int r=scanner.nextInt();; dout.writeInt(r);

System.out.println("Data Sent");
DataInputStream din=new DataInputStream(s.getInputStream()); int r;

System.out.println("Receiving Sorted Data………");

for(int i=0;i<n;i++)

r=din.readInt(); System.out.print(r+" ");

s.close();

OUTPUT:

Server:
Client:

4. There is request of DATE and TIME from Client to server. For


Connection oriented Protocol, write a program to display DATE
and Time in the Client side.

Server:

import java.net.*;

import java.io.*;

import java.util.*;

class DateServer

public static void main(String args[]) throws Exception


{

ServerSocket s=new ServerSocket(5217); while(true)

System.out.println("Waiting For Connection ..."); Socket soc=s.accept();

DataOutputStream out=new DataOutputStream(soc.getOutputStream());


out.writeBytes("Server Date: " + (new Date()).toString() + "\n"); out.close();

soc.close();

Client:

import java.io.*;

import java.net.*;

class DateClient

public static void main(String args[]) throws Exception

Socket soc=new Socket(InetAddress.getLocalHost(),5217);

BufferedReader in=new BufferedReader(new


InputStreamReader(soc.getInputStream()

));

System.out.println(in.readLine());

}
OUTPUT:

Server:

Client:

5. Create a chat application using TCP/IP Protocol

Server:
import java.io.*;

import java.net.*;

class ChatServer

public static DatagramSocket serversocket; public static DatagramPacket dp;

public static BufferedReader dis; public static InetAddress ia;

public static byte buf[] = new byte[1024]; public static int cport =

789,sport=790;

public static void main(String[] a) throws IOException


{

serversocket = new DatagramSocket(sport); dp = new


DatagramPacket(buf,buf.length); dis = new BufferedReader

(new InputStreamReader(System.in)); ia = InetAddress.getLocalHost();

System.out.println("Server is Running..."); while(true)

{
serversocket.receive(dp);

String str = new String(dp.getData(), 0, dp.getLength());

if(str.equals("STOP"))

{
System.out.println("Terminated..."); break;

System.out.println("Client: " + str); String str1 = new String(dis.readLine());

buf = str1.getBytes(); serversocket.send(new

DatagramPacket(buf,str1.length(), ia, cport));

}
}

Client:
import java.io.*;

import java.net.*;

class ChatClient

public static DatagramSocket clientsocket; public static DatagramPacket dp;

public static BufferedReader dis; public static InetAddress ia;


public static byte buf[] = new byte[1024]; public static int cport = 789, sport =

790;

public static void main(String[] a) throws IOException

clientsocket = new DatagramSocket(cport); dp = new DatagramPacket(buf,

buf.length);

dis = new BufferedReader(new InputStreamReader(System.in)); ia =

InetAddress.getLocalHost();

System.out.println("Client is Running..." ); while(true)

String str = new String(dis.readLine()); buf = str.getBytes();

if(str.equals("STOP"))

System.out.println("Terminated...");

clientsocket.send(new DatagramPacket(buf,str.length(), ia,sport)); break;

clientsocket.send(new DatagramPacket(buf,str.length(), ia, sport));

clientsocket.receive(dp);

String str2 = new String(dp.getData(), 0, dp.getLength());

System.out.println("Server: " + str2);

}
}

OUTPUT:

Server:

Client:

6. Implement with socket programming for TCP server to transfer


files from Client socket to Server socket

Server:

import java.io.*;

import java.net.*;

class Question6

public static void main(String[] args) throws Exception


{

try (ServerSocket ss = new ServerSocket(7777); Socket s = ss.accept()) {

System.out.println("connected..........");

FileInputStream fin=new FileInputStream("hi.txt");

DataOutputStream dout=new DataOutputStream(s.getOutputStream());

int r;

while((r=fin.read())!=-1)

dout.write(r);

System.out.println("\nFiletranfer Completed");

Client:

import java.io.*;

import java.net.*;

public class Question6Client {

public static void main(String[] args) throws Exception


{

try (Socket s = new Socket("127.0.0.1",7777)) {

if(s.isConnected())

System.out.println("Connected to server");

FileOutputStream fout;

fout = new FileOutputStream("received.txt");

DataInputStream din=new DataInputStream(s.getInputStream());

int r;

while((r=din.read())!=-1)

fout.write((char)r);

OUTPUT:
Server:

Client:

7. Write a socket programming where Server to respond more than


one client to respond reverse of string received from each client.

Server:

import java.io.*;

import java.net.*;

public class ReverseServer {

public static void main(String[] args) {

try{

ServerSocket serverSocket = new ServerSocket(12345);

System.out.println("Server is listening on port " + 12345);

while (true) {

Socket socket = serverSocket.accept();


System.out.println("New client connected");

InputStream input = socket.getInputStream();

BufferedReader reader = new BufferedReader(new

InputStreamReader(input));

OutputStream output = socket.getOutputStream();

PrintWriter writer = new PrintWriter(output, true);

String text;

do {

text = reader.readLine();

String reverseText = new

StringBuilder(text).reverse().toString();

writer.println("Server: " + reverseText);

} while (!text.equals("bye"));

socket.close();

}catch(IOException e){

System.out.println("Server exception: " + e.getMessage());

}
}

Client:

import java.net.*;

import java.io.*;

/**

* @author Neeraj

*/

public class Client7 {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

try{

Socket socket = new Socket("127.0.0.1",12345);

OutputStream output = socket.getOutputStream();


PrintWriter writer = new PrintWriter(output, true);

Console console = System.console();

String text;

do {

text = console.readLine("Enter text: ");

writer.println(text);

InputStream input = socket.getInputStream();

BufferedReader reader = new BufferedReader(new

InputStreamReader(input));

String time = reader.readLine();

System.out.println(time);

} while (!text.equals("bye"));

socket.close();

}catch(IOException e){

System.out.println(e);

}
OUTPUT:

Server:

Client:

8. Create an HTTP socket program for webpage upload and download.


import java.io.*;
import java.net.*;

public class Client8 {


public static void main(String[] args) {

String hostName = "www.martinbroadhurst.com";


int portNumber = 80;

try {
Socket socket = new Socket(hostName, portNumber);
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
BufferedReader in =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out.println("GET / HTTP/1.1\nHost: www.instagram.com\n\n");
String inputLine;
while ((inputLine = in.readLine()) != null) {
System.out.println(inputLine);
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
}

OUTPUT:
9. The Academics office of VIT wishes to make the Registrations
more secured. If you are a Programmer how you will implement a
system to validate thelogin credentials obtained from the user
thereby denying them access to unauthorized users.

Server:

import java.net.*;

import java.io.*;

public class server9

public static void main(String args[])


{

int port=4917; try

ServerSocket ss=new ServerSocket(port); System.out.println(Waiting for a client.);


Socket socket = ss.accept(); System.out.println(Got a client :)..);
System.out.println(); InputStreamReader(System.in));

DataInputStream in = new BufferedReader(new InputStreamReader(System.in));


DataInputStream out = new DataOutputStream(sout);String line =null; While(true)

line = in.readUTF(); line=keyboard.readLine();

System.out.printlnIm sending it .+line);

Out.writeUTF(line);

System.out.println(Waiting for the next line); System.out.println();

ss.close();

OUTPUT:
10. Implementa TCP/IPbased Serverprogramin Javatocompute
thefactorialofanumber(can contain arbitrary number of digits).
Write a client program to test the working of the same)

Server:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server10 {


public static void main(String[] args) throws Exception{
ServerSocket ss1=new ServerSocket(777);
Socket s1=ss1.accept();
System.out.println("Server is Ready...");
BufferedReader kb1=new BufferedReader(new
InputStreamReader(s1.getInputStream()));
String fname=kb1.readLine();
System.out.println("Received Number From Client : " + fname);
int num=Integer.parseInt(fname);
System.out.println("Counting Factorial ...");
int fact= factorial(num);
System.out.println("Send Factorial Value Of Number To Client
...");
PrintStream dos1=new PrintStream(s1.getOutputStream());
dos1.println(String.valueOf(fact));
s1.close();
ss1.close();
}
public static int factorial(int n){
if(n==0)
return 1;
else
return n* (n-1);
}
}
Client:
import java.io.*;

import java.net.Socket;

public class Client10 {

public static void main(String args[]) throws Exception{

Socket s=new Socket("localhost", 777);

BufferedReader br=new BufferedReader(new


InputStreamReader(System.in));

System.out.print("Enter Number To Find Factorial : ");

String fname=br.readLine();
PrintStream dos=new PrintStream(s.getOutputStream());

dos.println(fname);

System.out.println("Send Number To Server...");

BufferedReader kb=new BufferedReader(new


InputStreamReader(s.getInputStream()));

String sts=kb.readLine();

System.out.println("Received Factorial Value From Server ... ");

System.out.println("Factorial Of " + fname + " is : " + Integer.parseInt(sts));

s.close();

OUTPUT:
Server:

Client:

You might also like