Computer Networks Lab
Computer Networks Lab
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
http://csetube.weebly.com/
SOCKET PROGRAMMING
CLIENT
import java.io.*;
import java.net.*;
class Client
{
public static void main(String args[]) throws Exception
{
Socket s=new Socket("localhost",50);
/
k
t
.
e
b
u
t
e
String str;
s
c
/
str=br.readLine();
os.println(str);
/
:
p
str=is.readLine();
t
t
h
SERVER
import java.io.*;
import java.net.*;
class Server
{
public static void main(String args[])throws Exception
{
http://csetube.weebly.com/
String c;
ServerSocket ss=new ServerSocket(50);
Socket s=ss.accept();
BufferedReader is=new BufferedReader(new InputStreamReader
(s.getInputStream()));
PrintStream os=new PrintStream(s.getOutputStream());
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
c=is.readLine();
os.println(c);
ss.close();
s.close();
}
/
k
t
.
e
b
OUTPUT
u
t
e
SERVER
Z:\cs2k944> javac Server.java
s
c
/
/
:
p
CLIENT
t
t
h
http://csetube.weebly.com/
DATAGRAM
CLIENT
import java.io.*;
import java.net.*;
class UDPClient
{
public static DatagramSocket clientsocket;
public static DatagramPacket dp;
public static BufferedReader dis;
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
ia = InetAddress.getLocalHost();
System.out.println("Client is Running... Type 'STOP' to Quit");
while(true) {
System.out.print("Client:");
String str = new String(dis.readLine());
buf = str.getBytes();
if(str.equalsIgnoreCase("STOP")) {
System.out.println("Terminated...");
clientsocket.send(new DatagramPacket(buf,str.length(), ia,sport));
break;
}
http://csetube.weebly.com/
SERVER
/
k
t
import java.io.*;
import java.net.*;
.
e
b
class UDPServer
{
u
t
e
s
c
/
/
:
p
t
t
h
http://csetube.weebly.com/
if(str.equalsIgnoreCase("STOP")) {
System.out.println("Terminated...");
break;
}
System.out.println("Client: " + str);
System.out.print("Server:");
String str1 = new String(dis.readLine());
buf = str1.getBytes();
serversocket.send(new DatagramPacket(buf,str1.length(), ia, cport));
}
/
k
t
}}
.
e
b
OUTPUT
u
t
e
SERVER
s
c
/
/
:
p
Server is Running...
Client: hai
Server:hello
Terminated...
t
t
h
CLIENT
Z:\cs2k944> javac UDPClient.java
Z:\cs2k944> java UDPClient
Client is Running... Type 'STOP' to Quit
Client:hai
Server: hello
Client:stop
Terminated...
http://csetube.weebly.com/
TCP SOCKETS
CLIENT
import java.net.*;
import java.io.*;
class TCPClient
{
public static void main(String args[]) throws Exception
{
Socket s=new Socket("localhost",2000);
BufferedReader is=new BufferedReader(new
InputStreamReader(s.getInputStream()));
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
while(true) {
System.out.print("Client : ");
t
t
h
str=br.readLine();
os.println(str);
str=is.readLine();
System.out.print("Server : "+str+"\n");
if ( str.equalsIgnoreCase("BYE") ) {
System.out.println("Terminated...");
break;
}
}
s.close();
is.close();
http://csetube.weebly.com/
os.close();
br.close();
}
}
SERVER
import java.net.*;
import java.io.*;
class TCPServer
{
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
System.out.println("Server is Running...");
while(true) {
str=is.readLine();
if (str.equalsIgnoreCase("stop")) {
os.println("BYE");
System.out.println("Terminated...");
break;
}
System. out.print("Client : "+str+"\n");
System.out.print("Server : ");
str=br.readLine();
http://csetube.weebly.com/
os.println(str);
}
ss.close();
s.close();
is.close();
os.close();
br.close();
}
}
/
k
t
OUTPUT
.
e
b
SERVER
Z:\cs2k944> javac TCPServer.java
u
t
e
s
c
/
Server is Running...
Client : hai
/
:
p
Server : hello
Terminated...
CLIENT
t
t
h
http://csetube.weebly.com/
SMTP
SMTPClient.java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class SMTPClient extends JFrame implements ActionListener {
JLabel from_lbl,pwd_lbl,to_lbl,sub_lbl,msg_lbl;
JTextField from_txt,to_txt,sub_txt;
JPasswordField pwd_txt;
/
k
t
JTextArea msg_txt;
.
e
b
JButton send_btn,cancel_btn;
JScrollPane sp;
u
t
e
SMTPClient() {
s
c
/
setVisible(true);
setSize(390,400);
/
:
p
setLocation(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
t
t
h
setLayout(null);
from_lbl=new JLabel("FROM:");
from_lbl.setBounds(30,20,100,20);
pwd_lbl=new JLabel("PASSWORD:");
pwd_lbl.setBounds(30,60,100,20);
to_lbl=new JLabel("TO:");
to_lbl.setBounds(30,100,100,20);
sub_lbl=new JLabel("SUBJECT:");
sub_lbl.setBounds(30,140,100,20);
msg_lbl=new JLabel("MESSAGE:");
msg_lbl.setBounds(30,180,100,20);
from_txt=new JTextField();
http://csetube.weebly.com/
from_txt.setBounds(150,20,200,20);
pwd_txt=new JPasswordField();
pwd_txt.setEchoChar('*');
pwd_txt.setBounds(150,60,200,20);
to_txt=new JTextField();
to_txt.setBounds(150,100,200,20);
sub_txt=new JTextField();
sub_txt.setBounds(150,140,200,20);
msg_txt=new JTextArea();
sp=new JScrollPane(msg_txt);
sp.setBounds(30,210,320,100);
/
k
t
send_btn=new JButton("SEND");
send_btn.setBounds(90,320,80,30);
.
e
b
cancel_btn=new JButton("CANCEL");
cancel_btn.setBounds(210,320,80,30);
u
t
e
add(from_lbl);
add(from_txt);
s
c
/
add(pwd_lbl);
add(pwd_txt);
/
:
p
add(to_lbl);
t
t
h
add(to_txt);
add(sub_lbl);
add(sub_txt);
add(msg_lbl);
add(sp);
add(send_btn);
add(cancel_btn);
send_btn.addActionListener(this);
cancel_btn.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
Object x=ae.getSource();
if(x==send_btn) {
http://csetube.weebly.com/
/
k
t
MyMail.java
.
e
b
import java.util.Properties;
u
t
e
import javax.mail.Message;
import javax.mail.MessagingException;
s
c
/
import javax.mail.Session;
import javax.mail.Transport;
/
:
p
import javax.mail.internet.InternetAddress;
t
t
h
import javax.mail.internet.MimeMessage;
import javax.swing.JOptionPane;
http://csetube.weebly.com/
this.msg = msg;
}
public void SendMail() {
host = "smtp.gmail.com";
Properties props = System.getProperties();
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", 587);
/
k
t
props.put("mail.smtp.auth", "true");
try{
.
e
b
u
t
e
message.setFrom(new InternetAddress(from));
s
c
/
/
:
p
message.setText(msg);
t
t
h
transport.connect(host,from, pass);
System.out.println("connected");
transport.sendMessage(message, message.getAllRecipients());
System.out.println("Sent message successfully....");
JOptionPane.showMessageDialog(null,"Message Successfully
Sent","Successful",JOptionPane.INFORMATION_MESSAGE);
transport.close();
System.exit(0);
}
catch (MessagingException me)
{
http://csetube.weebly.com/
JOptionPane.showMessageDialog(null,me,"Warning Message",
JOptionPane.WARNING_MESSAGE);
}
}
}
OUTPUT
Z:\cs2k944> javac SMTPClient.java
Z:\cs2k944> java SMTPClient
/
k
t
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
http://csetube.weebly.com/
/
k
t
BufferedReader get=null;
.
e
b
PrintWriter put=null;
try
u
t
e
s=new Socket("localhost",8081);
s
c
/
/
:
p
put=new PrintWriter(s.getOutputStream(),true);
}
t
t
h
catch(Exception e) {
System.exit(0);
}
String f;
int u;
System.out.println("Enter the file name to transfer from server:");
DataInputStream dis=new DataInputStream(System.in);
f=dis.readLine();
put.println(f);
File f1=new File(f);
FileOutputStream fs=new FileOutputStream(f1);
http://csetube.weebly.com/
while((u=get.read())!=-1) {
fs.write((char)u);
}
fs.close();
System.out.println("File received");
s.close();
}
}
SERVER
/
k
t
import java.io.*;
import java.net.*;
.
e
b
class FTPServer
u
t
e
s
c
/
/
:
p
ServerSocket ss=null;
try
{
t
t
h
ss=new ServerSocket(8081);
}
catch(IOException e)
{
System.out.println("couldn't listen");
System.exit(0);
}
Socket cs=null;
try
{
http://csetube.weebly.com/
cs=ss.accept();
System.out.println("Connection established"+cs);
}
catch(Exception e)
{
System.out.println("Accept failed");
System.exit(1);
}
PrintWriter put=new PrintWriter(cs.getOutputStream(),true);
BufferedReader st=new BufferedReader(new
InputStreamReader(cs.getInputStream()));
/
k
t
String s=st.readLine();
.
e
b
u
t
e
if(f.exists()) {
s
c
/
int line;
/
:
p
while((line=d.read())!=-1) {
t
t
h
put.write(line);
put.flush();
d.close();
System.out.println("File Transferred");
cs.close();
ss.close();
}
}
}
http://csetube.weebly.com/
OUTPUT
SERVER
Z:\cs2k944> javac FTPServer.java
Z:\cs2k944> java FTPServer
Connection establishedSocket[addr=/127.0.0.1,port=1030,localport=8081]
The requested file is: cns.txt
File Transferred
CLIENT
/
k
t
.
e
b
u
t
e
cns.txt
s
c
/
File received
/
:
p
t
t
h
http://csetube.weebly.com/
/
k
t
if(a>size)
System.out.println("BUCKET OVERFLOW");
.
e
b
else {
u
t
e
Thread.sleep(1);
time++;
s
c
/
while(a>b) {
/
:
p
System.out.println(b+"bytes outputted");
tt
a-=b;
Thread.sleep(1);
time++;
}
System.out.println("Time: "+time +"seconds");
if(a>0)
System.out.println("Last "+a+" bytes sent");
System.out.println("BUCKET OUTPUT SUCCESSFUL......");
}
}
http://csetube.weebly.com/
/
k
t
Thread.sleep(delay);
time+=delay;
.
e
b
pkt_size=(int)(Math.random()*1000);
System.out.println("\n"+"Time: "+time+"seconds");
u
t
e
s
c
/
bktInput(pkt_size,output_rate,bkt_size);
delay=(int)(Math.random()*10);
/
:
p
}
}
}
t
t
h
OUTPUT
Z:\cs2k944> javac LeakyBucket.java
Z:\cs2k944> java LeakyBucket
Enter the Bucket Size:
512
Enter the Output Rate:
256
Enter No. of Packets:
4
http://csetube.weebly.com/
Time: 1seconds
Packet No. 1 Packet Size = 99
Time: 2seconds
Last 99 bytes sent
BUCKET OUTPUT SUCCESSFUL......
Time: 10seconds
Packet No. 2 Packet Size = 831
BUCKET OVERFLOW
Time: 10seconds
Packet No. 3 Packet Size = 283
Time: 11seconds
256bytes outputted
Time: 12seconds
Last 27 bytes sent
BUCKET OUTPUT SUCCESSFUL......
/
k
t
Time: 21seconds
Packet No. 4 Packet Size = 861
BUCKET OVERFLOW
.
e
b
u
t
e
s
c
/
/
:
p
t
t
h
http://csetube.weebly.com/
/
k
t
System.out.println("\n"+"Time: "+time+"seconds");
token+=10;
.
e
b
u
t
e
s
c
/
else {
token=token-ps;
/
:
p
t
t
h
http://csetube.weebly.com/
time+=delay;
token+=(delay*10);
System.out.println("\n"+"Time: "+time+"seconds");
if(token>bkt_size) {
System.out.println("BUCKET OVERFLOW");
token=bkt_size;
}
pkt_size=(int)(Math.random()*50);
System.out.println("No. of Token = "+token);
System.out.println("Packet No.: "+i+" Packet Size: "+pkt_size);
/
k
t
bktInput(i,pkt_size,bkt_size);
delay=(int)(Math.random()*10);
.
e
b
}
}
u
t
e
s
c
/
OUTPUT
/
:
p
t
t
h
Time: 1seconds
No. of Token = 10
Packet No.: 1 Packet Size: 20
Waiting for enough token
http://csetube.weebly.com/
Time: 2seconds
No. of Token = 20
PACKET NO.1 IS OUTPUTTED SUCCESSFULLY...
Time: 3seconds
No. of Token = 10
Packet No.: 2 Packet Size: 22
Waiting for enough token
Time: 4seconds
/
k
t
No. of Token = 20
Waiting for enough token
.
e
b
Time: 5seconds
u
t
e
No. of Token = 30
s
c
/
/
:
p
Time: 8seconds
No. of Token = 38
t
t
h
Time: 16seconds
BUCKET OVERFLOW
No. of Token = 60
Packet No.: 4 Packet Size: 15
PACKET NO.4 IS OUTPUTTED SUCCESSFULLY...
http://csetube.weebly.com/