Docsity

Streams and Sockets-Java Programming-Lecture Slides, Slides of Java Programming

Partial preview of the text

Download Streams and Sockets-Java Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

ava - programming

Introduction^ An

Streams and Sockets

J

Today’s Lecture

  • Streams

 Concept

 Examples

 Simple streams

 File Streams

 Data Streams

  • Sockets

 Concept

 Examples

InetAddress Class

  • The InetAddress class encapsulates Internet addresses.

 Supports both numeric IP addresses and hostnames.

 No Public variable or Constructor i.e. the class cannot be instantiated

  • Important Methods

 getLocalHost()

 Returns an InetAddress object representing the Internet address

of the local host computer

 getByName()

 Returns an InetAddress object for a specified host

 getHostAddress()

 Reurns the IP address of the host identified by the InetAddress

object (string)

 getHostName()

 Returns domain name of host identified by the InetAddress Object

Example: Getting an Instance of InetAddress

import java.net.*; public class InetTest { public static void main(String args[]) { try { //getting the Inet Object for a host InetAddress host = InetAddress.getByName("home"); }catch(UnknownHostException ex) { System.out.println("Unknown host"); return; } } }

The Socket Class

  • The Socket class implements client connection-based sockets

 Used for connection oriented services

  • Important Methods

 getInetAddress()

 Returns an object of the InetAddress class for the

remote/destination host to which socket is connected

 getPort()

 Returns the port number of the remote/destination host to which

socket is connected

 getLocalPort()

 Returns the source host local port number associated with the

socket.

 getInputStream(),getOutputStream()

 Are used to access the input and output streams associated with a

socket.

 Close() method is used to close a socket

Opening a Client Socket

import java.net.*;

public class socketTest {

public socketTest() { }

public static void main(String s[]){

try{

Socket conn=new Socket("home",5217);

}catch(Exception e){e.printStackTrace();}

}//end of class

Communicating with socket

  • Each socket provides an input and output stream bound to it
  • Open Input and Output streams

 Have to import java.io package for streams

Opening Streams of Client Socket

  • Import java.net.*;
  • public class socketTest {
  • public socketTest() { }
  • public static void main(String s[]){
  • try{
  • InputStream ins;
  • OutputStream outs;

Socket conn=new Socket("home",5217);

  • ins=conn.getInputStream();
  • outs=conn.getOutputStream();
  • }catch(Exception e){e.printStackTrace();}
  • }}//end of class
  • It is difficult to work with byte streams, better to use some formatting

stream

Reading and Writing to sockets

  • After getting the input and output stream use standard methods of streams to read and write
  • Read data

 To read the data from streams, input streams are used

 Any data received by the socket will be available at input stream

  • To write data to streams output streams are used

 All data is written to streams will be passed to the socket