viewing paste Unknown #22392 | Java

Posted on the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
--- RMI ---
 
import java.net.*;
import java.rmi.*;
public class RmiServer{
    public static void main(String args[]) throws Exception{
        try{ 
            RmiServerImpl obj = new RmiServerImpl();
            Naming.rebind("s", obj);
        } 
        catch (RemoteException e){
            System.out.println("java RMI registry already exists.");
        }
    }
}
 
import java.rmi.*;
import java.rmi.server.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class RmiServerImpl extends UnicastRemoteObject implements RmiServerIntf{
 
    JLabel l1;
    JTextArea tf;
    Container c;
    public RmiServerImpl() throws RemoteException{
        display e=new display();
    }
    public class display extends JFrame{
        public display(){
            c =getContentPane();
            FlowLayout f=new FlowLayout();
            c.setLayout(f);
            l1=new JLabel("Received Messages");
            c.add(l1);
            tf=new JTextArea(15,30);
            c.add(tf);
            tf.setText("");
            setVisible(true);   
            setSize(500,500);
            setTitle("Server");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }
    }
    public void show(String str) throws RemoteException{
        tf.append("\n"+str); 
    }
}
 
import java.rmi.*;
public interface RmiServerIntf extends Remote{
    public void show(String str) throws RemoteException;
}
 
import java.rmi.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class RmiClient extends JFrame implements ActionListener{ 
    JLabel l1;
    JTextField tf;
    JButton b1;
    Container c;
    public RmiClient(){
        c =getContentPane();
        FlowLayout f=new FlowLayout();
        c.setLayout(f);
        l1=new JLabel("Message");
        c.add(l1);
        tf=new JTextField(25);
        c.add(tf);
        b1=new JButton("Send");
        c.add(b1);
        b1.addActionListener(this);
        setVisible(true);
        setSize(500,500);
        setTitle("Client");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    public void actionPerformed(ActionEvent ee){
        try{
            String url="rmi://localhost/s";
            RmiServerIntf obj = (RmiServerIntf)Naming.lookup(url);
            String str=tf.getText();
            obj.show(str);
        }
        catch(Exception e){}
    }
    public static void main(String args[]) throws Exception{
        new RmiClient();
    }
}
 
-- TCP --
 
client
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class client2d extends JFrame implements ActionListener{
    BufferedReader br;
    PrintWriter pw;
    String st,st2;
    JLabel l1;
    JTextField tf;
    JTextArea ta;
    JButton b1;
    Container c;
    Socket skt;
    public client2d(){
        try{
            skt=new Socket("localhost",8000);
            c =getContentPane();
            FlowLayout f=new FlowLayout();
            c.setLayout(f);
            l1=new JLabel("Message");
            c.add(l1);
            tf=new JTextField(25);
            c.add(tf);
            ta=new JTextArea(15,15);
            c.add(ta);
            b1=new JButton("Send");
            c.add(b1);
            b1.addActionListener(this);
            setVisible(true);
            setSize(500,275);
            setTitle("Client");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            while(true){
                br=new BufferedReader(new InputStreamReader(skt.getInputStream()));
                st2=br.readLine();
                ta.append("\n"+st2);
            }
        }
        catch(Exception e){}
    }
    public void actionPerformed(ActionEvent ee){
        try{
            st=tf.getText();
            pw =  new PrintWriter(skt.getOutputStream(), true);
            pw.println(st);
        }
        catch(Exception e){}
    }
    public static void main(String args[]){
        new client2d();
    }
}
 
server
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class server2d extends JFrame implements ActionListener{
    BufferedReader br;
    PrintWriter pw;
    String st,st2;
    JLabel l1;
    JTextField tf;
    JTextArea ta;
    JButton b1;
    Container c;
    ServerSocket ss;
    Socket skt;
    public server2d(){
        try{    
            ss=new ServerSocket(8000);
            skt = ss.accept();
            c =getContentPane();
            FlowLayout f=new FlowLayout();
            c.setLayout(f);
            l1=new JLabel("Message");
            c.add(l1);
            tf=new JTextField(25);
            c.add(tf);
            ta=new JTextArea(15,15);
            c.add(ta);
            b1=new JButton("Send");
            c.add(b1);
            b1.addActionListener(this);
            setVisible(true);
            setSize(500,275);
            setTitle("Server");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            while(true){
                br=new BufferedReader(new InputStreamReader(skt.getInputStream()));
                st2=br.readLine();
                ta.append("\n"+st2);
            }
        }
        catch(Exception e){}
    }
    public void actionPerformed(ActionEvent ee){
        try{
            st=tf.getText();
            pw =  new PrintWriter(skt.getOutputStream(), true);
            pw.println(st);
        }
        catch(Exception e){}
    }
    public static void main(String args[]){
        new server2d();
    }
}
 
 
-- UDP --
 
server
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class udpserver extends JFrame implements ActionListener{
    BufferedReader br;
    PrintWriter pw;
    String st,st2;
    JLabel l1;
    JTextField tf;
    JTextArea ta;
    JButton b1;
    Container c;
    DatagramSocket ss,cc;
    DatagramPacket incoming,sending;
    public udpserver() {
        try  {
            ss=new DatagramSocket(8000);
            cc=new DatagramSocket();
            byte [] buffer=new byte[65535];
            incoming=new DatagramPacket(buffer,buffer.length);
            c =getContentPane();
            FlowLayout f=new FlowLayout();
            c.setLayout(f);
            l1=new JLabel("Message");
            c.add(l1);
            tf=new JTextField(25);
            c.add(tf);
            ta=new JTextArea(15,15);
            c.add(ta);
            b1=new JButton("Send");
            c.add(b1);
            b1.addActionListener(this);
            setVisible(true);
            setSize(500,275);
            setTitle("Server");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            while(true)  {
                ss.receive(incoming);
                byte[] data=incoming.getData();
                st2=new String(data,0,incoming.getLength());
                System.out.println(st2);
                ta.append("\nClient:"+st2);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
            }
        }
        catch(Exception e)
        {}
    }
    public void actionPerformed(ActionEvent ee) {
        try{
 
            st=tf.getText();
            byte [] b=st.getBytes();
            sending=new DatagramPacket(b,b.length,InetAddress.getLocalHost(),8001);
            ta.append("\nServer:"+st);
            cc.send(sending);
            tf.setText("");
        }
        catch(Exception e) {}
    }
    public static void main(String args[]){
        new udpserver();
    }
}
 
client
import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
public class udpclient extends JFrame implements ActionListener{
    BufferedReader br;
    PrintWriter pw;
    String st,st2;
    JLabel l1;
    JTextField tf;
    JTextArea ta;
    JButton b1;
    Container c;
    DatagramSocket ss,cc;
    DatagramPacket incoming,sending;
    byte [] buffer;
    public udpclient()  {
        try {   
            ss=new DatagramSocket(8001);
            cc=new DatagramSocket();
            byte[] buffer=new byte[65535];  
            c =getContentPane();
            FlowLayout f=new FlowLayout();
            c.setLayout(f);
            l1=new JLabel("Message");
            c.add(l1);
            tf=new JTextField(25);
            c.add(tf);
            ta=new JTextArea(15,15);
            c.add(ta);
            b1=new JButton("Send");
            c.add(b1);
            b1.addActionListener(this);
            setVisible(true);
            setSize(500,275);
            setTitle("Client");
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            while(true){
                incoming=new DatagramPacket(buffer,buffer.length);
                ss.receive(incoming);
                byte[] data=incoming.getData();
                st2=new String(data,0,incoming.getLength());
                ta.append("\nServer: "+st2);
                System.out.println(st2);
                setDefaultCloseOperation(EXIT_ON_CLOSE);
            }
        }
        catch(Exception e)  {}
    }
    public void actionPerformed(ActionEvent ee){
        try{    InetAddress in=InetAddress.getByName("localhost");
            st=tf.getText();
            sending=new DatagramPacket(st.getBytes(),st.length(),InetAddress.getLocalHost(),8000);
            ta.append("\nClient:"+st);
            tf.setText("");
            cc.send(sending);       
        }
        catch(Exception e){}
    }
 
    public static void main(String args[]){
        new udpclient();
    }
}
 
-- MultiCast --
 
import java.net.*;
import java.io.*;
class Multicastsender implements Runnable{  
    static int port;
    static MulticastSocket ms;
    static String group;
    Multicastsender() throws Exception{
        port=6789;
        group="224.2.2.9";
        ms=new MulticastSocket(port);
    }
    public void run(){  
        while(true) {
            try{
                BufferedReader br=new BufferedReader(new InputStreamReader (System.in));    
                byte sbuf[]=new byte[300];
                sbuf=br.readLine().getBytes();
                DatagramPacket spack=new DatagramPacket(sbuf,sbuf.length,InetAddress.getByName(group),port);
                ms.send(spack);
            }
            catch(Exception e){}
        }
    }
    public static void main(String args[])throws Exception{ 
        Multicastsender sender=new Multicastsender();
        Thread t= new Thread(sender);
        ms.joinGroup(InetAddress.getByName(group));
        t.start();
        int count=0;
        byte rbuf[]=new byte[30];
        System.out.println("MULTICAST SENDER");
        while(true){    
            DatagramPacket rpacket;
            rpacket=new DatagramPacket(rbuf,rbuf.length);
            ms.receive(rpacket);
            String str=new String(rpacket.getData(),0,rpacket.getLength());
            if(str.equals("join")){
                count++;
                System.out.println("Client Joined the group");
                System.out.println("currently "+count+"clients in the group");
            }
            else if(str.equals("leave")){
                count--;
                System.out.println("Client left the group");
                System.out.println("currently "+count+"clients in the group");
            }
            if(count==0){
                ms.leaveGroup(InetAddress.getByName(group));
                break;                  
            }
        }
    }
}
reciever
import java.net.*;
import java.io.*;
class Multicastreciever implements Runnable{    
    static int port;
    static MulticastSocket ms;
    static String group;
    public Multicastreciever() throws Exception{
        port=6789;
        ms=new MulticastSocket(port);
    }
    public void run(){
        while(true){    
            try{
                byte rbuf[]=new byte[300];
                DatagramPacket rpack=new DatagramPacket(rbuf,rbuf.length);
                ms.receive(rpack);
                String str=new String(rpack.getData(),0,rpack.getLength());
                if(str.compareTo("join")!=0 && str.compareTo("leave")!=0)
                System.out.println(str);
            }
            catch(Exception e){}
        }
    }
    public static void main(String args[]) throws Exception {       
        Multicastreciever mr=new Multicastreciever();
        Thread t= new Thread(mr);
        String group="224.2.2.9";
        System.out.println("Do you wish to join");  
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        if(br.readLine().equals("yes")) {
            ms.joinGroup(InetAddress.getByName(group));
            byte sbuf[]=new byte[30];
            sbuf="join".getBytes();
            DatagramPacket spack=new DatagramPacket(sbuf,sbuf.length,InetAddress.getByName(group),port);
            ms.send(spack);
            System.out.println("client joined the group");
            t.start();
            while(true){    
                String str1=br.readLine();
                if(str1.equals("leave")){
                    byte sbuf1[]=new byte[30];
                    sbuf1=str1.getBytes();
                    DatagramPacket spack1=new DatagramPacket(sbuf1,sbuf1.length, InetAddress.getByName(group),port);
                    ms.send(spack1);
                    System.out.println("client left the group");
                    ms.leaveGroup(InetAddress.getByName(group));
                    break;
                }
            }
        }
    }
}
 
 
-- SLIDING WINDOW --
sender
import java.io.*; 
import java.net.*; 
class SlidingWindowSender{      
    public static void main(String argv[]) throws Exception{       
        String msg1;          
        String msg2;
        String buf[]=new String[10];
        int n,sws=3,nf=0,i,b;
        System.out.println("SLIDING WINDOW SENDER");
        Thread.sleep(1000);
        System.out.println("WAITING FOR RECIEVER TO START...");          
        ServerSocket ss = new ServerSocket(8000);          
        Socket s = ss.accept(); 
        System.out.println("RECIEVER STARTED."); 
        do{   
            DataOutputStream op = new DataOutputStream(s.getOutputStream());   
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            BufferedReader br1 = new BufferedReader(new InputStreamReader (s.getInputStream()));
            System.out.println("ENTER THE NUMBER OF FRAMES TO SEND");
            nf=Integer.parseInt(br.readLine());
            if(nf>sws){
                n=sws;
                System.out.println("Enter frames:");
                op.writeBytes(""+n + '\n');
            }
            else{
                n=nf;
                System.out.println("Enter frames:");
                op.writeBytes(""+n + '\n');
            }
            //System.out.println("Enter frames:");
            //op.writeBytes(""+nf + '\n');
            for(i=0;i<n;i++){   
                buf[i]=br.readLine();
                op.writeBytes(buf[i] + '\n');   
            }
            if(n<nf){
                System.out.println("window size exceeds");  
            }
            sws=sws-nf;
            System.out.println("Remaining Window Size:"+sws);
            String ack=br1.readLine();
            if(!ack.equals("")){    
                System.out.println("Acknowledgement Recieved:ACK"+ack);
                sws=sws+nf;
            }
            else{   
                System.out.println("Resending frames");
                for(i=0;i<nf;i++){  
                    buf[i]=br.readLine();
                    op.writeBytes(buf[i] + '\n');   
                }
            }
            System.out.println("Window size changed");      
            System.out.println("Remaining Window Size:"+sws);
            System.out.println("Continue.. press 1 or press 0");
            b=Integer.parseInt(br.readLine());      
        } while(b==1);   
    }
} 
 
receiver
 
import java.io.*;
import java.net.*; 
class SlidingWindowReciever{    
    public static void main(String argv[]) throws Exception{    
        String msg1;   
        String msg2="";
        String buf[]=new String[10];
        int nf=0,i;
        Socket s = new Socket("localhost", 8000);
        System.out.println("SLIDING WINDOW RECIEVER");
        while(true) {    
            DataOutputStream op = new DataOutputStream(s.getOutputStream());   
            BufferedReader br = new BufferedReader(new    InputStreamReader (s.getInputStream()));
            nf = Integer.parseInt(br.readLine()); 
            System.out.println(nf+" Messages Waiting");  
            //op.writeBytes(msg1 + '\n');   
            for(i=0;i<nf;i++){
                msg1 = br.readLine();
                System.out.println("Message From Sender: " +msg1);
            }
            int ack=nf;
            op.writeBytes(""+ack + '\n');
            System.out.println("Acknowledgement for "+nf+" messages sent");
            //Thread.sleep(1000); 
        }
    }
}
 
-- Applet --
 
import java.applet.*;
import java.awt.*;
public class app extends Applet{
    String s;
    Image picture;  
    public void init() {
        s="WELCOME";
        picture = getImage(getDocumentBase(),"jerry.png"); 
    }
    public void paint(Graphics g) {
        Font f = new Font("TimesRoman", Font.PLAIN, 50);
        g.setFont(f);
        g.setColor(Color.RED);
        g.drawString(s,400,150);
        g.drawLine(20,20,20,250);
        g.setColor(Color.ORANGE);
        g.fillRect(20,20,200,30);
        g.setColor(Color.WHITE);
        g.fillRect(20,50,200,30); 
        g.setColor(Color.GREEN);
        g.fillRect(20,80,200,30);
        g.setColor(Color.BLUE);
        g.fillOval(110,50,30,30);
        g.drawImage(picture, 200,200, this);
    }
}
 
 
 
-- JDBC --
 
import java.io.*;
import java.sql.*;
import java.lang.*;
 
public class jdbc1{
    public static void main(String args[]) throws Exception{
        DataInputStream ds=new DataInputStream(System.in);
        System.out.println("Enter the  rollno.");
        int rollno=Integer.parseInt(ds.readLine());
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection con=DriverManager.getConnection("jdbc:odbc:ee","scott","tiger");
        Statement stmt=con.createStatement();
        ResultSet rs=stmt.executeQuery("Select * from student where ROLLNO= " +rollno);
        while(rs.next()){
            System.out.println("Rollno:"+rs.getString(1));
            System.out.println("Name:"+rs.getString(2));
            System.out.println("Marks:"+rs.getString(3));
        }
    }
}
 
 
Viewed 824 times, submitted by Guest.