Sei sulla pagina 1di 11

NAME : P.

JASWANTH

REG.NO : 17MIS0379

SUBJECT : COMPUTER NETWORKS

FACULTY NAME : PROF.USHA DEVI.G

SLOT : L59+L60
To study about InetAddress and NetworkInterface classes and
its methods

1. Write java programs

(i)to print the ipaddress of www.vit.ac.in

(ii)to find the address of a local machine

(iii)to list all network interfaces

Run the programs and upload the programs with snapshots of


its outputs.

(i)Program:
GetByName.java
import java.net.*;
public class GetByName {
public static void main (String[] args) {
try {
InetAddress address = InetAddress.getByName("www.vit.ac.in");
System.out.println(address);
}
catch (UnknownHostException ex) {
System.out.println("Could not find www.vit.ac.in");
}
}

}
Output:

Ip address:10.10.1.75

(ii)Program:

Myaddress.java

import java.net.*;

public class MyAddress {

public static void main (String[] args) {

try {

InetAddress address = InetAddress.getLocalHost();

System.out.println(address);

} catch (UnknownHostException ex) {

System.out.println("Could not find this computer's address.");


Output:

(iii)Program:

InterfaceLister.java

import java.net.*;

import java.util.*;

public class InterfaceLister {

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

Enumeration interfaces = NetworkInterface.getNetworkInterfaces();

while (interfaces.hasMoreElements()) {

NetworkInterface ni = (NetworkInterface) interfaces.nextElement();

System.out.println(ni);

}
Output:

Objective:
To implement a simple message transfer from client to server process using TCP
sockets.
Questions:
1. To develop a client side program to send the message to the server.
2. To develop a server side program to receive the message from the client and
display it.

1.Program:
Server .java
import java.net.*;

import java.io.*;

public class server {

public static void main(String args[]) {


int port = 4917; // just a random port number between 1025 and 65535

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();

InputStream sin = socket.getInputStream();

OutputStream sout = socket.getOutputStream();

BufferedReader keyboard = new BufferedReader(new

InputStreamReader(System.in));

DataInputStream in = new DataInputStream(sin);

DataOutputStream out = new DataOutputStream(sout);

String line = null;

while(true) {

line = in.readUTF();

line=keyboard.readLine();

System.out.println("I'm sending it ..."+line);

out.writeUTF(line);

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

System.out.println();

ss.close();

catch(Exception x) { System.out.println("Exception caught"+x); }

}
Output:

2.Program:
Client.java
import java.net.*;

import java.io.*;

public class Client {

public static void main(String[] ar) {

try {

Socket socket = new Socket("192.168.1.7",4917);

InputStream sin = socket.getInputStream();

OutputStream sout = socket.getOutputStream();

DataInputStream in = new DataInputStream(sin);

DataOutputStream out = new DataOutputStream(sout);

BufferedReader keyboard = new BufferedReader(new

InputStreamReader(System.in));

String line = null;

System.out.println("Type a message and press enter.");

System.out.println();

while(true) {

line = keyboard.readLine();
out.writeUTF(line);

out.flush();

line = in.readUTF();

System.out.println("The message is : " + line);

System.out.println();

catch(Exception x) { System.out.println("exception caught"+x);}

Output:
Objective:

To implement a simple message transfer from client to server process using UDP
sockets.

Questions:

1. To develop a client side program to send the message to the server.

2. To develop a server side program to receive the message from the client and
display it.

1.Program:
Server .java

import java.net.*;

import java.io.*;

public class server {

public static void main(String args[]) {

try {

byte buffer = new byte[65536];

DatagramPacket incoming = new DatagramPacket(buffer, buffer.length);

DatagramSocket ds = new DatagramSocket(2134);

ds.receive(incoming);

byte[] data = incoming.getData();

String s = new String(data, 0, data.getLength());

System.out.println("Port " + incoming.getPort() + " on " + incoming.getAddress() + " sent this

message:");

System.out.println(s);
}

catch (IOException e) {

System.err.println(e);

Output:

2.Program:
Client.java

import java.net.*;

import java.io.*;

public class Client {


public static void main(String[] ar) {

try {

InetAddress addr = new InetAddess("localhost");

int p = 19;

String s = "My UDP Packet";

byte[] b = s.getBytes();

DatagramPacket dp = new DatagramPacket(b, b.length, addr, p);

DatagramSocket sender = new DatagramSocket();

sender.send(dp);

catch (UnknownHostException e) { System.err.println(e); }

Output:

Potrebbero piacerti anche