You are on page 1of 7

Socket programming

A socket is one endpoint of a two-way communication link between two programs running on
the network. A socket is bound to a port number so that the TCP layer can identify the
application that data is destined to be sent to.

An endpoint is a combination of an IP address and a port number. Every TCP connection can be
uniquely identified by its two endpoints. That way you can have multiple connections between
your host and the server.

Server:
a server runs on a specific computer and has a socket that is bound to a specific port
number. The server just waits, listening to the socket for a client to make a connection
request.

1. Open a server socket

2. Wait for a client

3. Accept the connection -> Open a new socket for the client

1. Open an input stream and output stream to the socket for the client.

2. Read from and write to the stream according to the server's protocol.

3. Close the streams.

4. Close the socket for the client.

Client:
The client knows the hostname of the machine on which the server is running and the port
number on which the server is listening.

1. Open a socket.

2. Open an input stream and output stream to the socket.

3. Read from and write to the stream according to the server's protocol.

4. Close the streams.

5. Close the socket.


On the client side, if the connection is accepted, a socket is successfully created and the client
can use the socket to communicate with the server. The client and server can now communicate
by writing to or reading from their sockets.

*************************************************************************************

java.net package
ServerSocket class: implements a socket that servers can use to listen for and accept
connections to clients

new ServerSocket(int port)

Creates a server socket, bound to the specified port

new ServerSocket(int port, int backlog)

Creates a server socket and binds it to the specified local port number, with the
specified backlog

The maximum queue length for incoming connection indications (a request to connect)
is set to the backlog parameter

If a connection indication arrives when the queue is full, the connection is refused

Socket class: implements one side of a two-way connection between your Java program and
another program on the network

new Socket(String host, int port)

Creates a stream socket and connects it to the specified port number on the named host

*************************************************************************************
Client Side , opening socket
try {

Socket Client_Socket;

Client_Socket = new Socket("Machine name", PortNumber);

catch (Exception e) {

System.out.println(e);

}
When implementing a server you also need to create a socket object from the ServerSocket in
order to listen for and accept connections from clients.

try {

Socket clientSocket = null;

serviceSocket = MyService.accept();

catch (Exception e) {

System.out.println(e);

}
Server side, opening socket

try {

ServerSocket Server_Socket;

Server_Socket = new ServerSocket(PortNumber);

catch (Exception e) {

System.out.println(e);

}
*************************************************************************************
creating input stream on client side

try {

DataInputStream input;

input = new DataInputStream(MyClient.getInputStream());

catch (Exception e) {

System.out.println(e);

}
The class DataInputStream allows you to read lines of text and Java primitive data types in a
portable way.

*************************************************************************************
On the server side, we can use DataInputStream to receive input from
the client
try {

DataInputStream input;

input = new DataInputStream(serviceSocket.getInputStream());

catch (Exception e) {

System.out.println(e);

*************************************************************************
************

On the client side, we can create an output stream to send information to


the server socket using the class PrintStream or DataOutputStream of
java.io

try {

PrintStream output;

output = new PrintStream(MyClient.getOutputStream());

catch (Exception e) {

System.out.println(e);

}
The class PrintStream has methods for displaying textual representation of Java primitive data
types.
or we can do:

try {

DataOutputStream output;

output = new DataOutputStream(MyClient.getOutputStream());

catch (Exception e) {

System.out.println(e);

The class DataOutputStream allows you to write Java primitive data types; many of its methods
write a single Java primitive type to the output stream.

On the server side, you can use the class PrintStream to send
information to the client.

PrintStream output;

try {

output = new PrintStream(serviceSocket.getOutputStream());

catch (IOException e) {

System.out.println(e);

*************************************************************************************
******
closing socket
always close the output and input stream before closing the socket

On the client side:


try {

output.close();

input.close();

MyClient.close();

catch (Exception e) {

System.out.println(e);

On the server side:


try {

output.close();

input.close();

serviceSocket.close();

MyService.close();

catch (Exception e) {

System.out.println(e); }

You might also like