creating and using sockets

Upload: vinitha-arun

Post on 09-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/8/2019 Creating and Using Sockets

    1/11

    CREATING AND USINGJAVA SOCKETS:

    By P.Vinitha

  • 8/8/2019 Creating and Using Sockets

    2/11

    Sockets and Ports

    In order tocommunicate with a remote host the

    Java client must first create a Socket, which will

    establish the TCP connection. Whileestablishing theconnection a host name

    and port numbermust be specified.

    Theremust be a server actively listening on the

    specified port or theconnection will fail with

    IOException.

  • 8/8/2019 Creating and Using Sockets

    3/11

    Sockets

    The java.net.Socket class allows us to

    perform all four fundamental socket

    operationsconnecting toremotemachines

    sending data

    receiving dataclosing theconnection.

  • 8/8/2019 Creating and Using Sockets

    4/11

  • 8/8/2019 Creating and Using Sockets

    5/11

  • 8/8/2019 Creating and Using Sockets

    6/11

    Constructors of java.net.Socket class

    Public Socket() throws IOException

    Creates an unbound server socket.

    When using this constructor, use theconnect() method when you are ready

    to bind the server socket

  • 8/8/2019 Creating and Using Sockets

    7/11

    Methods of Socket Class

    public void connect(SocketAddress host,

    int timeout) throws IOException

    This method connects the socket to thespecified host. This method is needed only

    when you instantiated the Socket using the

    no-argument constructor.

    public InetAddress getInetAddress()

    This method returns the address of the other

    computer that this socket is connected to.

  • 8/8/2019 Creating and Using Sockets

    8/11

    Methods of Socket Class

    public int getPort()

    Returns the port the socket is bound toon the

    remotemachine. public int getLocalPort()

    Returns the port the socket is bound toon the

    localmachine.

  • 8/8/2019 Creating and Using Sockets

    9/11

    Methods of Socket Class

    public SocketAddress

    getRemoteSocketAddress()

    Returns the address of theremote socket. public InputStream getInputStream() throws

    IOException

    Returns the input streamof the socket. The inputstream is connected to theoutput streamof the

    remote socket.

  • 8/8/2019 Creating and Using Sockets

    10/11

    Methods of Socket Class

    public OutputStream getOutputStream()

    throws IOException

    Returns theoutput streamof the socket. Theoutput stream is connected to the input

    streamof theremote socket

    public void close() throws IOExceptionCloses the socket, which makes this Socket

    object nolongercapableof connecting

    again to any server.

  • 8/8/2019 Creating and Using Sockets

    11/11

    Program to read a webpage from a

    website

    import java.net.*;

    import java.io.*;

    class readurl {

    public static void main(String a[])throws Exception {

    BufferedReader br1=new BufferedReader(new

    InputStreamReader(System.in));String filename;

    System.out.print("Enter the webpage address:");filename=br1.readLine();

    URL myurl=new URL(filename);

    InputStream in=myurl.openStream();

    BufferedReader br=new BufferedReader(newInputStreamReader(in));

    String str;while((str=br.readLine())!=null)

    System.out.println(str);

    br.close();

    }}