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.

Tcp/ip server sockets

  • 1.
    R.Pavithra Department of CS&IT Nadarsaraswathi 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  Theterm 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 andclient 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 socketsin 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 forserver 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”)){
  • 10.
  • 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.
  • 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 cangain 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 “includingconnect()” 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 clientsocket 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(IOExceptioni){ 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 clientand 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.