jbn6972

Socket_programming(UDP)

Mar 1st, 2022 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. UDP
  2. ------------------Client-------------------------
  3. import java.io.*;
  4. import java.net.*;
  5. public class client{
  6.     public static void main(String[] args) throws IOException{
  7.         DatagramSocket client = new DatagramSocket();
  8.         InetAddress add = InetAddress.getByName("localhost");
  9.         String msg = "Hello Server";
  10.         byte[] buf = msg.getBytes();
  11.         DatagramPacket packet = new DatagramPacket(buf, buf.length,add,1234);
  12.         client.send(packet);
  13.         client.close();
  14.  
  15.     }
  16. }
  17. ---------------------Server-------------------------
  18. import java.io.*;
  19. import java.net.*;
  20. public class server {
  21.     public static void main(String[] args) throws IOException{
  22.         DatagramSocket server = new DatagramSocket(1234);
  23.         byte[] buf = new byte[256];
  24.         DatagramPacket packet = new DatagramPacket(buf,buf.length);
  25.         server.receive(packet);
  26.         String response = new String(packet.getData());
  27.         System.out.println("Response Data :: "+response);
  28.         server.close();
  29.     }
  30.  
  31. }
  32. ---------------------------------------------------------------------------
  33. TCP
  34. ------------------Client-------------------------
  35. import java.io.*;
  36. import java.net.*;
  37. import java.util.Scanner;
  38. public class client{
  39.     public static void main(String[] args) {
  40.         try{
  41.             Scanner sc = new Scanner(System.in);
  42.             Socket s=new Socket("localhost",6666);
  43.             DataOutputStream dout=new DataOutputStream(s.getOutputStream());
  44.             System.out.println("Establishing Connection...");
  45.             System.out.println("Connection Established");
  46.             System.out.println("Enter the message to be sent:");
  47.             String str = sc.nextLine();
  48.             dout.writeUTF(str);
  49.             dout.flush();
  50.             dout.close();
  51.             System.out.println("Connection Closed");
  52.             s.close();
  53.             sc.close();
  54.         }catch(Exception e){System.out.println(e);}
  55.     }
  56. }
  57. -----------------------------Server-------------------
  58. import java.io.*;
  59. import java.net.*;
  60. public class server {
  61.     public static void main(String[] args) throws Exception {
  62.         try{
  63.             ServerSocket ss=new ServerSocket(6666);
  64.             Socket s=ss.accept();//establishes connection
  65.             DataInputStream dis=new DataInputStream(s.getInputStream());
  66.             String  str=(String)dis.readUTF();
  67.             System.out.println("message: "+str);
  68.             System.out.println("Connection Closed");
  69.             ss.close();
  70.         }catch(Exception e){System.out.println(e);}
  71.     }
  72. }
  73.  
Add Comment
Please, Sign In to add comment