SlideShare a Scribd company logo
2
Most read
3
Most read
7
Most read
NETWORKING IN JAVA   AN EFFORT BY:- ANKUR AGRAWAL B.TECH(CSE 5 th  SEM)
TCP & UDP TCP(TRANSMISSION CONTROL PROTOCOL) TCP is a connection-based protocol that provides a reliable flow of data between two computers. e.g. : Telephone call. Web browsing. UDP(USER DATAGRAM PROTOCOL) UDP  is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. UDP is not connection-based like TCP. e.g.: ping command.
PORT Ports are identified by a 16-bit number. The TCP and UDP protocols use PORTS to map incoming data to a particular process running on a computer.
INTERNET ADDRESSING & DNS(DOMAIN NAME SYSTEM) Every computer on a network is known by a unique number which is known as IP Address. Generally we use IPV4 which consists of 4 BYTES. (e.g. 172.17.167.4)etc. Websites have both a friendly address, called a URL, and an IP address. People use URLs to find websites, but computers use IP addresses to find websites. DNS translates URLs into IP addresses (and vice versa).
NETWORKING CLASSES(java.net PACKAGE) InetAddress :- This class is used to encapsulate both the numerical IP address and the domain name for that address. To create an InetAddress object we have to use one of the available factory methods:- 1.static InetAddress getLocalHost( ) throws UnknownHostException 2.static InetAddress getByName(String hostName) throws UnknownHostException 3.static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException
import java.net.*; public class prog1 { public static void main(String args[])throws UnknownHostException { InetAddress in; in=InetAddress.getLocalHost(); System.out.println(&quot;local host name=&quot;+in); in=InetAddress.getByName(&quot;www.google.com&quot;); System.out.println(&quot;ip of google::&quot;+in); InetAddress dn[]=InetAddress.getAllByName(&quot;www.google.com&quot;); System.out.println(&quot;ALL NAMES FOR GOOGLE&quot;); for(int i=0;i<dn.length;i++) System.out.println(dn[i]); } } PROGRAM TO ILLUSTRATE InetAddress
URL:- URL is an acronym for   Uniform Resource Locator   and is a reference (an address) to a resource on the Internet. http :// www.google.com:80/index.htm PROTOCOL IDENTIFIER RESOURCE NAME CONSTRUCTOR:- 1. URL(String urlSpecifier) 2.URL(String protocolName, String hostName, int port, String path) 3.URL(String protocolName, String hostName, String path) All of these constructor throws MalformedURLException.
PROGRAM TO ILLUSTRATE URL import java.io.*; import java.net.*; public class prog2 { public static void main(String args[])throws MalformedURLException { URL u=new URL(&quot;http://www.google.com:80/index.htm&quot;); System.out.println(&quot;HOST NAME=&quot;+u.getHost()); System.out.println(&quot;PORT =&quot;+u.getPort()); System.out.println(&quot;PROTOCOL=&quot;+u.getProtocol()); System.out.println(&quot;PATH=&quot;+u.getFile()); } }
THE openStream() method It is used for directly reading from a URL. import java.net.*;  import java.io.*;  public class prog3 {   public static void main(String[] args) throws Exception  { URL yahoo = new URL(&quot;http://www.yahoo.com/&quot;);  BufferedReader in = new BufferedReader( new  InputStreamReader( yahoo.openStream()));  String inputLine;  while ((inputLine = in.readLine()) != null) System.out.println(inputLine); In.close();  } }
READING FROM  A  URL THROUGH URLConnection OBJECT We can create URLConnection object using the    openConnection() method of URL object. import java.net.*;  import java.io.*;  public class prog4 { public static void main(String[ ] args) throws Exception  { URL yahoo = new URL(&quot;http://www.yahoo.com/&quot;);  URLConnection yc = yahoo.openConnection();  BufferedReader in = new BufferedReader( new InputStreamReader(  yc.getInputStream()));  String inputLine;  while ((inputLine = in.readLine()) != null)   System.out.println(inputLine);  in.close();  } }
SOCKET PROGRAMMING URL s and  URLConnection s provide a relatively high-level mechanism for accessing resources on the Internet. Sometimes your programs require lower-level network communication, for example, when you want to write a client-server application.  The communication that occurs between the client and the server must be  reliable . TCP  provides a reliable, point-to-point communication channel . To communicate over TCP, a client program and a server program establish a connection to one another.  Each program binds a  socket  to its end of the connection. To communicate, the client and the server each reads from and writes to the socket bound to the connection.
WHAT IS A SOCKET A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent.  In java.net package there are two classes for socket purpose. Socket ServerSocket  We can initialize a Socket object using  following constructor– Socket(String hostName, int port) We can initialize a ServerSocket object using  following constructor- ServerSocket s = new ServerSocket(port no.) It will establishes a server that monitors port no.
Java Sockets ServerSocket(1234) Socket(“128.250.25.158”, 1234) Output/write stream Input/read stream It can be host_name like “mandroo.cs.mu.oz.au” Client Server
LET’S DEAL WITH ServerSocket CLASS accept():- ServerSocket has a method called accept( ), which is a blocking call that will wait for a client to initiate communications, and then return with a normal Socket that is then used for communication with the client. e.g . ServerSocket s=new ServerSocket(8189); establishes a server that monitors port 8189.  The command    Socket sp = s.accept(); tells the program to wait indefinitely until a client connects to that port. Once someone connects to this port by sending the correct request over the network, this method returns a Socket object that represents the connection that was made.
import java.io.*; import java.net.*; import java.util.*; public class prog5 { public static void main(String args[])throws    Exception   { ServerSocket s=new ServerSocket(8120); System.out.println(“SERVER IS  WAITING FOR THE CLIENTS TO BE  CONNECTED…..”); while(true) { Socket sp=s.accept();    Scanner in=new Scanner(sp.getInputStream()); PrintWriter  out=newPrintWriter(sp.getOutputStream(),true); out.println(&quot;WRITE STH. WRITE BYE TO EXIT &quot;); boolean f=true; While(f==true && (in.hasNextLine())==true) { String line=in.nextline(); If((line.trim()).equals(“BYE&quot;)) { f=false; break; } else out.Println(&quot;echo::&quot;+line) } Sp.Close(); } } } PROGRAM 1:- IMPLEMENTING SERVER SOCKET AND COMMUNICATION BETWEEN CLIENT AND SERVER.(THERE IS ONLY ONE CLIENT  AT A TIME) IF MORE THAN 1 CLIENT WANT TO COMMUNIACATE THEN THEY HAVE TO WAIT TILL ONE FINISHES.
PROG2:-COMMUNIACTION BETWEEN MULTIPLE CLIENTS AND A SERVER USING SOCKETS SIMULTANEOUSLY(no need to wait).(ServerSocket) import java.io.*; import java.net.*; import java.util.*; public class prog6 { public static void  main(String args[]) throws Exception { int i=1; ServerSocket s=new ServerSocket(8120); System.out.println(&quot;\n SERVER WAITS FOR THE CLIENTS TO      BE CONNECTED...........&quot;); while(true) { Socket sp=s.accept(); System.out.println(&quot;\nclient no.&quot;+i+&quot;connected\n&quot;); Runnable r=new netthread(sp,i); Thread t=new Thread(r); t.start(); // it begins the execution of thread beginning    at the run() method  i++; } } }
class netthread implements Runnable { Socket soc; int cl; public netthread(Socket k,int j) { soc=k; cl=j; } public void run()  { try {   try   { Scanner in=new     Scanner(soc.getInputStream()); PrintWriter out=new PrintWriter(soc.getOutputStream(),true); out.println(&quot;WRITE STH. WRITE BYE TO EXIT &quot;); Boolean f=true; while(f==true && in.hasNextLine()==true) { String line=in.nextLine(); if(line.trim().equals(&quot;BYE&quot;)) { f=false; break; } else out.println(&quot;ECHO::&quot;+line); }   }  finally { soc.close(); System.out.println(&quot;CLIENT NO.&quot;+cl+&quot;DISCONNECTED&quot;); }   }catch(IOException e) {   System.out.println(e); } } }
 

More Related Content

What's hot (20)

PPSX
Exception Handling
Reddhi Basu
 
PDF
Idiomatic Kotlin
intelliyole
 
PPT
Java static keyword
Lovely Professional University
 
PDF
Alarms
maamir farooq
 
PPT
The Kotlin Programming Language
intelliyole
 
PDF
Android resources
ma-polimi
 
PPTX
Control structures in java
VINOTH R
 
PPT
Singleton design pattern
11prasoon
 
PPTX
Interface in java
PhD Research Scholar
 
PPT
Fundamentals of JAVA
KUNAL GADHIA
 
PPT
JAVA OOP
Sunil OS
 
PDF
Enumeration in Java Explained | Java Tutorial | Edureka
Edureka!
 
PPTX
Introduction to Koltin for Android Part I
Atif AbbAsi
 
PPTX
Chatbot with RASA | Valuebound
valuebound
 
PPTX
Inner Classes & Multi Threading in JAVA
Tech_MX
 
PDF
Location-Based Services on Android
Jomar Tigcal
 
PPTX
Typescript ppt
akhilsreyas
 
PPT
Servlets
Sasidhar Kothuru
 
Exception Handling
Reddhi Basu
 
Idiomatic Kotlin
intelliyole
 
Java static keyword
Lovely Professional University
 
The Kotlin Programming Language
intelliyole
 
Android resources
ma-polimi
 
Control structures in java
VINOTH R
 
Singleton design pattern
11prasoon
 
Interface in java
PhD Research Scholar
 
Fundamentals of JAVA
KUNAL GADHIA
 
JAVA OOP
Sunil OS
 
Enumeration in Java Explained | Java Tutorial | Edureka
Edureka!
 
Introduction to Koltin for Android Part I
Atif AbbAsi
 
Chatbot with RASA | Valuebound
valuebound
 
Inner Classes & Multi Threading in JAVA
Tech_MX
 
Location-Based Services on Android
Jomar Tigcal
 
Typescript ppt
akhilsreyas
 

Viewers also liked (8)

PPTX
Network Socket Programming with JAVA
Dudy Ali
 
PPT
Sockets
sivindia
 
PDF
Java networking programs - theory
Mukesh Tekwani
 
PDF
Netty Cookbook - Table of contents
Trieu Nguyen
 
PPT
Basics of java programming language
masud33bd
 
PPT
Socket Programming Tutorial
Jignesh Patel
 
PDF
Basic Java Programming
Math-Circle
 
PPTX
Optical fiber
Math-Circle
 
Network Socket Programming with JAVA
Dudy Ali
 
Sockets
sivindia
 
Java networking programs - theory
Mukesh Tekwani
 
Netty Cookbook - Table of contents
Trieu Nguyen
 
Basics of java programming language
masud33bd
 
Socket Programming Tutorial
Jignesh Patel
 
Basic Java Programming
Math-Circle
 
Optical fiber
Math-Circle
 
Ad

Similar to Networking & Socket Programming In Java (20)

PPT
Unit 8 Java
arnold 7490
 
PPT
Network programming in Java
Tushar B Kute
 
PPT
Network Programming in Java
Tushar B Kute
 
PPTX
A.java
JahnaviBhagat
 
PPTX
Networking in Java
Tushar B Kute
 
PPTX
Advance Java-Network Programming
ashok hirpara
 
PDF
28 networking
Ravindra Rathore
 
PPT
Socket Programming it-slideshares.blogspot.com
phanleson
 
PPTX
5_6278455688045789623.pptx
EliasPetros
 
PPT
Java networking
Arati Gadgil
 
PPT
Network programming in Java
Tushar B Kute
 
PPT
Socket Programming - nitish nagar
Nitish Nagar
 
PPT
Socket Programming in Java.ppt yeh haii
inambscs4508
 
PDF
javanetworking
Arjun Shanka
 
PPT
Md13 networking
Rakesh Madugula
 
PPT
Chapter 4 slides
lara_ays
 
DOCX
Please look at the attach See.doc. I am getting this error all th.docx
randymartin91030
 
DOCX
Mail Server Project Report
Kavita Sharma
 
PPTX
Chapter 4
Ebisa Bekele
 
PPTX
Socket & Server Socket
Hemant Chetwani
 
Unit 8 Java
arnold 7490
 
Network programming in Java
Tushar B Kute
 
Network Programming in Java
Tushar B Kute
 
Networking in Java
Tushar B Kute
 
Advance Java-Network Programming
ashok hirpara
 
28 networking
Ravindra Rathore
 
Socket Programming it-slideshares.blogspot.com
phanleson
 
5_6278455688045789623.pptx
EliasPetros
 
Java networking
Arati Gadgil
 
Network programming in Java
Tushar B Kute
 
Socket Programming - nitish nagar
Nitish Nagar
 
Socket Programming in Java.ppt yeh haii
inambscs4508
 
javanetworking
Arjun Shanka
 
Md13 networking
Rakesh Madugula
 
Chapter 4 slides
lara_ays
 
Please look at the attach See.doc. I am getting this error all th.docx
randymartin91030
 
Mail Server Project Report
Kavita Sharma
 
Chapter 4
Ebisa Bekele
 
Socket & Server Socket
Hemant Chetwani
 
Ad

Recently uploaded (20)

PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
July Patch Tuesday
Ivanti
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Biography of Daniel Podor.pdf
Daniel Podor
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
July Patch Tuesday
Ivanti
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 

Networking & Socket Programming In Java

  • 1. NETWORKING IN JAVA AN EFFORT BY:- ANKUR AGRAWAL B.TECH(CSE 5 th SEM)
  • 2. TCP & UDP TCP(TRANSMISSION CONTROL PROTOCOL) TCP is a connection-based protocol that provides a reliable flow of data between two computers. e.g. : Telephone call. Web browsing. UDP(USER DATAGRAM PROTOCOL) UDP is a protocol that sends independent packets of data, called datagrams, from one computer to another with no guarantees about arrival. UDP is not connection-based like TCP. e.g.: ping command.
  • 3. PORT Ports are identified by a 16-bit number. The TCP and UDP protocols use PORTS to map incoming data to a particular process running on a computer.
  • 4. INTERNET ADDRESSING & DNS(DOMAIN NAME SYSTEM) Every computer on a network is known by a unique number which is known as IP Address. Generally we use IPV4 which consists of 4 BYTES. (e.g. 172.17.167.4)etc. Websites have both a friendly address, called a URL, and an IP address. People use URLs to find websites, but computers use IP addresses to find websites. DNS translates URLs into IP addresses (and vice versa).
  • 5. NETWORKING CLASSES(java.net PACKAGE) InetAddress :- This class is used to encapsulate both the numerical IP address and the domain name for that address. To create an InetAddress object we have to use one of the available factory methods:- 1.static InetAddress getLocalHost( ) throws UnknownHostException 2.static InetAddress getByName(String hostName) throws UnknownHostException 3.static InetAddress[ ] getAllByName(String hostName) throws UnknownHostException
  • 6. import java.net.*; public class prog1 { public static void main(String args[])throws UnknownHostException { InetAddress in; in=InetAddress.getLocalHost(); System.out.println(&quot;local host name=&quot;+in); in=InetAddress.getByName(&quot;www.google.com&quot;); System.out.println(&quot;ip of google::&quot;+in); InetAddress dn[]=InetAddress.getAllByName(&quot;www.google.com&quot;); System.out.println(&quot;ALL NAMES FOR GOOGLE&quot;); for(int i=0;i<dn.length;i++) System.out.println(dn[i]); } } PROGRAM TO ILLUSTRATE InetAddress
  • 7. URL:- URL is an acronym for Uniform Resource Locator and is a reference (an address) to a resource on the Internet. http :// www.google.com:80/index.htm PROTOCOL IDENTIFIER RESOURCE NAME CONSTRUCTOR:- 1. URL(String urlSpecifier) 2.URL(String protocolName, String hostName, int port, String path) 3.URL(String protocolName, String hostName, String path) All of these constructor throws MalformedURLException.
  • 8. PROGRAM TO ILLUSTRATE URL import java.io.*; import java.net.*; public class prog2 { public static void main(String args[])throws MalformedURLException { URL u=new URL(&quot;http://www.google.com:80/index.htm&quot;); System.out.println(&quot;HOST NAME=&quot;+u.getHost()); System.out.println(&quot;PORT =&quot;+u.getPort()); System.out.println(&quot;PROTOCOL=&quot;+u.getProtocol()); System.out.println(&quot;PATH=&quot;+u.getFile()); } }
  • 9. THE openStream() method It is used for directly reading from a URL. import java.net.*; import java.io.*; public class prog3 { public static void main(String[] args) throws Exception { URL yahoo = new URL(&quot;http://www.yahoo.com/&quot;); BufferedReader in = new BufferedReader( new InputStreamReader( yahoo.openStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); In.close(); } }
  • 10. READING FROM A URL THROUGH URLConnection OBJECT We can create URLConnection object using the openConnection() method of URL object. import java.net.*; import java.io.*; public class prog4 { public static void main(String[ ] args) throws Exception { URL yahoo = new URL(&quot;http://www.yahoo.com/&quot;); URLConnection yc = yahoo.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader( yc.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); } }
  • 11. SOCKET PROGRAMMING URL s and URLConnection s provide a relatively high-level mechanism for accessing resources on the Internet. Sometimes your programs require lower-level network communication, for example, when you want to write a client-server application. The communication that occurs between the client and the server must be reliable . TCP provides a reliable, point-to-point communication channel . To communicate over TCP, a client program and a server program establish a connection to one another. Each program binds a socket to its end of the connection. To communicate, the client and the server each reads from and writes to the socket bound to the connection.
  • 12. WHAT IS A SOCKET A socket is one endpoint of a two-way communication link between two programs running on the network. A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent. In java.net package there are two classes for socket purpose. Socket ServerSocket We can initialize a Socket object using following constructor– Socket(String hostName, int port) We can initialize a ServerSocket object using following constructor- ServerSocket s = new ServerSocket(port no.) It will establishes a server that monitors port no.
  • 13. Java Sockets ServerSocket(1234) Socket(“128.250.25.158”, 1234) Output/write stream Input/read stream It can be host_name like “mandroo.cs.mu.oz.au” Client Server
  • 14. LET’S DEAL WITH ServerSocket CLASS accept():- ServerSocket has a method called accept( ), which is a blocking call that will wait for a client to initiate communications, and then return with a normal Socket that is then used for communication with the client. e.g . ServerSocket s=new ServerSocket(8189); establishes a server that monitors port 8189. The command Socket sp = s.accept(); tells the program to wait indefinitely until a client connects to that port. Once someone connects to this port by sending the correct request over the network, this method returns a Socket object that represents the connection that was made.
  • 15. import java.io.*; import java.net.*; import java.util.*; public class prog5 { public static void main(String args[])throws Exception { ServerSocket s=new ServerSocket(8120); System.out.println(“SERVER IS WAITING FOR THE CLIENTS TO BE CONNECTED…..”); while(true) { Socket sp=s.accept(); Scanner in=new Scanner(sp.getInputStream()); PrintWriter out=newPrintWriter(sp.getOutputStream(),true); out.println(&quot;WRITE STH. WRITE BYE TO EXIT &quot;); boolean f=true; While(f==true && (in.hasNextLine())==true) { String line=in.nextline(); If((line.trim()).equals(“BYE&quot;)) { f=false; break; } else out.Println(&quot;echo::&quot;+line) } Sp.Close(); } } } PROGRAM 1:- IMPLEMENTING SERVER SOCKET AND COMMUNICATION BETWEEN CLIENT AND SERVER.(THERE IS ONLY ONE CLIENT AT A TIME) IF MORE THAN 1 CLIENT WANT TO COMMUNIACATE THEN THEY HAVE TO WAIT TILL ONE FINISHES.
  • 16. PROG2:-COMMUNIACTION BETWEEN MULTIPLE CLIENTS AND A SERVER USING SOCKETS SIMULTANEOUSLY(no need to wait).(ServerSocket) import java.io.*; import java.net.*; import java.util.*; public class prog6 { public static void main(String args[]) throws Exception { int i=1; ServerSocket s=new ServerSocket(8120); System.out.println(&quot;\n SERVER WAITS FOR THE CLIENTS TO BE CONNECTED...........&quot;); while(true) { Socket sp=s.accept(); System.out.println(&quot;\nclient no.&quot;+i+&quot;connected\n&quot;); Runnable r=new netthread(sp,i); Thread t=new Thread(r); t.start(); // it begins the execution of thread beginning at the run() method i++; } } }
  • 17. class netthread implements Runnable { Socket soc; int cl; public netthread(Socket k,int j) { soc=k; cl=j; } public void run() { try { try { Scanner in=new Scanner(soc.getInputStream()); PrintWriter out=new PrintWriter(soc.getOutputStream(),true); out.println(&quot;WRITE STH. WRITE BYE TO EXIT &quot;); Boolean f=true; while(f==true && in.hasNextLine()==true) { String line=in.nextLine(); if(line.trim().equals(&quot;BYE&quot;)) { f=false; break; } else out.println(&quot;ECHO::&quot;+line); } } finally { soc.close(); System.out.println(&quot;CLIENT NO.&quot;+cl+&quot;DISCONNECTED&quot;); } }catch(IOException e) { System.out.println(e); } } }
  • 18.