Tuesday, October 12, 2010

cn lab print












lab print


FTP Client
import java.io.*;
import java.net.*;
public class ftpc
{
public static void main(String args[])throws Exception
            {
                        Socket s=null;
                        DataInputStream si=null;
                        s=new Socket("192.168.3.13",55555);
                        si=new DataInputStream(s.getInputStream());
                        DataInputStream inp=new DataInputStream(System.in);
                        DataOutputStream so=new DataOutputStream(s.getOutputStream());
                        String str;
                        System.out.println("\n enter the file name(path):");
                        str=inp.readLine();
                        so.writeBytes(str);
                        so.writeBytes("\n");
                        FileOutputStream fos=new FileOutputStream("venkat.txt");
                        int str1;
                        System.out.println("transferring the file:"+str+"\n");
                        while((str1=si.read())!=-1)     
                        fos.write((char)str1);
                        System.out.println("\n file received successfully");
                        si.close();
            }
}

















FTP SERVER
import java.io.*;
import java.net.*;
class ftps
{
            public static void main(String args[])throws Exception
            {
            Socket s=null;
            ServerSocket ss=null;
            DataOutputStream sso=null;
            DataInputStream sin=null;
            ss=new ServerSocket(55555);
            System.out.println("waiting");
            s=ss.accept();
            sso=new DataOutputStream(s.getOutputStream());
            sin=new DataInputStream(s.getInputStream());
            String s1;
            s1=sin.readLine();
            FileInputStream fos=new FileInputStream(s1);
            int str;
            while((str=fos.read())!=-1)
            sso.writeBytes(""+(char)str);
            System.out.println("file has been sent successfully");
            sso.close();
            s.close();
            }
}



















SMTP
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.net.*;
import java.io.*;
import javax.swing.*;
public class MailClient
{
public static void main(String arg[])
{
JFrame frame=new MailClientFrame();
frame.show();
}
}
class MailClientFrame extends JFrame implements ActionListener
{
private BufferedReader in;
private PrintWriter out;
private JTextField from;
private JTextField to;
private JTextField smtpServer;
private JTextArea message;
private JTextArea response;
private  JLabel fromLb1;
private  JLabel toLb1;
private  JLabel serverLb1;
public MailClientFrame()
{
setTitle("Mail Client");
setSize(250,400);
addWindowListener(new WindowAdapter()
{
public void WindowClosing(WindowEvent e)
{
System.exit(0);
}
}
);
getContentPane().setLayout(new FlowLayout(FlowLayout.LEFT));
fromLb1=new JLabel("From:");
getContentPane().add(fromLb1);
from=new JTextField(20);
getContentPane().add(from);
toLb1=new JLabel ("to:");
getContentPane().add(toLb1);
to=new JTextField (20);
getContentPane().add(to);
serverLb1=new JLabel("SMTPServer:");
getContentPane().add(serverLb1);
smtpServer=new JTextField(20);
getContentPane().add(smtpServer);
message=new JTextArea(5,20);
getContentPane().add(message);
JScrollPane p=new JScrollPane(message);
getContentPane().add(p);
response=new JTextArea(5,20);
getContentPane().add(response);
JScrollPane p1=new JScrollPane(response);
getContentPane().add(p1);
JButton sendButton=new JButton("send");
sendButton.addActionListener(this);
JPanel buttonPanel=new JPanel();
buttonPanel.add(sendButton);
getContentPane().add(sendButton);
}
public void actionPerformed(ActionEvent evt)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
sendMail();
}
}
);
}
public void sendMail()
{
try
{
Socket s=new Socket(smtpServer.getText(),25);
out=new PrintWriter(s.getOutputStream());
String hostName=InetAddress.getLocalHost().getHostName();
send(null);
send("HELLO:"+hostName);
send("MAIL FROM:"+from.getText());
send("RCPT TO:"+to.getText());
send("DATA");
out.println(message.getText());
send(",");
s.close();
}
catch(IOException e)
{
response.append("error:"+e);
}
}
public void send(String s)throws IOException
{
if(s!=null)
{
response.append(s+"");
out.println(s);
out.flush();
}
String line;
if((line=in.readLine())!=null)
{
response.append(line+"");
}
}
}




















SOCKET SERVER
import java.io.*;
import java.net.*;
import java.lang.String;
public class serverecho
{
public static void main(String args[])throws Exception
{
            ServerSocket ss=new ServerSocket(1234);
            Socket s=ss.accept();
            DataInputStream in=new DataInputStream(s.getInputStream());
            DataOutputStream out=new DataOutputStream(s.getOutputStream());
            String str;
            System.out.println("\n server side");
            while(true)
            {
                        str=in.readLine();
                        out.writeBytes(str+"\n");
                        System.out.println("msg from client");
                        System.out.println(str+"\n");
            }
}
}





















SOCKET CLIENT
import java.io.*;
import java.net.*;
import java.lang.String;
public class clientecho1
{
public static void main(String args[])throws Exception
{
DataInputStream in=new DataInputStream(System.in);
Socket s=new Socket("localhost",1234);
DataInputStream inecho=new
DataInputStream(s.getInputStream());
DataOutputStream out=new
DataOutputStream(s.getOutputStream());
String str;
System.out.println("client side");
System.out.println("\n enter the client message:");
while((str=in.readLine())!=null)
{
out.writeBytes(str+"\n");
if(str.equals("exit"))
{
out.writeBytes("\n client terminated");
}
else
{
System.out.println("echo from server:");
System.out.println(inecho.readLine());
System.out.println("\n enter client message:");
}
}
}
}











TCP SERVER
import java.io.*;
import java.net.*;
import java.util.*;
class tcpserver
{
            public static void main(String args[])throws IOException
            {
                        ServerSocket ss=new ServerSocket(9000);
                        while(true)
                        {
                        Socket Client=ss.accept();
                        OutputStream out=Client.getOutputStream();
                        PrintWriter pw=new PrintWriter(out,true);
                        String x="hai";
                        pw.println(x);
                        }
            }
}

TCP CLIENT
import java.io.*;
import java.util.*;
import java.net.*;
class tcpclient
{
            public static void main(String args[])throws IOException,IOException
            {
                        Socket s=new Socket(InetAddress.getLocalHost(),9000);
                        InputStream in=s.getInputStream();
                        BufferedReader br=new BufferedReader(new InputStreamReader(in));
                        String x=null;
                        while((x=br.readLine())!=null)
                        {
                                    System.out.println(""+x);
                        }
            }
}





UDP SERVER
import java.io.*;
import java.net.*;
class udpserver
{
public static void main(String args[])throws Exception
{
DatagramSocket ds=new DatagramSocket(700);
byte data[]=new byte[20];
String str;
while(true)
{
DatagramPacket dp=new DatagramPacket(new byte[255],255);
ds.receive(dp);
data=dp.getData();
int n=(int)data[0];
if(n==0)
break;
else
n=n*n;
System.out.println(Integer.toString(n));
}
}
}




















UDP CLIENT
import java.io.*;
import java.net.*;
class udpclient
{
            public static void main(String args[])throws Exception
            {
                        DatagramSocket ds=new DatagramSocket(55555);
                        DataInputStream in=new DataInputStream(System.in);
                        byte a[]=new byte[20];
                        String str;
                        while(true)
                        {
                        str=in.readLine();
                        int n=Integer.parseInt(str);
                        a[0]=(byte)n;
                        DatagramPacket dp=new DatagramPacket              
(a,a.length,InetAddress.getLocalHost(),700);
                        ds.send(dp);
                        if(str.equals("0"))
                        break;
                        }
            }
}





















CONGESTION CONTROL 1
import java.io.*;
class sample18
{
int len;
public static void main(String arg[])throws IOException
{
            sample18 Svar=new sample18();
            int a[]=new int[50];
            int b[]=new int[50];
            int n,wsz,i,j,k,t,v,ch,sdch,rvch;
            DataInputStream dis=new DataInputStream(System.in);
            String st;
            j=t=v=wsz=0;
            while(true)
            {
                        System.out.println("\n\t\t\t SLIDING WINDOW PROTOCOL");
                        System.out.println("\t\t\t*******");
                        System.out.println("\t\t1.sender");
                        System.out.println("\t\t2.receiver");
                        System.out.println("\t\t3.exit");
                        System.out.println("\t\t enter your choice:");
                        st = dis.readLine();
                        ch=Integer.parseInt(st);
                        switch(ch)
                        {
                        case 1:
                        bk1:while(true)
                        {
                        System.out.println("\n\t\t Data Senders Menu");
                        System.out.println("\t\t-----");
                        System.out.println("1.Store");
                        System.out.println("\t\t2.Window Size Set");
                        System.out.println("t\t3.Transmit");
                        System.out.println("\t\t4.Current window items");
                        System.out.println("\t\t5.Exit");
                        System.out.println("\t\t enter your choice:");
                        st=dis.readLine();
                        sdch=Integer.parseInt(st);
                        switch(sdch)
                                    {
                                    case 1:
                                    System.out.println("\n\t\t How many data want to Store\n:");
                                    st=dis.readLine();
                                    n=Integer.parseInt(st);
                                    for(i=0;i
                                    {
                                    System.out.println("\n\t\t enter the data :");
                                    st=dis.readLine();
                                    a[i]=Integer.parseInt(st);
                                    }
                                    break;
                                    case 2:
                                    System.out.println("\n\t\t enter the window size:");
                                    st=dis.readLine();
                                    wsz=Integer.parseInt(st);
                                    break;
                                    case 3:
                                    System.out.println("\n\t\t how many data want to transmit:");
                                    st=dis.readLine();
                                    Svar.len=Integer.parseInt(st);
                                    for(i=0;i
                                    System.out.println(b[i]);
                                    break;
                                    case 4:
                                    System.out.println("\n\t\t the data inside transmitter window");
                                    t=0;
                                    wsz=wsz+j;
                                    v=v+j;
                                    for(i=v;i
                                    b[t++]=a[i];
                                    for(i=0;i
                                    System.out.println(b[i]);
                                    break;
                                    case 5:
                                    break bk1;
                                    }
                        }
                        break;
            case 2:
            bk2:while(true)
            {
            System.out.println("\n\t\t Data receiver menu");
            System.out.println("\t\t----");
            System.out.println("\t\t1.Receive");
            System.out.println("\t\t2.Acknowledge");
            System.out.println("\t\t3.Exit");
            System.out.println("\t\t Enter your choice:");
            st=dis.readLine();
            rvch=Integer.parseInt(st);
            switch(rvch)
                        {
                        case 1:
                        System.out.println("\n\t\t the number of data received");
                        for(i=0;i
                        System.out.println(b[i]);
                        break;
                        case 2:
                        System.out.println("\n\t\t enter the number of data ackonwledge:");
                        st=dis.readLine();
                        j=Integer.parseInt(st);
                        break;
                        case 3:
                        break bk2;
                        }
            }
            break;
            case 3:
            System.exit(1);
            }
            }
}
}















CONGESTION CONTROL 2
#include
#include
int send(int);
int rece(int);
int process(int);
int tr[50];
int i,n,ch,ans,m=0;
int main(void)
{
clrscr();
printf ("\n\n\t\t implementation of sliding window protocol");
printf("\n\n enter the number of bit to be trnsamitted:");
scanf ("%d",&n);
printf("\n\nenter the bit one by one:");
for(i=1;i<=n;i++)
scanf ("%d",&tr[i]);
printf("\n\ntransmitted bits:");
for(i=1;i<=7;i++)
printf("%d",tr[i]);
do
{printf ("\n\n\t\t1.sending  sliding window");
printf ("\n\n\t\t2.receiving sliding window");
printf ("\n\n\t\t 3.exit");
printf ("\n\nenter your choice");
scanf ("%d",&ch);
switch(ch)
{
case 1:
send(m);
break;
case 2:
rece(m);
break;
case 3:
break;
default:
printf("\n\n enter choice is wrong");
break;
}
printf("\n\n do you want to continue(yes=1/no=0):");
scanf("%d",&ans);
}
while(ans!=0);
return 0;
}
int send(int s)
{int pos=0;
s++;
printf("\n\n enter the bits position to be transmitted:");
scanf("%d",&pos);
printf("\n\n after bit transmitted in the sender:");
process(pos);
return;
}
int  rece(int s)
{
int pos;
char a[10];
s++;
printf("\n\n received bit position in the receiver:");
scanf("%d",&pos);
printf("\n\n after bit received,the remaining bits are:");
process(pos);
printf("\n\n would u like to send ack(y/n):");
scanf("%s",a);
if(strcmp(a,"y")==0)
{
acknow(pos);
}
return 0;
}
int process(int x)
{
int i;
for(i=(x+1);i<=7;i++)
printf("%d",tr[i]);
return 0;
}
int acknow(int x)
{
int i;
printf("\n\n sender and receiver expanded after act is received and send");
for(i=(x+1);i<=7;i++)
printf("%d",tr[i]);
getch();
return 0;
}