Sockets - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Sockets

Description:

IOException must be caught that is thrown by. Socket Constructor ... IOException is thrown by. Constructor of ServerSocket .accept( ) Method. ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 28
Provided by: bas44
Category:

less

Transcript and Presenter's Notes

Title: Sockets


1
Sockets
  • How-to

2
Ingredients of communication
  • For a communication application to work, there
    are two basic ingredients
  • Both of them should be present to make a sensible
    communication

3
Ingredients of communication
  • Ingredients are
  • An established communication link
  • A protocol

4
Communication link
  • We will use a client-server socket based
    connection between two machines as a link

5
Protocol
  • We will write a simple sample protocol to learn
    how protocols work

6
What is a Socket
  • Socket is a logical entity that can be used for
    following basic operations
  • Connect to a remote machine
  • Send data
  • Receive data
  • Close a connection
  • Bind to a port
  • Listen to incoming connections
  • Accept Connections from remote machine on bound
    port

7
Socket Architecture
Server
Client
Socket
Socket
8
Server Socket
  • Server Socket is bound to an port on server
  • Then it waits for connections from server
  • Whenever a client requests, it accepts a
    connection

Server
Socket
Socket
9
Client Socket
  • Client socket connects to the server socket
  • Then it can start sending and receiving data
    according to the protocol

10
Writing a Client Application
  • Using java.net.Socket

11
Writing a Socket
  • Socket is a java class found in java.net package
    and represents the logical socket we just
    discussed
  • Socket can be instantiated using one of the
    following constructors
  • Socket(String host, int port)
  • Socket(InetAddress address, int port)

12
Socket(String host, int port)
  • First argument to Socket constructor is the
    host-name address of the SERVER that is waiting
    for the clients to connect to it
  • Second argument is the port number on which
    server is waiting

13
A sample protocol
  • Client Request
  • Hello
  • name
  • date
  • Anything else
  • Server Response
  • Hi, how are you?
  • Machine name is ____
  • Server Date/Time
  • Server Error

14
Client Program for sample protocol
  • Socket s new Socket("basit", 1357)
  • System.out.println("Connected to basit on port
    1357")
  • InputStream in s.getInputStream()
  • OutputStream out s.getOutputStream()
  • PrintStream write new PrintStream( out )
  • DataInputStream read new DataInputStream(in)
  • write.println("name") //REQUEST
  • String temp read.readLine() //READING
    RESPONSE
  • System.out.println("Response for name is "
    temp)

Server
Protocol Command
15
Output of above program
  • Connected to basit on port 1357
  • Response for name is Machine name is
    BASIT/192.168.12.54

16
Exceptions to be caught
  • Socket constructor throws UnknownHostException if
    the server is not accessible to the client
    machine
  • IOException must be caught that is thrown by
  • Socket Constructor
  • getInputStream and getOutputStream methods
  • Other input and output methods

17
Where is the server?
  • If protocol is designed by us, we should write
    the server
  • But where is the server?
  • It is running on the machine 192.168.12.54

18
Try out the client first
19
Writing the Server Application
  • Using java.net.ServerSocket

20
Writing the server
  • Following steps are typically followed for the
    server to function
  • Create a ServerSocket that binds a Socket to a
    port on the server machine
  • Wait for the incoming client connections
  • When a connection is established, open streams
    for I/O on the socket
  • Read the request from incoming stream
  • Write back the response to the output stream

21
ServerSocket
  • ServerSocket is a class found in java.net package
  • It is used to bind a socket to a server port and
    wait for the incoming connections from the client

22
Server Code Creating Server Socket
  • ServerSocket ss new ServerSocket(1357)
  • System.out.println("Waiting on port 1357")
  • Socket s ss.accept()
  • System.out.println("Client connection
    established")

23
Server Code Reading Request
  • InputStream in s.getInputStream()
  • OutputStream out s.getOutputStream()
  • DataInputStream read new DataInputStream(in)
  • PrintStream write new PrintStream( out )
  • // reading request
  • String temp read.readLine()

24
Server Code Sending response
  • String temp read.readLine()// reading
    request
  • if(temp.equals("Hello")) //writing back
    response
  • write.println("Hi, how are you?")
  • else if (temp.equals("name"))
  • write.println("Machine name is
    "InetAddress.getLocalHost())
  • else if (temp.equals("date"))
  • write.println("Machine time is " new
    Date())
  • else
  • write.println("ERROR from server
    InetAddress.getLocalHost(), bad command")
  • s.close()

25
Look for the Exceptions
  • IOException is thrown by
  • Constructor of ServerSocket
  • .accept( ) Method
  • .getInputStream and getOutputStream methods
  • And must be caught

26
Protocol
  • You can design your own protocols and make them
    work for your purpose
  • You can modify a few commands to the existing
    server to check for your protocol design
    capabilities

27
END OF TODAYs SESSION
Write a Comment
User Comments (0)
About PowerShow.com