DCCN Lab Assignment 2
DCCN Lab Assignment 2
DCCN Lab Assignment 2
1. Write a program to display the name of the computer and its IP address that you are
currently working on.
import java.net.InetAddress;
import java.net.UnknownHostException;
InetAddress ip;
String hostname;
try {
ip = InetAddress.getLocalHost();
hostname = ip.getHostName();
System.out.println("Your current IP address : " + ip);
System.out.println("Your current Hostname : " + hostname);
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Output
import java.io.*;
import java.util.*;
import java.net.*;
try{
InetAddress[] p=InetAddress.getAllByName("www.google.com");
InetAddress[] p1=InetAddress.getAllByName("www.microsoft.com");
int i;
for(i=0;i<p.length;i++)
{
System.out.println("1:"+p[i]);
}
for(i=0;i<p1.length;i++)
{
System.out.println("1:"+p1[i]);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output
3.Write a program to list all ports hosting a TCP server in a specified host and identify available
servers in well-known ports?
To find ports of host
import java.io.*;
import java.net.*;
class quest3 {
public static void main(String[] args)
{
log(isSocketAliveUitlity("localhost", 8080));
log(isSocketAliveUitlity("localhost", 8081));
log(isSocketAliveUitlity("google.com", 80));
log(isSocketAliveUitlity("reddit.com", 81));
log(isSocketAliveUitlity("facebook.com", 80));
}
public static boolean isSocketAliveUitlitybyCrunchify(String hostName, int port) {
boolean isAlive = false;
SocketAddress socketAddress = new InetSocketAddress(hostName, port);
Socket socket = new Socket();
int timeout = 2000;
log("hostName: " + hostName + ", port: " + port);
try
{
socket.connect(socketAddress, timeout);
socket.close();
isAlive = true;
}catch (SocketTimeoutException exception)
{
System.out.println("SocketTimeoutException " + hostName + ":" + port + ". "+ exception
.getMessage());
} catch (IOException exception)
{
System.out.println("IOException - Unable to connect to " + hostName + ":" +port + ". "
+ exception.getMessage());
}
return isAlive;
}
Output
To find free ports
import java.net.*;
import java.io.*;
if (args.length > 0) {
host = args[0];
}
for (int i = 0; i < 1024; i++) {
try {
System.out.println("Looking for "+ i);
Skt = new Socket(host, i);
System.out.println("There is a server on port " + i + " of " + host);
} catch (UnknownHostException e) {
System.out.println("Exception occured"+ e);
break;
} catch (IOException e) {}
}
}
}
Output
4. Write a program to implement a simple message transfer from client to server process using
TCP
Client Side
import java.io.*;
import java.net.*;
public class two7client {
public static void main(String[] args) {
try
{
Socket s=new Socket("localhost",6666);
DataOutputStream dout=new DataOutputStream(s.getOutputStream());
dout.writeUTF("Hello my name is Piyush Sahu 19BIT0038");
dout.flush();
dout.close();
s.close();
}
catch(Exception e){System.out.println(e);}
}
}
Server side
import java.io.*;
import java.net.*;
public class two7server {
public static void main(String[] args){
try
{
ServerSocket ss=new ServerSocket(6666);
Socket s=ss.accept();
DataInputStream dis=new DataInputStream(s.getInputStream());
String str=(String)dis.readUTF();
System.out.println("message = "+str);
ss.close();
}
catch(Exception e){System.out.println(e);}
}
}
Output
5. Write a program to implement date server and client in java using TCP sockets.
CODE
Client code
import java.io.*;
import java.net.*;
class Q5Client
{
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());
}
}
Server Code
import java.net.*;
import java.io.*;
import java.util.*;
class Q5Server
{
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();
}
}
}
Output
6. Write a program to implement TCP client/server application for transferring a text file from
client to server?
Server Code
import java.net.*;
import java.io.*;
Client Code
import java.net.*;
import java.io.*;
try {
Socket s = new Socket("localhost", 1999);
BufferedReader br = new BufferedReader(new FileReader("E:\\test.txt"));
byte []b = new byte[30];
String k = br.readLine();
DataOutputStream dos = new DataOutputStream(s.getOutputStream());
dos.writeUTF(k);
System.out.println("File Transferred");
} catch (IOException ie) {
ie.printStackTrace();
}
}
}
Output
7. Write a program to implement a chat server and client in java using TCP sockets.
Client Code
import java.io.*;
import java.net.*;
dos.writeUTF("Welcome ");
String str1,str2;
while(true) {
str2= dis.readUTF();
if(str2.equals("quit"))
break;
System.out.println("Client: "+ str2);
System.out.print("Server: ");
str1=br.readLine();
dos.writeUTF(str1);
if(str1.equals("quit"))
break;
}
ss.close();
}
}
Output
8. Implement a TCP based server program to authenticate the client’s User Name and
Password. The validity of the client must be sent as the reply message to the client and display
it on the standard output.
CODE
out.println(uname);
out.println(pword);
}
}
Server code
import java.util.*;
import java.io.*;
import java.net.*;
String ouname="Piyush";
String opword="abc123";
Client code
import java.io.*;
import java.util.*;
import java.net.*;
class two9cliento {
public static void main(String[] args)throws Exception {
String number,pin,r1,amt,r2;
Socket s= new Socket("localhost",9086);
BufferedReader inc = new BufferedReader(new InputStreamReader(System.in));
DataOutputStream out = new DataOutputStream(s.getOutputStream());
BufferedReader ins = new BufferedReader(new InputStreamReader(s.getInputStream()));
System.out.println("Enter card Number");
number = inc.readLine();
out.writeBytes(number + '\n');
System.out.println("Enter Pin");
pin = inc.readLine();
out.writeBytes(pin + '\n');
r1 = ins.readLine();
System.out.println(r1);
if(r1.equals("welcome")){
System.out.println("Enter withdraw Amount :");
amt = inc.readLine();
out.writeBytes(amt + '\n');
r2=ins.readLine();
System.out.println(r2);
}
}
}
Server Code
import java.io.*;
import java.util.*;
import java.net.*;
class two9servero{
public static void main(String[] args) throws Exception
{
String name = "Piyush";
int cno = 1234;
int pin = 4321;
int bal = 6000;
String x,y,z;
int no,pin1,amt;
String a1="welcome";
String a2="Invalid Credentials";
String a3="withdraw successful";
String a4="insufficient balance";
ServerSocket ss= new ServerSocket(9086);
while(true) {
Socket s = ss.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
DataOutputStream out = new DataOutputStream(s.getOutputStream());
x=in.readLine();
no=Integer.parseInt(x);
y=in.readLine();
pin1=Integer.parseInt(y);
if(cno==no&&pin==pin1) {
out.writeBytes(a1+'\n');
z=in.readLine();
amt=Integer.parseInt(z);
if(amt<=bal) {
bal=bal-amt;
out.writeBytes(a3+'\n');
}
else {
out.writeBytes(a4+'\n');
}
}
else {
out.writeBytes(a2+'\n');
}
}
}
OUTPUT