4887 characters | 229 lines | 4.77 KB
DOWNLOAD | RAW | EMBED | CREATE NEW VERSION OF THIS PASTE | REPORT ABUSE | x
  1. //UDPCLIENT:
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import java.net.DatagramPacket;
  6. import java.net.DatagramSocket;
  7. import java.io.IOException;
  8. import java.net.SocketException;
  9.  
  10.  
  11.  
  12. class test1
  13. {
  14.         void receiving()
  15.         {
  16.                 try
  17.               {
  18.                         DatagramSocket socket=new DatagramSocket(5001);
  19.                         byte[] buff=new byte[1024];
  20.                         InetAddress address=InetAddress.getByName("localhost");
  21.                         int rec=0;
  22.                         while(true)
  23.                            {
  24.                                 rec = 0;
  25.                                 DatagramPacket packet=new DatagramPacket(buff,buff.length);
  26.                                 socket.receive(packet);
  27.                             String client_packet=new String(packet.getData(),0,packet.getLength());
  28.                                 System.out.println("Him : "+client_packet);
  29.                                 rec =1;
  30.                                 if(rec > 0)
  31.                                 {
  32.                                        
  33.                                         socket.close();
  34.                                         break;
  35.                                 }
  36.                            }
  37.         }
  38.                 catch(SocketException socket_error)
  39.                 {
  40.                         System.out.println("Socket exception has occured.");
  41.                         socket_error.printStackTrace();
  42.                 }
  43.                 catch(IOException input_error)
  44.                 {
  45.                         System.out.println("Input error has occured.");
  46.                         input_error.printStackTrace();
  47.                 }
  48.         }
  49.  
  50.         void sending()
  51.         {
  52.                 try
  53.                 {
  54.                 DatagramSocket socket=new DatagramSocket();
  55.                 byte [] buff=new byte[1024];
  56.                 InetAddress address=InetAddress.getByName("localhost");
  57.                 // System.out.println("Trying...to connect to the server.");
  58.                 while(true)
  59.         {
  60.                         System.out.print("You : ");
  61.                         BufferedReader buff_data=new BufferedReader(new InputStreamReader(System.in));
  62.                     String user_data=buff_data.readLine();
  63.                     System.out.println();
  64.                         buff=user_data.getBytes();
  65.                         DatagramPacket packet=new DatagramPacket(buff,buff.length,address,4455);
  66.                         socket.send(packet);
  67.                        
  68.                         break;
  69.             }
  70.  
  71.             }
  72.                catch(UnknownHostException unknown_host)
  73.                 {
  74.                         System.out.println("Unknown host..");
  75.                         unknown_host.printStackTrace();
  76.                 }
  77.                 catch(SocketException socket_error)
  78.             {
  79.                         System.out.println("Some socket error has occured.");
  80.                         socket_error.printStackTrace();
  81.                
  82.                 }
  83.                 catch(IOException input_error)
  84.                 {
  85.                         System.out.println("Some input error has occured.");
  86.                         input_error.printStackTrace();
  87.                 }
  88.         }
  89. }
  90. class UDPC1
  91. {
  92.        
  93.        
  94.         public static void main(String args[])
  95.         {
  96.                 int count = 0;
  97.                 test1 t = new test1();
  98.                 while(true)
  99.                 {
  100.                         if(count == 0)
  101.                         {       t.sending();
  102.                                 // System.out.println(count);
  103.                                 count=1;
  104.                         }
  105.                         if(count == 1)
  106.                         {       t.receiving();
  107.                                 // System.out.println(count);
  108.                                 count=0;
  109.                         }
  110.                        
  111.                 }       
  112.         }
  113. }
  114.  
  115. //UDPSERVER
  116. import java.net.DatagramPacket;
  117. import java.net.DatagramSocket;
  118. import java.io.IOException;
  119. import java.net.SocketException;
  120. import java.io.*;
  121. import java.net.*;
  122.  
  123.  
  124. class test2
  125. {
  126.         void receiving()
  127.         {
  128.                 try
  129.               {
  130.                         DatagramSocket socket=new DatagramSocket(4455);
  131.                         byte[] buff=new byte[1024];
  132.                         InetAddress address=InetAddress.getByName("localhost");
  133.                         int rec = 0;
  134.                         while(true)
  135.                            {   
  136.                                 rec = 0;
  137.                                 DatagramPacket packet=new DatagramPacket(buff,buff.length);
  138.                                 socket.receive(packet);
  139.                             String client_packet=new String(packet.getData(),0,packet.getLength());
  140.                                 System.out.println("Him : "+client_packet);
  141.                                 rec =1;
  142.                                 if(rec > 0)
  143.                                 {
  144.                                        
  145.                                         socket.close();
  146.                                         break;
  147.                                 }
  148.                            }
  149.         }
  150.                 catch(SocketException socket_error)
  151.                 {
  152.                         System.out.println("Socket exception has occured.");
  153.                         socket_error.printStackTrace();
  154.                 }
  155.                 catch(IOException input_error)
  156.                 {
  157.                         System.out.println("Input error has occured.");
  158.                         input_error.printStackTrace();
  159.                 }
  160.         }
  161.  
  162.         void sending()
  163.         {
  164.                 try
  165.                 {
  166.                 DatagramSocket socket=new DatagramSocket();
  167.                 byte [] buff=new byte[1024];
  168.                 InetAddress address=InetAddress.getByName("localhost");
  169.                 // System.out.println("Trying...to connect to the server.");
  170.                 while(true)
  171.         {
  172.                         System.out.print("You : ");
  173.                         BufferedReader buff_data=new BufferedReader(new InputStreamReader(System.in));
  174.                     String user_data=buff_data.readLine();
  175.                     System.out.println();
  176.                         buff=user_data.getBytes();
  177.                         DatagramPacket packet=new DatagramPacket(buff,buff.length,address,5001);
  178.                         socket.send(packet);
  179.                         break;
  180.             }
  181.  
  182.             }
  183.                catch(UnknownHostException unknown_host)
  184.                 {
  185.                         System.out.println("Unknown host..");
  186.                         unknown_host.printStackTrace();
  187.                 }
  188.                 catch(SocketException socket_error)
  189.             {
  190.                         System.out.println("Some socket error has occured.");
  191.                         socket_error.printStackTrace();
  192.                 }
  193.                 catch(IOException input_error)
  194.                 {
  195.                         System.out.println("Some input error has occured.");
  196.                         input_error.printStackTrace();
  197.                 }
  198.         }
  199. }
  200. class UDPS1
  201. {
  202.  
  203.  
  204.  
  205.         public static void main(String args[])
  206.         {
  207.                 int count = 0;
  208.                 test2 t = new test2();
  209.                 while(true)
  210.                 {
  211.                         if(count == 0)
  212.                         {       t.receiving();
  213.                                 // System.out.println(count);
  214.                                 count=1;
  215.                         }
  216.                         if(count == 1)
  217.                         {       t.sending();
  218.                                 // System.out.println(count);
  219.                                 count=0;
  220.                         }
  221.                
  222.                 }
  223.         }
  224.        
  225.  }