0% found this document useful (0 votes)
29 views

Creating and Using Sockets

This document discusses how to create and use Java sockets to connect to remote hosts. It explains that a Java client must first create a Socket, which requires specifying a host name and port number. The Socket class allows connecting to remote machines, sending data, receiving data, and closing the connection. Constructors are provided to create Sockets and connect to specified hosts and ports. Methods allow getting connection details, streams for input/output, and closing the socket. An example program is given to read a webpage using a URL Socket.

Uploaded by

Vinitha Arun
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Creating and Using Sockets

This document discusses how to create and use Java sockets to connect to remote hosts. It explains that a Java client must first create a Socket, which requires specifying a host name and port number. The Socket class allows connecting to remote machines, sending data, receiving data, and closing the connection. Constructors are provided to create Sockets and connect to specified hosts and ports. Methods allow getting connection details, streams for input/output, and closing the socket. An example program is given to read a webpage using a URL Socket.

Uploaded by

Vinitha Arun
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

CREATING AND USING

JAVA SOCKETS:
By P.Vinitha
Sockets and Ports
 In order to communicate with a remote host
the Java client must first create a Socket,
which will establish the TCP connection.
 While establishing the connection a host name

and port number must be specified.


 There must be a server actively listening on

the specified port or the connection will fail


with IOException.
Sockets
 The java.net.Socket class allows us to
perform all four fundamental socket
operations
connecting to remote machines
sending data
receiving data
closing the connection.
Constructors of java.net.Socket class

 public Socket(String host, int port)


throws UnknownHostException,
IOException
 public Socket(InetAddress address, int

port) throws IOException


 These constructors create a Socket and
connect to the specified host and port. Host
can be a host name or IP address and port
must be in a range of 1-65535.
Constructors of java.net.Socket class

 public Socket(String host, int port,


InetAddress localAddress,int
localPort) throws IOException
 public Socket(InetAddress address, int

port, InetAddress localAddress, int


localPort) throws IOException
 These constructors create a socket and
connect it to the specified remote host on
the specified remote port.
Constructors of java.net.Socket class

 Public Socket() throws


IOException
 Creates an unbound server socket.
When using this constructor, use the
connect() method when you are ready
to bind the server socket
Methods of Socket Class
 public void connect(SocketAddress
host, int timeout) throws IOException
 This method connects the socket to the
specified 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.
Methods of Socket Class
 public int getPort()
 Returns the port the socket is bound to on
the remote machine.
 public int getLocalPort()
 Returns the port the socket is bound to on
the local machine.
Methods of Socket Class
 public SocketAddress
getRemoteSocketAddress()
 Returns the address of the remote socket.
 public InputStream getInputStream()
throws IOException
 Returns the input stream of the socket. The input
stream is connected to the output stream of the
remote socket.
Methods of Socket Class
 public OutputStream
getOutputStream() throws
IOException
 Returns the output stream of the socket. The
output stream is connected to the input
stream of the remote socket
 public void close() throws IOException
 Closes the socket, which makes this Socket
object no longer capable of connecting again
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(new
InputStreamReader(in));
String str;
while((str=br.readLine())!=null)
System.out.println(str);
br.close();
}
}

You might also like