0% found this document useful (0 votes)
25 views16 pages

Pra15 17

Uploaded by

Priti Mane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views16 pages

Pra15 17

Uploaded by

Priti Mane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Exercise:

1)

import java.net.*;

public class URLInfo

public static void main(String[] args)

try

URL url = new URL("http://www.msbte.org.in");

System.out.println("Host: " + url.getHost());

System.out.println("Protocol: " + url.getProtocol());

System.out.println("Port: " + url.getPort());

System.out.println("File: " + url.getFile());

catch (MalformedURLException e)

System.out.println("Malformed URL: " + e.getMessage());

OUTPUT:
2)

import java.net.*;

import java.io.*;

import java.util.Scanner;

public class URLInfo

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

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a URL: ");

String urlString = scanner.nextLine();

URL url = new URL(urlString);

URLConnection urlConnection = url.openConnection();

long date = urlConnection.getDate();

if (date == 0)

System.out.println("No date information.");

else

System.out.println("Date: " + new java.util.Date(date));

String contentType = urlConnection.getContentType();

if (contentType == null)

System.out.println("No content type information.");


}

else

System.out.println("Content Type: " + contentType);

int contentLength = urlConnection.getContentLength();

if (contentLength == -1)

System.out.println("No content length information.");

else

System.out.println("Content Length: " + contentLength + " bytes");

OUTPUT:
Practical 16
Name: Hulage Dnyaneshwari Shivaji Roll No:24/35-
019
Client Code:
import java.net.*;
import java.io.*;
class ClientProg
{
public static void main(String args[]) throws IOException
{
Socket s=new Socket("localhost",100);
DataOutputStream dos =new
DataOutputStream(s.getOutputStream());
DataInputStream dio=new DataInputStream(s.getInputStream());
System.out.println("Client application is sending username");
dos.writeUTF("Sujit");
s.close();
}
}

Server Code:
import java.net.*;
import java.io.*;
public class ServerProg
{
public static void main(String args[]) throws IOException
{
ServerSocket ss=new ServerSocket(100);
Socket s=ss.accept();
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();

System.out.println("Server says, Hello "+str);


ss.close();
s.close();
}
}

OUTPUT:

Exercise:
Server Program:

import java.net.*;
import java.io.*;
public class ServerProg
{
public static void main(String args[]) throws IOException
{
ServerSocket ss=new ServerSocket(100);
Socket s=ss.accept();
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("Server says, Hello "+str);
ss.close();
s.close();
}
}

Client Program:

import java.net.*;
import java.io.*;
import java.util.*;
class ClientProg
{
public static void main(String args[]) throws IOException
{
Socket s = new Socket("localhost", 100);
Scanner sc = new Scanner(System.in);
System.out.println("Enter message: ");
String msg = sc.nextLine();
DataOutputStream dos = new
DataOutputStream(s.getOutputStream());
DataInputStream dio = new
DataInputStream(s.getInputStream());
dos.writeUTF(msg);
s.close();
}
}

OUTPUT:
2)

Server Program:

import java.net.*;

import java.io.*;

public class ServerProg

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

ServerSocket ss = new ServerSocket(1234);

Socket s = ss.accept();

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

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

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

System.out.println("Server is waiting for request input from client");

String str=(String)dis2.readUTF();

System.out.println("Server receives input from client");

int num = Integer.parseInt(str);

int cnt=0;

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

if(num%i==0)

cnt++;

if(cnt==2)

dos2.writeUTF(num+" is prime");

else

dos2.writeUTF(num+" is not prime");

System.out.println("The number is detected wether prime or not.");

ss.close();

s.close();

Client Program:
import java.net.*;
import java.io.*;
public class ClientProg
{
public static void main(String args[]) throws IOException
{
Socket s = new Socket("localhost", 1234);
DataInputStream dis2 = new DataInputStream(s.getInputStream());
DataOutputStream dos2 = new
DataOutputStream(s.getOutputStream());
System.out.println("Client application sending request value");
dos2.writeUTF("11");
String ans = (String)dis2.readUTF();
//System.out.println("Client program receive result from server");
System.out.println(" " + ans);
s.close();
}
}
OUTPUT:
Practical 17
Name: Hulage Dnyaneshwari Shivaji Roll No:24/35-
019
Data Sender:

import java.net.*;

public class DataSender

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

DatagramSocket ds = new DatagramSocket();

String str = "Welcome Java";

InetAddress ip = InetAddress.getByName("127.0.0.1");

DatagramPacket dp = new DatagramPacket(str.getBytes(),


str.length(), ip, 3000);

ds.send(dp);

ds.close();

Data Receiver:

import java.net.*;

public class DataReceiver

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

DatagramSocket ds = new DatagramSocket(3000);


byte[] buf = new byte[1024];

DatagramPacket dp = new DatagramPacket(buf, 1024);

ds.receive(dp);

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

System.out.println(str);

ds.close();

OUTPUT:

Exercise:
Data Sender:

import java.net.*;

import java.util.*;

public class DataSender

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

{
Scanner sc = new Scanner(System.in);

DatagramSocket ds = new DatagramSocket();

System.out.println("Enter message:");

String msg = sc.nextLine();

InetAddress ip = InetAddress.getByName("127.0.0.1");

DatagramPacket dp = new DatagramPacket(msg.getBytes(),


msg.length(), ip, 3000);

ds.send(dp);

ds.close();

sc.close();

Data Receiver:

import java.net.*;
public class DataReceiver
{
public static void main(String args[]) throws Exception
{
DatagramSocket ds = new DatagramSocket(3000);
byte[] buf = new byte[1024];
DatagramPacket dp = new DatagramPacket(buf, 1024);
ds.receive(dp);
String str = new String(dp.getData(), 0, dp.getLength());
System.out.println(str);
ds.close();
}
}
OUTPUT:
Data Sender:

import java.net.*;
import java.io.*;
import java.util.*;
public class DataSender
{
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner(System.in);
DatagramSocket ds = new DatagramSocket();
System.out.println("Enter the source file path:");
String sourceFilePath = sc.nextLine();
FileInputStream fis = new FileInputStream(sourceFilePath);
InetAddress ip = InetAddress.getByName("127.0.0.1");
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1)
{
DatagramPacket dp = new DatagramPacket(buffer, bytesRead, ip,
3000);
ds.send(dp);
}
fis.close();
ds.close();
sc.close();
System.out.println("File contents sent successfully.");
}
}

Data Receiver:

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

public class DataReceiver


{
public static void main(String[] args) throws Exception
{
DatagramSocket ds = new DatagramSocket(3000);
byte[] buffer = new byte[1024];
System.out.println("Enter the destination file path:");
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String destinationFilePath = br.readLine();
FileOutputStream fos = new FileOutputStream(destinationFilePath);
while (true)
{
DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
ds.receive(dp);
fos.write(dp.getData(), 0, dp.getLength());
System.out.println("Contents copied: " + new String(dp.getData(), 0,
dp.getLength()));
}
}
}

OUTPUT:

You might also like