SlideShare a Scribd company logo
1 of 18
R.Pavithra
Department of CS&IT
Nadar saraswathi college of
arts & science, theni.
contents
 Introduction Sockets
 Server Socket And Client Socket
 Tcp/ Ip In Java
 Types Of Sockets In Java
 Tcp/Ip Server Socket
 Tcp/Ip Client Socket
 Conculsion
Introduction sockets
 The term socket programming refers to writing programs
that execute across multiple computers in which the
devices are all connected to each other using a network.
 There are two communication protocols that one can use
for socket programming :User datagram
protocol(UDP)and Transfer control protocol(TCP)
Server socket and client socket
 A server socket keeps on listening to any incoming
connections from any of the clients.
 When you create a client socket, you simply specify the
server address and port. You never bind or listen using a
client socket.
 You simply connect a client socket . On the other hand
you dont connect using a server socket.
TCP/IP in java
 Accessing TCP/IP from java is Straightforward. The
main functionality is in the following classes:
 Java.net.InetAddress: Represents an IP address( either
IPV4or IPV6) and has methods for performing DNS
lookup( next slide).
 Java.net.Socket: Represents a TCP socket.
 Java.net.ServerSocket: Represents a server socket which
is capable of waiting for requests from clients.
Types of sockets in java
 Java provides three different types of sockets connection-
oriented(TCP) sockets are implemented with the socket
class.
 Connectionless(UDP) sockets uses the Datagram Socket
class. A third type is the Multicast Socket class, which is a
subclass of the datagram Socket class.
 Server
 client
TCP/IP SERVER SOCKETS
 A socket in java is one endpoint of two-way
communication link between two programs runnning 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
to .
 An endpoint is a combination of an IP address and a port
number.
continue
syntax descriptions
Server Socket( int port ) throws
IOException
Creates server socket on the specified
port with a queue length of 50
Server Socket( int port, int maxQueue)
Throws IOException
Creates a server socket on the specified
port with a maximum queue length of
maxQueue.
Server Socket( it port, int
maxQueue,inetAddress local Address)
Throws IOException
Creates a server socket on the specified
port with a maximum queue length of
maxQueue, on a multihomed host, local
Address specifies the ip address to which
this socket binds.
Example program for server socket
Import java.net.*;
Import java.io.*;
Public class serverside{
Private Socket socket=null;
Private serversocket =null;
Sop(“server started”);
Sop(“waiting for client”);
Socket=server.accept();
Sop(“client accepted”);
In=new dataintputstream (
New bufferedinputstream(socket.getInputstream()));
String line=””;
While(!line.equals(‘over”)){
Try{
Line=input.readline();
Out.writeUTF(line);}
Catch(IOException i){
Sop(i);}}
Sop(“closing connection”);
Socket.close();
In.close();}
Catch(IOException i){
Sop(i);}}
Public static void main(stringargs[]){
Server server =new server(5000);}}
TCP/IP CLIENT SOCKET
 TCP/IP client sockets. TCP/IP sockets are used to
implement reliable, bidirectional, persistent, point-to-
point, stream-based connections between hosts on the
internet. A socket can be used to connect java’s I/O system
to other programs that may reside either on the local
machine or on any other machine on the internet.
syntax description
InetAddress getInetAddress() Returns the InetAdd associated with
the socket object .It returns null If
the socket is not connected.
Intgetport()
Returns the remote port to which
the invoking socket object is
connected . It returns O if the
socket is not connected.
Intgetlocalport() Returns the local port to which the
invoking socket object is bound. It
returns -1if the socket is not bound.
Continue……
 You can gain access I/P and O/P streams associated with
socket by use of
 Get I/P stream()
 Get O/P stream()
 Each can throw an IOException if the socket has been
invalidated by a loss of connections.
syntax description
inputstream getI/Pstream()
Throws IOException
Returns the inputs stream
associated with the invoking
socket
Outputstream get O/P
stream() throws IOException
Returns the outputs stream
associated with the invoking
socket
Other method
Syntax Description
“including connect()” Which allows you to specify a new
connections
“is connected()” Which returns true if the socket is
connected to server.
“is bound()” Which returns if the socket is bound to
an address
“is closed()” Which returns true if the socket is
closed.
Example program client socket
Import java.net.*;
Import java.io.*;
Public class clientprogram{
Private Socket socket=null;
Private datainputstream input=null;
Private dataoutputstream output=null;
Public client(string address,int port){
Try{
Socket =new socket(address,port);
Sop(“connected”);
Input=new datainputstream(system.in);
Out=new dataoutputstream(socket.getoutputstream());}
Catch(unknownHostException u){
Sop(u);}
Catch(IOException i){
Sop(i);}
String line=””;
While(!line.equals(‘over”)){
Try{
Line=input.readline();
Out.writeUTF(line);}
Catch(IOException i){
Sop(i);}}
Try
{input.close();
Out.close();
Socket.close();}
Catch(IOException i){
Sop(i);}}
Public static void n=amin(stringargs[]){
Client client =new client(127.0.0.1”,5000);}}
conculsion
 Programing client and server applications in
java is fun and challenging.
 Programming socket programming injava is
much easier than doing it in other languages
such as C.
 Key words:
 Client,servers,TCP/IP,port,number,sockets, java
sockets.

More Related Content

What's hot

Page replacement algorithms
Page replacement algorithmsPage replacement algorithms
Page replacement algorithms
Piyush Rochwani
 

What's hot (20)

Osi model
Osi modelOsi model
Osi model
 
Food donation project report II
Food donation project report IIFood donation project report II
Food donation project report II
 
Transport layer
Transport layer Transport layer
Transport layer
 
Introduction to oops concepts
Introduction to oops conceptsIntroduction to oops concepts
Introduction to oops concepts
 
Decision properties of reular languages
Decision properties of reular languagesDecision properties of reular languages
Decision properties of reular languages
 
Transport layer
Transport layerTransport layer
Transport layer
 
RPC: Remote procedure call
RPC: Remote procedure callRPC: Remote procedure call
RPC: Remote procedure call
 
Issues in Data Link Layer
Issues in Data Link LayerIssues in Data Link Layer
Issues in Data Link Layer
 
Osi reference model
Osi reference modelOsi reference model
Osi reference model
 
Java Server Pages(jsp)
Java Server Pages(jsp)Java Server Pages(jsp)
Java Server Pages(jsp)
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Communication primitives
Communication primitivesCommunication primitives
Communication primitives
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Data link layer
Data link layer Data link layer
Data link layer
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-Systems
 
Two pass Assembler
Two pass AssemblerTwo pass Assembler
Two pass Assembler
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 
Page replacement algorithms
Page replacement algorithmsPage replacement algorithms
Page replacement algorithms
 
Chat Application
Chat ApplicationChat Application
Chat Application
 

Similar to Tcp/ip server sockets

Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
Kavita Sharma
 
Socket programming
Socket programmingSocket programming
Socket programming
harsh_bca06
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
Sasi Kala
 

Similar to Tcp/ip server sockets (20)

Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
Sockets
SocketsSockets
Sockets
 
Socket Programming it-slideshares.blogspot.com
Socket  Programming it-slideshares.blogspot.comSocket  Programming it-slideshares.blogspot.com
Socket Programming it-slideshares.blogspot.com
 
Java 1
Java 1Java 1
Java 1
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Basic Networking in Java
Basic Networking in JavaBasic Networking in Java
Basic Networking in Java
 
A.java
A.javaA.java
A.java
 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
 
Java networking
Java networkingJava networking
Java networking
 
Java
JavaJava
Java
 
Java networking
Java networkingJava networking
Java networking
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Socket
SocketSocket
Socket
 
Lab manual cn-2012-13
Lab manual cn-2012-13Lab manual cn-2012-13
Lab manual cn-2012-13
 

More from rajshreemuthiah (20)

oracle
oracleoracle
oracle
 
quality
qualityquality
quality
 
bigdata
bigdatabigdata
bigdata
 
polymorphism
polymorphismpolymorphism
polymorphism
 
solutions and understanding text analytics
solutions and understanding text analyticssolutions and understanding text analytics
solutions and understanding text analytics
 
interface
interfaceinterface
interface
 
Testing &ampdebugging
Testing &ampdebuggingTesting &ampdebugging
Testing &ampdebugging
 
concurrency control
concurrency controlconcurrency control
concurrency control
 
Education
EducationEducation
Education
 
Formal verification
Formal verificationFormal verification
Formal verification
 
Transaction management
Transaction management Transaction management
Transaction management
 
Multi thread
Multi threadMulti thread
Multi thread
 
System testing
System testingSystem testing
System testing
 
software maintenance
software maintenancesoftware maintenance
software maintenance
 
exception handling
exception handlingexception handling
exception handling
 
e governance
e governancee governance
e governance
 
recovery management
recovery managementrecovery management
recovery management
 
Implementing polymorphism
Implementing polymorphismImplementing polymorphism
Implementing polymorphism
 
Buffer managements
Buffer managementsBuffer managements
Buffer managements
 
os linux
os linuxos linux
os linux
 

Recently uploaded

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Simplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptxSimplifying Mobile A11y Presentation.pptx
Simplifying Mobile A11y Presentation.pptx
 
Decarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational PerformanceDecarbonising Commercial Real Estate: The Role of Operational Performance
Decarbonising Commercial Real Estate: The Role of Operational Performance
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Navigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern EnterpriseNavigating Identity and Access Management in the Modern Enterprise
Navigating Identity and Access Management in the Modern Enterprise
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 

Tcp/ip server sockets

  • 1. R.Pavithra Department of CS&IT Nadar saraswathi college of arts & science, theni.
  • 2. contents  Introduction Sockets  Server Socket And Client Socket  Tcp/ Ip In Java  Types Of Sockets In Java  Tcp/Ip Server Socket  Tcp/Ip Client Socket  Conculsion
  • 3. Introduction sockets  The term socket programming refers to writing programs that execute across multiple computers in which the devices are all connected to each other using a network.  There are two communication protocols that one can use for socket programming :User datagram protocol(UDP)and Transfer control protocol(TCP)
  • 4. Server socket and client socket  A server socket keeps on listening to any incoming connections from any of the clients.  When you create a client socket, you simply specify the server address and port. You never bind or listen using a client socket.  You simply connect a client socket . On the other hand you dont connect using a server socket.
  • 5. TCP/IP in java  Accessing TCP/IP from java is Straightforward. The main functionality is in the following classes:  Java.net.InetAddress: Represents an IP address( either IPV4or IPV6) and has methods for performing DNS lookup( next slide).  Java.net.Socket: Represents a TCP socket.  Java.net.ServerSocket: Represents a server socket which is capable of waiting for requests from clients.
  • 6. Types of sockets in java  Java provides three different types of sockets connection- oriented(TCP) sockets are implemented with the socket class.  Connectionless(UDP) sockets uses the Datagram Socket class. A third type is the Multicast Socket class, which is a subclass of the datagram Socket class.  Server  client
  • 7. TCP/IP SERVER SOCKETS  A socket in java is one endpoint of two-way communication link between two programs runnning 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 to .  An endpoint is a combination of an IP address and a port number.
  • 8. continue syntax descriptions Server Socket( int port ) throws IOException Creates server socket on the specified port with a queue length of 50 Server Socket( int port, int maxQueue) Throws IOException Creates a server socket on the specified port with a maximum queue length of maxQueue. Server Socket( it port, int maxQueue,inetAddress local Address) Throws IOException Creates a server socket on the specified port with a maximum queue length of maxQueue, on a multihomed host, local Address specifies the ip address to which this socket binds.
  • 9. Example program for server socket Import java.net.*; Import java.io.*; Public class serverside{ Private Socket socket=null; Private serversocket =null; Sop(“server started”); Sop(“waiting for client”); Socket=server.accept(); Sop(“client accepted”); In=new dataintputstream ( New bufferedinputstream(socket.getInputstream())); String line=””; While(!line.equals(‘over”)){
  • 11. TCP/IP CLIENT SOCKET  TCP/IP client sockets. TCP/IP sockets are used to implement reliable, bidirectional, persistent, point-to- point, stream-based connections between hosts on the internet. A socket can be used to connect java’s I/O system to other programs that may reside either on the local machine or on any other machine on the internet.
  • 12.
  • 13. syntax description InetAddress getInetAddress() Returns the InetAdd associated with the socket object .It returns null If the socket is not connected. Intgetport() Returns the remote port to which the invoking socket object is connected . It returns O if the socket is not connected. Intgetlocalport() Returns the local port to which the invoking socket object is bound. It returns -1if the socket is not bound.
  • 14. Continue……  You can gain access I/P and O/P streams associated with socket by use of  Get I/P stream()  Get O/P stream()  Each can throw an IOException if the socket has been invalidated by a loss of connections. syntax description inputstream getI/Pstream() Throws IOException Returns the inputs stream associated with the invoking socket Outputstream get O/P stream() throws IOException Returns the outputs stream associated with the invoking socket
  • 15. Other method Syntax Description “including connect()” Which allows you to specify a new connections “is connected()” Which returns true if the socket is connected to server. “is bound()” Which returns if the socket is bound to an address “is closed()” Which returns true if the socket is closed.
  • 16. Example program client socket Import java.net.*; Import java.io.*; Public class clientprogram{ Private Socket socket=null; Private datainputstream input=null; Private dataoutputstream output=null; Public client(string address,int port){ Try{ Socket =new socket(address,port); Sop(“connected”); Input=new datainputstream(system.in); Out=new dataoutputstream(socket.getoutputstream());} Catch(unknownHostException u){
  • 17. Sop(u);} Catch(IOException i){ Sop(i);} String line=””; While(!line.equals(‘over”)){ Try{ Line=input.readline(); Out.writeUTF(line);} Catch(IOException i){ Sop(i);}} Try {input.close(); Out.close(); Socket.close();} Catch(IOException i){ Sop(i);}} Public static void n=amin(stringargs[]){ Client client =new client(127.0.0.1”,5000);}}
  • 18. conculsion  Programing client and server applications in java is fun and challenging.  Programming socket programming injava is much easier than doing it in other languages such as C.  Key words:  Client,servers,TCP/IP,port,number,sockets, java sockets.