Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DCCN Lab Assignment 2

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

DCCN lab assignment 2

ITE3001-Data Communication and Computer Networks


Laboratory
Introduction to TCP Client and Server Programming
By-
Piyush Sahu
19BIT0038

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;

public class two1 {

public static void main(String[] args) {

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

2. Write a program to print the IP address of www.google.com and all IP addresses of


www.microsoft.com

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

public class two2


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

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;
}

private static void log(String string) {


System.out.println(string);
}

private static void log(boolean isAlive) {


System.out.println("isAlive result: " + isAlive + "\n");
}
}

Output
To find free ports
import java.net.*;
import java.io.*;

public class quest3tp {


public static void main(String[] args) {
Socket Skt;
String host = "localhost";

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.*;

public class Q6Server {


public static void main (String []args) {
try
{
ServerSocket ss = new ServerSocket(1999);
Socket s = ss.accept();
DataInputStream dis = new DataInputStream(s.getInputStream());
String k = dis.readUTF();
System.out.println("File Received from Client and content of file written to ServerTrans
fer.txt");
FileOutputStream fos = new FileOutputStream("E:\\test.txt");
byte[] b = k.getBytes();
fos.write(b);
}
catch (IOException ie)
{
ie.printStackTrace();
}
}
}

Client Code
import java.net.*;
import java.io.*;

public class Q6Client {


public static void main(String[] args) {

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.*;

public class q7Clientr {


public static void main(String[] args) throws Exception {
Socket s= new Socket("localhost", 3000);
DataOutputStream dos= new DataOutputStream(s.getOutputStream());
DataInputStream dis= new DataInputStream(s.getInputStream());
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

System.out.println("Server: "+ dis.readUTF());


String str1,str2;
while(true) {
System.out.print("Client: ");
str1=br.readLine();
dos.writeUTF(str1);
if(str1.equals("quit"))
break;
str2= dis.readUTF();
if(str2.equals("quit"))
break;
System.out.println("Server: "+ str2);
dos.flush();
}
s.close();
}
}
Server Code
import java.io.*;
import java.net.*;

public class q7Serverr {


public static void main(String[] args) throws Exception {
ServerSocket ss= new ServerSocket(3000);
Socket s= ss.accept();
DataOutputStream dos= new DataOutputStream(s.getOutputStream());
DataInputStream dis= new DataInputStream(s.getInputStream());
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));

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

Client side code


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

public class two8client


{
public static void main(String args[])
{
try
{
System.out.println("client started");
Socket soc=new Socket("localhost",9806);
BufferedReader userinput=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter your username");
String uname=userinput.readLine();
System.out.println("enter your password");
String pword=userinput.readLine();

PrintWriter out=new PrintWriter(soc.getOutputStream(),true);

out.println(uname);
out.println(pword);

BufferedReader in=new BufferedReader(new InputStreamReader(soc.getInputStream()));


System.out.println(in.readLine());
}
catch(Exception e)
{
System.out.println(e);
}

}
}
Server code
import java.util.*;
import java.io.*;
import java.net.*;

public class two8server


{
public static void main(String args[])
{
try
{
System.out.println("server waiting for client");
ServerSocket ss = new ServerSocket(9806);
Socket soc=ss.accept();
System.out.println("connection established ");
BufferedReader in=new BufferedReader(new InputStreamReader(soc.getInputStream()));
String uname=in.readLine();
String pword=in.readLine();

PrintWriter out=new PrintWriter(soc.getOutputStream(),true);

String ouname="Piyush";
String opword="abc123";

if(ouname.equals(uname) && opword.equals(pword))


{
out.println("correct username and password, your are logged in");
}
else
{
out.println("password and/or username incorrect");
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Output
9. Implement a TCP/IP socket based ATM System. Make the server to maintain the customer
details (name, card no, pin and balance). When a client wants to withdraw amount, validate his
login with card no & pin, display a welcome message and perform the withdraw operation if he
is having sufficient balance or display a warning message

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

You might also like