[go: up one dir, main page]

0% found this document useful (0 votes)
11 views32 pages

Using Sockects For Both TCPIP & UDP

A socket is a connection between two hosts that allows them to communicate over a network. There are two main types of sockets - TCP sockets which provide a connection-oriented link and UDP sockets which are connectionless. To create a server, a server socket listens on a port for client connections. For each connection, input/output streams are used to send and receive data with the client. To create a client, a socket connects to the server's IP address and port, and also uses input/output streams to communicate.

Uploaded by

wajiha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views32 pages

Using Sockects For Both TCPIP & UDP

A socket is a connection between two hosts that allows them to communicate over a network. There are two main types of sockets - TCP sockets which provide a connection-oriented link and UDP sockets which are connectionless. To create a server, a server socket listens on a port for client connections. For each connection, input/output streams are used to send and receive data with the client. To create a client, a socket connects to the server's IP address and port, and also uses input/output streams to communicate.

Uploaded by

wajiha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

SOCKETS

• A socket is a connection between two hosts.


• It can perform seven basic operations:
• Connect to a remote machine
• Send data
• Receive data
• Close a connection
• Bind to a port
• Listen for incoming data
• Accept connections from remote machines on the bound port

Zulfiqar Ali, CIIT Islamabad 1


Using Sockets
• different processes (programs) can communicate with
each other across networks by means of sockets.
• Java implements both TCP/IP sockets and UDP sockets.
• Very often, the two communicating processes will have a
client/server relationship.

• The steps required to create client/server programs are:

Zulfiqar Ali, CIIT Islamabad 2


TCP Sockets
• A communication link created via TCP/IP sockets is a
connection-orientated link.
• This means that the connection between server and client
remains open throughout the duration of the dialogue between
the two and is only broken (under normal circumstances)
• when one end of the dialogue formally terminates the
exchanges (via an agreed protocol).

Zulfiqar Ali, CIIT Islamabad 3


SERVER SIDE
• Since there are two separate types of process involved (client
and server), we shall examine them separately,

• Taking the SERVER first.


• Setting up a server process requires five steps...

Zulfiqar Ali, CIIT Islamabad 4


1. Create a ServerSocket object.

• The ServerSocket constructor requires a port number (1024-65535, for


non-reserved ones) as an argument. For example:
ServerSocket servSock = new ServerSocket(1234);

In this example, the server will await ('listen for') a connection from a
client on port 1234.

Zulfiqar Ali, CIIT Islamabad 5


2. Put the server into a waiting state.

• The server waits indefinitely ('blocks') for a client to connect.


• It does this by calling method accept() of class ServerSocket, which
returns a Socket object when a connection is made.
• For example:

Socket link = servSock.accept();

Zulfiqar Ali, CIIT Islamabad 6


3. Set up input and output streams.
• Methods getInputStream and getOutputStream of class Socket are used to
get references to streams associated with the socket returned in step 2.
• These streams will be used for communication with the client that has just
made connection.
• For a non-GUI application, we can wrap a Scanner object around the
InputStream object returned by method getInputStream, in order to obtain
string-orientated input (just as we would do with input from the standard
input stream, System.in).
• For example:
Scanner input = new Scanner(link.getInputStream());

Zulfiqar Ali, CIIT Islamabad 7


• Similarly, we can wrap a PrintWriter object around the OutputStream
object returned by method getOutputStream.
• Supplying the PrintWriter constructor with a second argument of true will
cause the output buffer to be flushed for every call of println (which is
usually desirable).
• For example:

PrintWriter output = new PrintWriter(link.getOutputStream(),true);

Zulfiqar Ali, CIIT Islamabad 8


4. Send and receive data.
• Having set up our Scanner and PrintWriter objects, sending and receiving
data is very straightforward.
• We simply use method nextLine for receiving data and method println
for sending data, just as we might do for console I/O.
For example:

output.println("Awaiting data...");
String input = input.nextLine();

Zulfiqar Ali, CIIT Islamabad 9


5. Close the connection (after completion of the dialogue).

• This is achieved via method close of class Socket. For example:


link.close();

• The following example program is used to illustrate the use of these steps.

Zulfiqar Ali, CIIT Islamabad 10


CLIENT SIDE
• Setting up the corresponding client involves four steps...
1. Establish a connection to the server.

• We create a Socket object, supplying its constructor with the following two
arguments:
• the server's IP address (of type InetAddress);
• the appropriate port number for the service.
• (The port number for server and client programs must be the same, of
course!)
• For simplicity's sake, we shall place client and server on the same host, which
will allow us to retrieve the IP address by calling static method getLocalHost
of class. For example:

Socket link = new Socket(InetAddress.getLocalHost(),1234);


Zulfiqar Ali, CIIT Islamabad 11
2. Set up input and output streams.

• These are set up in exactly the same way as the server streams were set up
(by calling methods getInputStream and getOutputStream of the Socket
object that was created in step 2).

Zulfiqar Ali, CIIT Islamabad 12


3. Send and receive data.

• The Scanner object at the client end will receive messages sent by the
PrintWriter object at the server end,
• while the PrintWriter object at the client end will send messages that are
received by the Scanner object at the server end (using methods nextLine
and println respectively).

Zulfiqar Ali, CIIT Islamabad 13


4. Close the connection.

• This is exactly the same as for the server process (using method close of
class Socket).

Zulfiqar Ali, CIIT Islamabad 14


Datagram (UDP) Sockets
• Unlike TCP/IP sockets, datagram sockets are connectionless.
• The connection between client and server is not maintained throughout the
duration of the dialogue.
• Instead, each datagram packet is sent as an isolated transmission whenever
necessary.
• datagram (UDP) sockets provide a faster means of transmitting data than
TCP/IP sockets, but they are unreliable.
• Since the connection is not maintained between transmissions,

“the server does not create an individual Socket object for


each client, as it did in our TCP/IP example.”

Zulfiqar Ali, CIIT Islamabad 15


• A further difference from TCP/IP sockets is that,

“instead of a ServerSocket object, the server creates a DatagramSocket


object, as does each client when it wants to send datagram(s) to the
server. “

• The final and most significant difference is that,

“DatagramPacket objects are created and sent at both ends, rather than
simple strings. “

Zulfiqar Ali, CIIT Islamabad 16


SERVER Side

1. Create a DatagramSocket object.


• Just as for the creation of a ServerSocket object, this means supplying the
object's constructor with the port number.
• For example:

DatagramSocket datagramSocket = new DatagramSocket(1234);

Zulfiqar Ali, CIIT Islamabad 17


2. Create a buffer for incoming datagrams.

• This is achieved by creating an array of bytes.


• For example:
byte[] buffer = new byte[256];

Zulfiqar Ali, CIIT Islamabad 18


3. Create a DatagramPacket object for the incoming datagrams.
• The constructor for this object requires two arguments:

• the previously-created byte array;


• the size of this array.
• For example:
DatagramPacket inPacket =
new DatagramPacket(buffer, buffer.length);

Zulfiqar Ali, CIIT Islamabad 19


4. Accept an incoming datagram.
• This is effected via the receive method of our
DatagramSocket object, using our DatagramPacket object as
the receptacle.
• For example:

datagramSocket.receive(inPacket);

Zulfiqar Ali, CIIT Islamabad 20


5. Accept the sender's address and port from the packet.

• Methods getAddress and getPort of our DatagramPacket object are used


for this.
• For example:

InetAddress clientAddress = inPacket.getAddress();


int clientPort = inPacket.getPort();

Zulfiqar Ali, CIIT Islamabad 21


6. Retrieve the data from the buffer.
• For convenience of handling, the data will be retrieved as a string, using an
overloaded form of the String constructor that takes three arguments:
• a byte array;
• the start position within the array (= 0 here);
• the number of bytes (= full size of buffer here).
• For example:
String message = new String(inPacket.getData(),
0,inPacket.getLength());

Zulfiqar Ali, CIIT Islamabad 22


7. Create the response datagram.
• Create a DatagramPacket object, using an overloaded form of the constructor that
takes four arguments:
• the byte array containing the response message;
• the size of the response;
• the client's address;
• the client's port number.
• The first of these arguments is returned by the getBytes method of the String class
• (acting on the desired String response). For example:
DatagramPacket outPacket = new DatagramPacket(response.getBytes(),
response.length(),clientAddress, clientPort);
(Here, response is a String variable holding the return message.)

Zulfiqar Ali, CIIT Islamabad 23


8. Send the response datagram.
• This is achieved by calling method send of our DatagramSocket object,
supplying our outgoing DatagramPacket object as an argument.
• For example:
datagramSocket.send(outPacket);

• Steps 4-8 may be executed indefinitely (within a loop).

Zulfiqar Ali, CIIT Islamabad 24


• Under normal circumstances, the server would probably not be closed
down at all.
• However, if an exception occurs, then the associated DatagramSocket
should be Closed. (as shown in step 9 below. )
9. Close the DatagramSocket.

• This is effected simply by calling method close of our DatagramSocket


object.
• For example:
datagramSocket.close();

Zulfiqar Ali, CIIT Islamabad 25


CLIENT SIDE
1. Create a DatagramSocket object.
• This is similar to the creation of a DatagramSocket object in the server
program, but with the important difference that the constructor here
requires no argument, since a default port (at the client end) will be used.

• For example:

DatagramSocket datagramSocket = new DatagramSocket();

Zulfiqar Ali, CIIT Islamabad 26


2. Create the outgoing datagram.
• This step is exactly as for step 7 of the server program.
• For example:

DatagramPacket outPacket = new


DatagramPacket(message.getBytes(),
message.length(), host, PORT);

Zulfiqar Ali, CIIT Islamabad 27


3. Send the datagram message.
• Just as for the server, this is achieved by calling method send of the
DatagramSocket object, supplying our outgoing DatagramPacket object as
an argument.
For example:
datagramSocket.send(outPacket);

• Steps 4-6 below are exactly the same as steps 2-4 of the server procedure.

Zulfiqar Ali, CIIT Islamabad 28


4. Create a buffer for incoming datagrams.
• For example:

byte[] buffer = new byte[256];

Zulfiqar Ali, CIIT Islamabad 29


5. Create a DatagramPacket object for the incoming datagrams.

• For example:

DatagramPacket inPacket = new DatagramPacket(buffer,


buffer.length);

Zulfiqar Ali, CIIT Islamabad 30


6. Accept an incoming datagram.
• For example:
datagramSocket.receive(inPacket);

7. Retrieve the data from the buffer.


• This is the same as step 6 in the server program. For example:
String response = new String(inPacket.getData(),0
inPacket.getLength());

Steps 2-7 may then be repeated as many times as required.

Zulfiqar Ali, CIIT Islamabad 31


8. Close the DatagramSocket.
• This is the same as step 9 in the server program.
• For example:

datagramSocket.close();

Zulfiqar Ali, CIIT Islamabad 32

You might also like