Professional Documents
Culture Documents
· Once connection is established, the local and remote hosts exchanges information. Both client and server
can receive and send data simultaneously.
· When the transmission is complete, one or both side can close the connection.
The method of Socket class is used to establish and terminate connection. This class provides streams to the
programmer.
Constructors
Socket class has 4 constructors. In these constructors, hosts can be specified as an InetAddress or a string
and ports are always specified as int values ranging from 0 to 65,535.
A TCP socket to the specified port on the specified host will be created and attempt to connect to a remote
host is being made.
UnknownException- If DNS can’t resolve the host name then this exception will be thrown.
IOException- If the socket can’t be opened for some other reason this exception will occur.
import java.net.*;
import java.io.*;
If(a.length>0)
host=a0];
for (int i=1;i<1024;i++)
try
InetAddress addr=InetAddress.getByName(host);
for(int i=1024;i<65536;i++)
try{
catch(UnknownHostException ie)
System.out.println(ie);
catch(IOException e)
{
System.out.println(e);
This constructor creates a TCP socket to the specified port on the specified host.
IOException- If the socket can’t be opened for some other reason this exception will occur.
import java.net.*;
import java.io.*;
String host=’localhost’;
If(a.length>0)
host=a0];
for (int i=1;i<1024;i++)
try
InetAddress addr=InetAddress.getByName(host);
for(int i=1024;i<65536;i++)
try{
catch(IOException ie)
System.out.println(ie);
catch(Exception e)
System.out.println(e);
3) public Socket(String host, int port, InetAddress intref, int localport)throws IOException
Localport-used to choose from the available port between 1024 and 65,535. It takes the value 0.
IOException- If the socket can’t be opened for some other reason this exception will occur.
4) public Socket(InetAddress host, int port, InetAddress intref, int localport)throws IOException
This constructor is used to try to connect to be specified host by providing InetAddress instead of host name
as string.
5) ProtectedSocket()
6) ProtectedSocket(SocketImpl impl)
This will also create a new socket without connecting it. But it implements SocketImpl object. If SocketImpl
object is not needed then null can be passed to the constructor.
Getting Socket Information
Socket object has only one field, a SocketImpl which has native field code for all the fields of Socket.
public InetAddress getInetAddress() – Tells to which remote host the socket is connect to or connection is
now closed.
public int getPort() – Tells to which port the socket is connected to on the remote host.
public int getLocalPort() – To find the port number of the local end of connection.
public InetAddress getLocalAddress() – To find the network interface to which the socket is bounded.
public InputStream getInputStream() throws IOException – return an input stream that can read data from
the socket into a program.
public OutputStream getOutputStream() throws IOException – Return a row OutputStream for writing
data from application to the other end of the socket.
· The ServerSocket() listens for the connection with the help of accept() method, which will return a socket
object when a client attempts to make a connection.
Constructors
It will create a java socket server on the port specified by the argument. If 0 is passed the available ports
between 1024 and 65,535 will be selected. The argument ports are the ports selected by the System.
Create a java socket server (ServerSocket) on the specified port with queueLength for incoming
connections.
Create a java socket server( ServerSocket) on the specified port with queueLength for incoming requests and
in addition it binds the ServerSocket only to the specified IP address. This is useful for Servers that run on
Systems with several IP address.
accept() method
Block or stops the flow of execution and waits until a client connects when a client is connected. This
method returns a Socket object getInputStream() and getOutputStream methods can be used to exchange
data between client and server.
Example:
While(true)
out.Writer(“connected to server”);
con.close();
close() method
Free up port for other connections. It allows another java socket service to bind to the port. It can close itself
automatically when a program dies.
get() method
1. getInetAddress()
try
{
InetAddress add=SS.getInetAddress();
2. getlocalPort()
Returns a port which is being listened. This can be used in peer to peer multi socket program.
try
Socket Option
The only socket option supported by ServerSocket class is So-TIMEOUT which is the amount of time in
milliseconds that accept() waits for incoming connection before throwing an interrupted IOException. It is 0
then accept() will never time out.
Methods
Sets the So-TIMEOUT field for Server Socket object and the countdown starts when accept() is invoked.
importjava.io.*;
importjava.net.*;
class ftpclient
{
Socket client;
PrintStream ps;
String fn;
Int i=0;
try
Client=new Socket(InetAddress.getByName(“127.0.0.1”,3330);
ps=new PrintStream(client.getOutputStream(),true);
Line=ds.readLine();
System.out.println(“File Transferring…”);
dos.writeCars(line);
System.out.println(“File tTransferred…”);
Catch(Exception e)
System.out.println(e);
Server Program:
import java.io.*;
import java.net.*;
class FTPServer
ServerSocket server;
Socket con;
try
con=server.accept();
System.out.println(“Connection established”);
String str1=dis.readLine();
System.out.println(“Client”+str1);
While(msg=disf.reaadLine()!=null)
con.close();
Catch(Exception e)
{
System.out.println(e);