diff --git a/1. URL/URLClass.java b/1. URL/URLClass.java new file mode 100644 index 0000000..e6a454c --- /dev/null +++ b/1. URL/URLClass.java @@ -0,0 +1,17 @@ +import java.net.URL; + +public class URLClass { + + public static void main(String args[]) { + try { + URL url = new URL( + "https://www.google.co.in/search?sxsrf=ACYBGNTlCg6stXRWj6XfKxNBgxecqLRJiQ%3A1572264736726&source=hp&ei=INu2XfvqKYPzvAT4ho_oAQ&q=go&oq=go&gs_l=psy-ab.3..35i39l2j0i131l3j0l5.19302.19387..19663...0.0..0.125.239.0j2......0....1..gws-wiz.9sac5rg-3P0&ved=0ahUKEwj7-v3I9r7lAhWDOY8KHXjDAx0Q4dUDCAY&uact=5"); + System.out.println(url.getProtocol()); + System.out.println(url.getHost()); + System.out.println(url.getPort()); + System.out.println(url.getFile()); + } catch (Exception e) { + System.out.println(e); + } + } +} diff --git a/1. URL/URLclass.java b/1. URL/URLclass.java deleted file mode 100644 index a93b1de..0000000 --- a/1. URL/URLclass.java +++ /dev/null @@ -1,19 +0,0 @@ -import java.io.*; -import java.net.*; - -class URLclass{ - public static void main(String args[]) - { - try{ - URL url=new URL("https://www.google.co.in/search?sxsrf=ACYBGNTlCg6stXRWj6XfKxNBgxecqLRJiQ%3A1572264736726&source=hp&ei=INu2XfvqKYPzvAT4ho_oAQ&q=go&oq=go&gs_l=psy-ab.3..35i39l2j0i131l3j0l5.19302.19387..19663...0.0..0.125.239.0j2......0....1..gws-wiz.9sac5rg-3P0&ved=0ahUKEwj7-v3I9r7lAhWDOY8KHXjDAx0Q4dUDCAY&uact=5"); - System.out.println(url.getProtocol()); - System.out.println(url.getHost()); - System.out.println(url.getPort()); - System.out.println(url.getFile()); - } - catch(Exception e) - { - System.out.println(e); - } - } -} diff --git a/10. Two Way Communication using TCP/TcpClient.java b/10. Two Way Communication using TCP/TcpClient.java deleted file mode 100644 index 91af9f1..0000000 --- a/10. Two Way Communication using TCP/TcpClient.java +++ /dev/null @@ -1,29 +0,0 @@ -import java.net.*; -import java.io.*; -import java.util.*; -public class TcpClient{ - public static void main(String[] args) throws Exception - { - Scanner scan=new Scanner(System.in); - Socket s=new Socket("localhost",5656); - DataOutputStream dout=new DataOutputStream(s.getOutputStream()); - DataInputStream dis=new DataInputStream(s.getInputStream()); - while(true) - { - System.out.println("Write Your message"); - String str=scan.nextLine(); - dout.writeUTF(str); - dout.flush(); - if(str.equals("bye")) - { - dout.close(); - s.close(); - break; - } - String str1=(String)dis.readUTF(); - System.out.println("Server Messaged--->"+str1); - - - } - } -} \ No newline at end of file diff --git a/10. Two Way Communication using TCP/TcpServer.java b/10. Two Way Communication using TCP/TcpServer.java deleted file mode 100644 index a9a47d1..0000000 --- a/10. Two Way Communication using TCP/TcpServer.java +++ /dev/null @@ -1,32 +0,0 @@ -import java.net.*; -import java.io.*; -import java.util.*; -class TcpServer{ - public static void main(String[] args) throws Exception - { - int i=0; - ServerSocket ss=new ServerSocket(5656); - Socket s=ss.accept(); - Scanner scan=new Scanner(System.in); - DataOutputStream dout=new DataOutputStream(s.getOutputStream()); - DataInputStream dis=new DataInputStream(s.getInputStream()); - while(true) - { - String str=dis.readUTF(); - System.out.println("Client--->"+str); - if(str.equals("bye")) - { - System.out.println("Client Messaged .... BYE..Exiting"); - dis.close(); - s.close(); - ss.close(); - break; - } - System.out.println("Enter your message:"); - String str1=scan.nextLine(); - dout.writeUTF(str1); - dout.flush(); - } - - } -} \ No newline at end of file diff --git a/10. Two Way Communication using TCP/TwoWayTcpClient.java b/10. Two Way Communication using TCP/TwoWayTcpClient.java new file mode 100644 index 0000000..e440811 --- /dev/null +++ b/10. Two Way Communication using TCP/TwoWayTcpClient.java @@ -0,0 +1,29 @@ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.Socket; +import java.util.Scanner; + +public class TwoWayTcpClient { + + public static void main(String[] args) throws Exception { + Scanner scan = new Scanner(System.in); + Socket s = new Socket("localhost", 5656); + DataOutputStream dout = new DataOutputStream(s.getOutputStream()); + DataInputStream dis = new DataInputStream(s.getInputStream()); + while (true) { + System.out.println("Write Your message"); + String str = scan.nextLine(); + dout.writeUTF(str); + dout.flush(); + if (str.equals("bye")) { + dout.close(); + s.close(); + break; + } + String str1 = (String) dis.readUTF(); + System.out.println("Server Messaged--->" + str1); + + + } + } +} \ No newline at end of file diff --git a/10. Two Way Communication using TCP/TwoWayTcpServer.java b/10. Two Way Communication using TCP/TwoWayTcpServer.java new file mode 100644 index 0000000..a1534f5 --- /dev/null +++ b/10. Two Way Communication using TCP/TwoWayTcpServer.java @@ -0,0 +1,32 @@ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Scanner; + +public class TwoWayTcpServer { + + public static void main(String[] args) throws Exception { + ServerSocket ss = new ServerSocket(5656); + Socket s = ss.accept(); + Scanner scan = new Scanner(System.in); + DataOutputStream dout = new DataOutputStream(s.getOutputStream()); + DataInputStream dis = new DataInputStream(s.getInputStream()); + while (true) { + String str = dis.readUTF(); + System.out.println("Client--->" + str); + if (str.equals("bye")) { + System.out.println("Client Messaged .... BYE..Exiting"); + dis.close(); + s.close(); + ss.close(); + break; + } + System.out.println("Enter your message:"); + String str1 = scan.nextLine(); + dout.writeUTF(str1); + dout.flush(); + } + + } +} \ No newline at end of file diff --git a/11. Two Way Communication Local/MyClient.java b/11. Two Way Communication Local/MyClient.java index 5511ed0..7819d91 100644 --- a/11. Two Way Communication Local/MyClient.java +++ b/11. Two Way Communication Local/MyClient.java @@ -1,27 +1,28 @@ -import java.net.*; -import java.io.*; -import java.util.*; -public class MyClient{ - public static void main(String[] args)throws Exception{ - Socket s=new Socket("192.168.43.153",6666); - DataOutputStream dout=new DataOutputStream(s.getOutputStream()); - DataInputStream din=new DataInputStream(s.getInputStream()); - Scanner scan=new Scanner(System.in); - while(true) - { - System.out.println("Enter Your message."); - String str=scan.nextLine(); - dout.writeUTF(str); - dout.flush(); - if(str.equals("bye")) - { - dout.close(); - s.close(); - break; - } - String str1=(String)din.readUTF(); - System.out.println("Server--->"+str1); - - } - } +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.Socket; +import java.util.Scanner; + +public class MyClient { + + public static void main(String[] args) throws Exception { + Socket s = new Socket("192.168.43.153", 6666); + DataOutputStream dout = new DataOutputStream(s.getOutputStream()); + DataInputStream din = new DataInputStream(s.getInputStream()); + Scanner scan = new Scanner(System.in); + while (true) { + System.out.println("Enter Your message."); + String str = scan.nextLine(); + dout.writeUTF(str); + dout.flush(); + if (str.equals("bye")) { + dout.close(); + s.close(); + break; + } + String str1 = (String) din.readUTF(); + System.out.println("Server--->" + str1); + + } + } } \ No newline at end of file diff --git a/11. Two Way Communication Local/MyServer.java b/11. Two Way Communication Local/MyServer.java index 1c96a26..e0a7b7a 100644 --- a/11. Two Way Communication Local/MyServer.java +++ b/11. Two Way Communication Local/MyServer.java @@ -1,31 +1,32 @@ -import java.net.*; -import java.io.*; -import java.util.*; -public class MyServer{ - public static void main(String[] args) throws Exception - { - ServerSocket ss=new ServerSocket(6666); - Socket s=ss.accept(); - DataInputStream dis=new DataInputStream(s.getInputStream()); - DataOutputStream dout=new DataOutputStream(s.getOutputStream()); - Scanner scan=new Scanner(System.in); - while(true) - { - String str=(String)dis.readUTF(); - System.out.println("Client--->"+str); - if(str.equals("bye")) - { - System.out.println("Client Said Bye exiting"); - dis.close(); - dout.close(); - s.close(); - ss.close(); - break; - } - System.out.println("Enter Your Message"); - String str1=scan.nextLine(); - dout.writeUTF(str1); - dout.flush(); - } - } +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Scanner; + +public class MyServer { + + public static void main(String[] args) throws Exception { + ServerSocket ss = new ServerSocket(6666); + Socket s = ss.accept(); + DataInputStream dis = new DataInputStream(s.getInputStream()); + DataOutputStream dout = new DataOutputStream(s.getOutputStream()); + Scanner scan = new Scanner(System.in); + while (true) { + String str = dis.readUTF(); + System.out.println("Client--->" + str); + if (str.equals("bye")) { + System.out.println("Client Said Bye exiting"); + dis.close(); + dout.close(); + s.close(); + ss.close(); + break; + } + System.out.println("Enter Your Message"); + String str1 = scan.nextLine(); + dout.writeUTF(str1); + dout.flush(); + } + } } \ No newline at end of file diff --git a/12. Multithreaded Server/Client.java b/12. Multithreaded Server/Client.java index c02a39c..55a4d9a 100644 --- a/12. Multithreaded Server/Client.java +++ b/12. Multithreaded Server/Client.java @@ -1,29 +1,29 @@ -import java.io.*; -import java.net.*; -import java.util.*; -public class Client{ - public static void main(String[] args) throws Exception - { - Socket s=new Socket("localhost",6666); - DataOutputStream dout= new DataOutputStream(s.getOutputStream()); - DataInputStream din=new DataInputStream(s.getInputStream()); - Scanner scan=new Scanner(System.in); - while(true) - { - String strrecived=din.readUTF(); - System.out.println("Server Messaged:"+strrecived); - System.out.println("Write Your Message"); - String strtosend=scan.nextLine(); - dout.writeUTF(strtosend); - dout.flush(); - if(strtosend.equals("Exit")) - { - System.out.println("Client "+s+" is exiting"); - s.close(); - din.close(); - dout.close(); - break; - } - } - } +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.Socket; +import java.util.Scanner; + +public class Client { + + public static void main(String[] args) throws Exception { + Socket s = new Socket("localhost", 6666); + DataOutputStream dout = new DataOutputStream(s.getOutputStream()); + DataInputStream din = new DataInputStream(s.getInputStream()); + Scanner scan = new Scanner(System.in); + while (true) { + String strrecived = din.readUTF(); + System.out.println("Server Messaged:" + strrecived); + System.out.println("Write Your Message"); + String strtosend = scan.nextLine(); + dout.writeUTF(strtosend); + dout.flush(); + if (strtosend.equals("Exit")) { + System.out.println("Client " + s + " is exiting"); + s.close(); + din.close(); + dout.close(); + break; + } + } + } } \ No newline at end of file diff --git a/12. Multithreaded Server/ClientHandler.java b/12. Multithreaded Server/ClientHandler.java new file mode 100644 index 0000000..39b0c3e --- /dev/null +++ b/12. Multithreaded Server/ClientHandler.java @@ -0,0 +1,43 @@ +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.Socket; + +/** + * @author Deki on 03.10.2020 + * @project Networking_In_Java + **/ +public class ClientHandler extends Thread { + + Socket s; + DataInputStream dis; + DataOutputStream dout; + String strrecieved = "", strsent = "", name; + + public ClientHandler(Socket s, String name, DataInputStream dis, DataOutputStream dout) { + this.s = s; + this.dis = dis; + this.dout = dout; + this.name = name; + } + + @Override + public void run() { + try { + while (true) { + dout.writeUTF("What is your name? Written already write Exit."); + strrecieved = dis.readUTF(); + if (strrecieved.equals("Exit")) { + System.out.println("Client " + this.s + " EXITED"); + this.s.close(); + this.dis.close(); + this.dout.close(); + break; + } + System.out.println("Client Messaged " + strrecieved); + } + } catch (Exception e) { + System.out.println(e); + } + + } +} \ No newline at end of file diff --git a/12. Multithreaded Server/Server.java b/12. Multithreaded Server/Server.java index f66a044..40f733b 100644 --- a/12. Multithreaded Server/Server.java +++ b/12. Multithreaded Server/Server.java @@ -1,58 +1,22 @@ -import java.net.*; -import java.io.*; -public class Server{ - public static void main(String[] args) throws Exception - { - ServerSocket ss=new ServerSocket(6666); - while(true) - { - Socket s=ss.accept(); - System.out.println("A new client is connected via "+s); - DataInputStream dis=new DataInputStream(s.getInputStream()); - DataOutputStream dout=new DataOutputStream(s.getOutputStream()); - dout.writeUTF("Enter the name of the client"); - String name=dis.readUTF(); - System.out.println("Assigning a new thread to the client"); - Thread t1=new ClientHandler(s,name,dis,dout); - t1.start(); - } - } -} -class ClientHandler extends Thread{ - Socket s; - DataInputStream dis; - DataOutputStream dout; - String strrecieved="",strsent="",name; - ClientHandler(Socket s,String name,DataInputStream dis,DataOutputStream dout) - { - this.s=s; - this.dis=dis; - this.dout=dout; - this.name=name; - } - public void run() - { - try - { - while(true) - { - dout.writeUTF("What is your name? Written already write Exit."); - strrecieved=dis.readUTF(); - if(strrecieved.equals("Exit")) - { - System.out.println("Client "+this.s+" EXITED"); - this.s.close(); - this.dis.close(); - this.dout.close(); - break; - } - System.out.println("Client Messaged "+strrecieved); - } - } - catch(Exception e) - { - System.out.println(e); - } - - } +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.ServerSocket; +import java.net.Socket; + +public class Server { + + public static void main(String[] args) throws Exception { + ServerSocket ss = new ServerSocket(6666); + while (true) { + Socket s = ss.accept(); + System.out.println("A new client is connected via " + s); + DataInputStream dis = new DataInputStream(s.getInputStream()); + DataOutputStream dout = new DataOutputStream(s.getOutputStream()); + dout.writeUTF("Enter the name of the client"); + String name = dis.readUTF(); + System.out.println("Assigning a new thread to the client"); + Thread t1 = new ClientHandler(s, name, dis, dout); + t1.start(); + } + } } \ No newline at end of file diff --git a/2. URLConnection/URLConnectionClass.java b/2. URLConnection/URLConnectionClass.java new file mode 100644 index 0000000..0beb3fa --- /dev/null +++ b/2. URLConnection/URLConnectionClass.java @@ -0,0 +1,20 @@ +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; + +public class URLConnectionClass { + + public static void main(String[] args) { + try { + URL url = new URL("https://golang.org/doc/code.html"); + URLConnection con = url.openConnection(); + InputStream stream = con.getInputStream(); + int i; + while ((i = stream.read()) != -1) { + System.out.print((char) i); + } + } catch (Exception e) { + System.out.println(e); + } + } +} \ No newline at end of file diff --git a/2. URLConnection/URLConnectionclass.java b/2. URLConnection/URLConnectionclass.java deleted file mode 100644 index b80ca43..0000000 --- a/2. URLConnection/URLConnectionclass.java +++ /dev/null @@ -1,22 +0,0 @@ -import java.io.*; -import java.net.*; -public class URLConnectionclass{ - public static void main(String[] args) - { - try - { - URL url=new URL("https://golang.org/doc/code.html"); - URLConnection con=url.openConnection(); - InputStream stream=con.getInputStream(); - int i; - while((i=stream.read())!=-1) - { - System.out.print((char)i); - } - } - catch(Exception e) - { - System.out.println(e); - } - } -} \ No newline at end of file diff --git a/3. HttpURLConnection/HttpURLConnectionClass.java b/3. HttpURLConnection/HttpURLConnectionClass.java new file mode 100644 index 0000000..8f28ce1 --- /dev/null +++ b/3. HttpURLConnection/HttpURLConnectionClass.java @@ -0,0 +1,19 @@ +import java.net.HttpURLConnection; +import java.net.URL; + +public class HttpURLConnectionClass { + + public static void main(String[] args) { + int i; + try { + URL url = new URL("https://golang.org/doc/code.html"); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + for (i = 1; i <= 8; i++) { + System.out.println(con.getHeaderFieldKey(i) + " = " + con.getHeaderField(i)); + } + con.disconnect(); + } catch (Exception e) { + System.out.println(e); + } + } +} \ No newline at end of file diff --git a/3. HttpURLConnection/HttpURLConnectionclass.java b/3. HttpURLConnection/HttpURLConnectionclass.java deleted file mode 100644 index f5fdeb5..0000000 --- a/3. HttpURLConnection/HttpURLConnectionclass.java +++ /dev/null @@ -1,21 +0,0 @@ -import java.io.*; -import java.net.*; -class HttpURLConnectionclass{ - public static void main(String[] args) - { - int i; - try{ - URL url=new URL("https://golang.org/doc/code.html"); - HttpURLConnection con=(HttpURLConnection) url.openConnection(); - for(i=1;i<=8;i++) - { - System.out.println(con.getHeaderFieldKey(i)+" = "+con.getHeaderField(i)); - } - con.disconnect(); - } - catch(Exception e) - { - System.out.println(e); - } - } -} \ No newline at end of file diff --git a/4. InetAddress/InetAddressClass.java b/4. InetAddress/InetAddressClass.java new file mode 100644 index 0000000..ec57259 --- /dev/null +++ b/4. InetAddress/InetAddressClass.java @@ -0,0 +1,15 @@ +import java.net.InetAddress; + +public class InetAddressClass { + + public static void main(String[] args) { + try { + InetAddress ip = InetAddress.getByName("www.golang.org"); + System.out.println("HostName: " + ip.getHostName()); + System.out.println("IP address: " + ip.getHostAddress()); + + } catch (Exception e) { + System.out.println(e); + } + } +} \ No newline at end of file diff --git a/4. InetAddress/InetAddressclass.java b/4. InetAddress/InetAddressclass.java deleted file mode 100644 index 4be6155..0000000 --- a/4. InetAddress/InetAddressclass.java +++ /dev/null @@ -1,18 +0,0 @@ -import java.io.*; -import java.net.*; -class InetAddressclass{ - public static void main(String[] args) - { - try - { - InetAddress ip=InetAddress.getByName("www.golang.org"); - System.out.println("HostName: "+ip.getHostName()); - System.out.println("IP address: "+ip.getHostAddress()); - - } - catch(Exception e) - { - System.out.println(e); - } - } -} \ No newline at end of file diff --git a/5. DatagramSocket/Reciever.java b/5. DatagramSocket/Reciever.java index 27a5311..c9c58c5 100644 --- a/5. DatagramSocket/Reciever.java +++ b/5. DatagramSocket/Reciever.java @@ -1,14 +1,16 @@ -import java.net.*; -import java.io.*; -class Reciever{ - public static void main(String[] args) throws Exception{ - DatagramSocket ds=new DatagramSocket(3000); - byte[] buff=new byte[1024]; - DatagramPacket dp=new DatagramPacket(buff,1024); - ds.receive(dp); - String str=new String(dp.getData(),0,dp.getLength()); - System.out.println(str); - ds.close(); - } - +import java.net.DatagramPacket; +import java.net.DatagramSocket; + +public class Reciever { + + public static void main(String[] args) throws Exception { + DatagramSocket ds = new DatagramSocket(3000); + byte[] buff = new byte[1024]; + DatagramPacket dp = new DatagramPacket(buff, 1024); + ds.receive(dp); + String str = new String(dp.getData(), 0, dp.getLength()); + System.out.println(str); + ds.close(); + } + } \ No newline at end of file diff --git a/5. DatagramSocket/Sender.java b/5. DatagramSocket/Sender.java index 4412db0..7fbc7a0 100644 --- a/5. DatagramSocket/Sender.java +++ b/5. DatagramSocket/Sender.java @@ -1,13 +1,15 @@ -import java.io.*; -import java.net.*; -public class Sender{ - public static void main(String[] args) throws Exception - { - DatagramSocket ds=new DatagramSocket(); - String str="Welcome to my channel"; - InetAddress ip=InetAddress.getByName("localhost"); - DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),ip,3000); - ds.send(dp); - ds.close(); - } +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; + +public class Sender { + + public static void main(String[] args) throws Exception { + DatagramSocket ds = new DatagramSocket(); + String str = "Welcome to my channel"; + InetAddress ip = InetAddress.getByName("localhost"); + DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 3000); + ds.send(dp); + ds.close(); + } } \ No newline at end of file diff --git a/6. Communication Using UDP/UDPClient.java b/6. Communication Using UDP/UDPClient.java index 18d0f75..510f1c4 100644 --- a/6. Communication Using UDP/UDPClient.java +++ b/6. Communication Using UDP/UDPClient.java @@ -1,25 +1,24 @@ -import java.net.*; -import java.io.*; -import java.util.*; -class UDPClient{ - public static void main(String[] args) throws Exception - { - - DatagramSocket ds=new DatagramSocket(); - Scanner scan=new Scanner(System.in); - while(true) - { - System.out.println("Enter your message:"); - String str=scan.nextLine(); - InetAddress ip=InetAddress.getByName("localhost"); - DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),ip,2334); - ds.send(dp); - if(str.equals("bye")) - { - ds.close(); - break; - } - - } - } +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.util.Scanner; + +public class UDPClient { + + public static void main(String[] args) throws Exception { + DatagramSocket ds = new DatagramSocket(); + Scanner scan = new Scanner(System.in); + while (true) { + System.out.println("Enter your message:"); + String str = scan.nextLine(); + InetAddress ip = InetAddress.getByName("localhost"); + DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 2334); + ds.send(dp); + if (str.equals("bye")) { + ds.close(); + break; + } + + } + } } \ No newline at end of file diff --git a/6. Communication Using UDP/UDPServer.java b/6. Communication Using UDP/UDPServer.java index 771f598..636792e 100644 --- a/6. Communication Using UDP/UDPServer.java +++ b/6. Communication Using UDP/UDPServer.java @@ -1,23 +1,23 @@ -import java.net.*; -import java.io.*; -public class UDPServer{ - public static void main(String[] args) throws Exception - { - DatagramSocket ds=new DatagramSocket(2334); - byte[] buff=new byte[1024]; - //DatagramPacket dpreceive=null; - while(true){ - DatagramPacket dpreceive=new DatagramPacket(buff,buff.length); - ds.receive(dpreceive); - String str=new String(dpreceive.getData(),0,dpreceive.getLength()); - System.out.println("Client Messaged--> "+str); - if(str.equals("bye")) - { - System.out.println("Server Is Exiting .... BYE"); - break; - } - buff=new byte[1024]; - } - - } +import java.net.DatagramPacket; +import java.net.DatagramSocket; + +public class UDPServer { + + public static void main(String[] args) throws Exception { + DatagramSocket ds = new DatagramSocket(2334); + byte[] buff = new byte[1024]; + //DatagramPacket dpreceive = null; + while (true) { + DatagramPacket dpreceive = new DatagramPacket(buff, buff.length); + ds.receive(dpreceive); + String str = new String(dpreceive.getData(), 0, dpreceive.getLength()); + System.out.println("Client Messaged--> " + str); + if (str.equals("bye")) { + System.out.println("Server Is Exiting .... BYE"); + break; + } + buff = new byte[1024]; + } + + } } \ No newline at end of file diff --git a/7. Two Way Communication using UDP/TwoWayUDPClient.java b/7. Two Way Communication using UDP/TwoWayUDPClient.java new file mode 100644 index 0000000..68dbb5a --- /dev/null +++ b/7. Two Way Communication using UDP/TwoWayUDPClient.java @@ -0,0 +1,30 @@ +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.util.Scanner; + +public class TwoWayUDPClient { + + public static void main(String[] args) throws Exception { + DatagramSocket ds = new DatagramSocket(); + DatagramSocket ds1 = new DatagramSocket(6000); + byte[] buff = new byte[1024]; + Scanner scan = new Scanner(System.in); + while (true) { + System.out.println("Enter your message:"); + String str = scan.nextLine(); + InetAddress ip = InetAddress.getByName("localhost"); + DatagramPacket dp = new DatagramPacket(str.getBytes(), str.length(), ip, 2334); + ds.send(dp); + if (str.equals("bye")) { + ds.close(); + break; + } + DatagramPacket dpreceive = new DatagramPacket(buff, buff.length); + ds1.receive(dpreceive); + String str1 = new String(dpreceive.getData(), 0, dpreceive.getLength()); + System.out.println("Server Messaged--> " + str1); + buff = new byte[1024]; + } + } +} \ No newline at end of file diff --git a/7. Two Way Communication using UDP/TwoWayUDPServer.java b/7. Two Way Communication using UDP/TwoWayUDPServer.java new file mode 100644 index 0000000..968075b --- /dev/null +++ b/7. Two Way Communication using UDP/TwoWayUDPServer.java @@ -0,0 +1,31 @@ +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.util.Scanner; + +public class TwoWayUDPServer { + + public static void main(String[] args) throws Exception { + DatagramSocket ds = new DatagramSocket(2334); + byte[] buff = new byte[1024]; + //DatagramPacket dpreceive = null; + Scanner scan = new Scanner(System.in); + while (true) { + DatagramPacket dpreceive = new DatagramPacket(buff, buff.length); + ds.receive(dpreceive); + String str = new String(dpreceive.getData(), 0, dpreceive.getLength()); + System.out.println("Client Messaged--> " + str); + if (str.equals("bye")) { + System.out.println("Server Is Exiting .... BYE"); + break; + } + buff = new byte[1024]; + System.out.println("Enter your message:"); + String str1 = scan.nextLine(); + InetAddress ip = InetAddress.getByName("localhost"); + DatagramPacket dp = new DatagramPacket(str1.getBytes(), str1.length(), ip, 6000); + ds.send(dp); + } + + } +} \ No newline at end of file diff --git a/7. Two Way Communication using UDP/UDPClient.java b/7. Two Way Communication using UDP/UDPClient.java deleted file mode 100644 index 673166d..0000000 --- a/7. Two Way Communication using UDP/UDPClient.java +++ /dev/null @@ -1,31 +0,0 @@ -import java.net.*; -import java.io.*; -import java.util.*; -class UDPClient{ - public static void main(String[] args) throws Exception - { - - DatagramSocket ds=new DatagramSocket(); - DatagramSocket ds1=new DatagramSocket(6000); - byte[] buff=new byte[1024]; - Scanner scan=new Scanner(System.in); - while(true) - { - System.out.println("Enter your message:"); - String str=scan.nextLine(); - InetAddress ip=InetAddress.getByName("localhost"); - DatagramPacket dp=new DatagramPacket(str.getBytes(),str.length(),ip,2334); - ds.send(dp); - if(str.equals("bye")) - { - ds.close(); - break; - } - DatagramPacket dpreceive=new DatagramPacket(buff,buff.length); - ds1.receive(dpreceive); - String str1=new String(dpreceive.getData(),0,dpreceive.getLength()); - System.out.println("Server Messaged--> "+str1); - buff=new byte[1024]; - } - } -} \ No newline at end of file diff --git a/7. Two Way Communication using UDP/UDPServer.java b/7. Two Way Communication using UDP/UDPServer.java deleted file mode 100644 index ddd5b5a..0000000 --- a/7. Two Way Communication using UDP/UDPServer.java +++ /dev/null @@ -1,30 +0,0 @@ -import java.net.*; -import java.io.*; -import java.util.*; -public class UDPServer{ - public static void main(String[] args) throws Exception - { - DatagramSocket ds=new DatagramSocket(2334); - byte[] buff=new byte[1024]; - //DatagramPacket dpreceive=null; - Scanner scan=new Scanner(System.in); - while(true){ - DatagramPacket dpreceive=new DatagramPacket(buff,buff.length); - ds.receive(dpreceive); - String str=new String(dpreceive.getData(),0,dpreceive.getLength()); - System.out.println("Client Messaged--> "+str); - if(str.equals("bye")) - { - System.out.println("Server Is Exiting .... BYE"); - break; - } - buff=new byte[1024]; - System.out.println("Enter your message:"); - String str1=scan.nextLine(); - InetAddress ip=InetAddress.getByName("localhost"); - DatagramPacket dp=new DatagramPacket(str1.getBytes(),str1.length(),ip,6000); - ds.send(dp); - } - - } -} \ No newline at end of file diff --git a/8. ServerSocket/ServerSocketClass.java b/8. ServerSocket/ServerSocketClass.java new file mode 100644 index 0000000..1daee7c --- /dev/null +++ b/8. ServerSocket/ServerSocketClass.java @@ -0,0 +1,16 @@ +import java.io.DataInputStream; +import java.net.ServerSocket; +import java.net.Socket; + +public class ServerSocketClass { + + public static void main(String[] args) throws Exception { + ServerSocket ss = new ServerSocket(9000); + Socket s = ss.accept(); + DataInputStream dis = new DataInputStream(s.getInputStream()); + String str = dis.readUTF(); + System.out.println("Message:-> " + str); + ss.close(); + } + +} \ No newline at end of file diff --git a/8. ServerSocket/ServerSocketclass.java b/8. ServerSocket/ServerSocketclass.java deleted file mode 100644 index 8f6f996..0000000 --- a/8. ServerSocket/ServerSocketclass.java +++ /dev/null @@ -1,14 +0,0 @@ -import java.net.*; -import java.io.*; -class ServerSocketclass{ - public static void main(String[] args)throws Exception - { - ServerSocket ss=new ServerSocket(9000); - Socket s=ss.accept(); - DataInputStream dis=new DataInputStream(s.getInputStream()); - String str=(String) dis.readUTF(); - System.out.println("Message:-> "+str); - ss.close(); - } - -} \ No newline at end of file diff --git a/8. ServerSocket/SocketClass.java b/8. ServerSocket/SocketClass.java new file mode 100644 index 0000000..22698c2 --- /dev/null +++ b/8. ServerSocket/SocketClass.java @@ -0,0 +1,14 @@ +import java.io.DataOutputStream; +import java.net.Socket; + +public class SocketClass { + + public static void main(String[] args) throws Exception { + Socket s = new Socket("localhost", 9000); + DataOutputStream dout = new DataOutputStream(s.getOutputStream()); + dout.writeUTF("Hello my name is Prajjawal"); + dout.flush(); + dout.close(); + s.close(); + } +} \ No newline at end of file diff --git a/8. ServerSocket/Socketclass.java b/8. ServerSocket/Socketclass.java deleted file mode 100644 index f31d61d..0000000 --- a/8. ServerSocket/Socketclass.java +++ /dev/null @@ -1,13 +0,0 @@ -import java.net.*; -import java.io.*; -public class Socketclass{ - public static void main(String[] args) throws Exception - { - Socket s=new Socket("localhost",9000); - DataOutputStream dout=new DataOutputStream(s.getOutputStream()); - dout.writeUTF("Hello my name is Prajjawal"); - dout.flush(); - dout.close(); - s.close(); - } -} \ No newline at end of file diff --git a/9. Communication using TCP/TcpClient.java b/9. Communication using TCP/TcpClient.java index a56b1d7..e054c2a 100644 --- a/9. Communication using TCP/TcpClient.java +++ b/9. Communication using TCP/TcpClient.java @@ -1,27 +1,27 @@ -import java.net.*; -import java.io.*; -import java.util.*; -public class TcpClient{ - public static void main(String[] args) throws Exception - { - Scanner scan=new Scanner(System.in); - Socket s=new Socket("localhost",5656); - DataOutputStream dout=new DataOutputStream(s.getOutputStream()); - DataInputStream dis=new DataInputStream(s.getInputStream()); - while(true) - { - System.out.println("Write Your message"); - String str=scan.nextLine(); - dout.writeUTF(str); - dout.flush(); - if(str.equals("bye")) - { - dout.close(); - s.close(); - break; - } - - } - } +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.Socket; +import java.util.Scanner; + +public class TcpClient { + + public static void main(String[] args) throws Exception { + Scanner scan = new Scanner(System.in); + Socket s = new Socket("localhost", 5656); + DataOutputStream dout = new DataOutputStream(s.getOutputStream()); + DataInputStream dis = new DataInputStream(s.getInputStream()); + while (true) { + System.out.println("Write Your message"); + String str = scan.nextLine(); + dout.writeUTF(str); + dout.flush(); + if (str.equals("bye")) { + dout.close(); + s.close(); + break; + } + + } + } } \ No newline at end of file diff --git a/9. Communication using TCP/TcpServer.java b/9. Communication using TCP/TcpServer.java index 2a8a2cc..097a65f 100644 --- a/9. Communication using TCP/TcpServer.java +++ b/9. Communication using TCP/TcpServer.java @@ -1,28 +1,28 @@ -import java.net.*; -import java.io.*; -import java.util.*; -class TcpServer{ - public static void main(String[] args) throws Exception - { - int i=0; - ServerSocket ss=new ServerSocket(5656); - Socket s=ss.accept(); - Scanner scan=new Scanner(System.in); - DataOutputStream dout=new DataOutputStream(s.getOutputStream()); - DataInputStream dis=new DataInputStream(s.getInputStream()); - while(true) - { - String str=dis.readUTF(); - System.out.println("Client--->"+str); - if(str.equals("bye")) - { - System.out.println("Client Messaged .... BYE..Exiting"); - dis.close(); - s.close(); - ss.close(); - break; - } - } - - } +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.net.ServerSocket; +import java.net.Socket; +import java.util.Scanner; + +public class TcpServer { + + public static void main(String[] args) throws Exception { + ServerSocket ss = new ServerSocket(5656); + Socket s = ss.accept(); + Scanner scan = new Scanner(System.in); + DataOutputStream dout = new DataOutputStream(s.getOutputStream()); + DataInputStream dis = new DataInputStream(s.getInputStream()); + while (true) { + String str = dis.readUTF(); + System.out.println("Client--->" + str); + if (str.equals("bye")) { + System.out.println("Client Messaged .... BYE..Exiting"); + dis.close(); + s.close(); + ss.close(); + break; + } + } + + } } \ No newline at end of file