Download Socket Programming PDF

TitleSocket Programming
TagsTransmission Control Protocol Network Packet Port (Computer Networking) Network Socket Network Congestion
File Size204.1 KB
Total Pages17
Document Text Contents
Page 8

[8]




Compiled By: Sapna Saxena, Lecturer ,Chitkara University

}
catch(IOException ioe)
{
System.out.ptintln(“Trouble: “ + ioe.getMessage());
}
catch(SecurityException se)
{
System.out.ptintln(“Trouble: “ + se.getMessage());
}
}
}

Reading - Writing Socket

Connection oriented sockets are an abstraction for a byte-stream communication between two
network programs. Therefore the most important methods of the Socket class are the ones
that permit users to actually use instances for communication. The Socket class provides two
methods, one for obtaining an input stream for reading bytes and one for obtaining an output
stream for writing byte-stream to the output stream. The streams are represented as
java.io.InputStream and java.io.OutputStream instances encapsulating the
communication stream. An exception java.io.IOException will be thrown in this case. The
signatures are –

java.io.InputStream getInputStream() throws IOException;

java.io.OutputStream getOutputStream() throws IOException;

Once the input and output streams for the socket have been obtained, it is up to application to
determine the contents of communication.

Getting Socket Information

Connection oriented sockets encapsulate a connection to a network peer or remote host,
therefore, they may be characterized by set:

<local_address, local_port, remote_address, remote_port>

The four methods for querying the Socket local and remote addresses and port numbers are –

InetAddress getInetAddress() throws IOException;

int getPort() throws IOException;

InetAddress getLocalAddress() throws IOException;

int getLocalPort() throws IOException;

Terminating socket

As file objects also consume finite operating system resources. An open socket keeps a local
port busy, and in most cases uses at least one native operating system file handle. Both port
numbers as well as file handlers are limited and therefore it is very important to explicitly

Page 16

[16]




Compiled By: Sapna Saxena, Lecturer ,Chitkara University

catch(SecurityException se)
{

System.out.println(“Trouble : ” + se.getMessage());
}
catch(IOException ioe)
{

System.out.println(“Trouble : ” + ioe.getMessage());
}

Server Socket is a java.net.class that provides a system independent implementation of the
server side of a client – server socket connection. The constructor of Server Socket throws an
exception if it can’t listen on the specified port. (for example, the port is already being used.
In this case, the Echo Server has to exit.

If the server successfully connects to its port, then the Server Socket object is successfully
created and the server continues to the next step – accepting a connection from a client.

try
{
clientSocket = serverSocket.accept();

} catch(SecurityException se)
{

System.out.println(“Trouble : ” + se.getMessage());
}
catch(IOException ioe)
{

System.out.println(“Trouble : ” + ioe.getMessage());
}

The accept() method waits until a client starts up and requests a connection on the host and
port of server. When a connection is requested and successfully established, the accept
method returns a new socket object which is bound to a new port. The server can
communicate with the client over this new socket and continue to listen for client connection
requests on the Server Socket bound to the original, predetermined port.

After the server successfully establishes a connection with a client, it communicates with the
client using this code:

Print Writer out = new Print Writer (client Socket.get out put stream() );

Buffer Reader in = new Buffered Reader (new Input stream Reader

(Client Socket.get Input stream() );

The first statement here gets the client socket’s output stream and opens Print Writer on it.
Similarly in the next statement gets the client Socket’s input stream and opens a Buffered
Reader on it.

String inputLine;
boolean finished = true;

Similer Documents