socket programming using java

Post on 07-Aug-2015

55 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

OpenLab 1

Socket Programming

openlabworld@gmail.com

OpenLab 2

Socket Programming

OpenLab 3

TCP/IP Beginning

OpenLab 4

TCP/IP ModelEncapsulation Decapsulation

OpenLab 5

Socket Description

OpenLab 6

Sockets

● Defination - An interface between an application process and transport layer– The application process can send/receive

messages to/from another application process (local or remote)via a socket

● Types of Sockets– TCP/Stream Socket

– UDP/Datagram Socket

– Raw Socket

● No Sockets – No communication

OpenLab 7

Socket Programming using JAVA

● I/O Stream ● Reader/Writer● Create Socket (Class)

– TCP Socket ● Server ServerSocket● Client Socket

– UDP Socket ● DatagramSocket

OpenLab 8

I/O Stream

*https://docs.oracle.com/javase/tutorial/essential/io/streams.html

OutputStream

InputStream

Write

Read

- Input Source - Output Destination

A stream can represent many different kinds of sources & destinations -- Files - Devices - Programs- Memory arrays

OpenLab 9

Classification of Streams

● Byte Stream– Data in the form of individual bytes

● Text Strem– Data as character of each 2 bytes

OpenLab 10

Byte Strem

● Byte Stream– Data in the form of individual bytes

● InputStrem– DataInputStream– FileInputStream– BufferedInputStream– . . .

● OutputStream– DataOutputStream– FileOutputStream– BufferedOutputStream– . . .

OpenLab 11

Text Stream

● Text Stream– Data as character of each 2 bytes

● Reader– FileReader– BufferedReader

● Writer– FileWriter– BufferedWriter

OpenLab 12

Steps to create Socket(Server/Client)

● TCP Server Socket– Step-1 Create ServerSocket Object

– Step-2 Wait for the client request

– Step-3 Create I/O streams for communication to the client.

– Step-4 Perform communication with client

– Step-5 Close Socket

● TCP Client Socket– Step-1 Create Socket Object

– Step-2 Create I/O streams for communication with the server

– Step-3 Perform I/O or communication with the server

– Step-4 Close Socket

OpenLab 13

Create TCP Socket(Connection Oriented)

socket()

connect()

bind()

accept()

send()

recv()

listen()

socket()

send()

recv()

Server Client

close() close()

OpenLab 14

Working of TCP Socket

OpenLab 15

Scenario 2● Two BEST friends , they want to chat to each

using their computer and their fathers uninstalled messanger from their computers

Ganesh

Mohan

OpenLab 16

Case-1

● They decided to create a STREAM/TCP Socket ● One way Communication

I/O Stream

Client SocketMOHAN Server Socket

GANESH

`

OpenLab 17

Case-2

● They decided to create a STREAM/TCP Socket ● Two way Communication

I/O Stream

Client SocketMOHAN Server Socket

GANESH

`

OpenLab 18

Steps to Create UDP Socket

● UDP Server/Client– Step-1 Create DatagramSocket object

– Step-2 Create buffer for tmp storage

– Step-3 Store/Reterive data in/from buffer

– Step-4 Create DatagramPacket

– Step-5 send/receive DatagramPacket

OpenLab 19

Create UDP Socket[Connection less]

socket()

bind()

recvfrom()

sendto()

socket()

recvfrom()

sendto()

ClientServer

close() close()

OpenLab 20

Case-1

● They decided to create a DATAGRAM/UDP Socket

● One way Communication

Client SocketMOHAN Server Socket

GANESH

Datagram Packet

OpenLab 21

Case-2

● They decided to create a DATAGRAM/UDP Socket

● Two way Communication

Client SocketMOHAN Server Socket

GANESH

Datagram Packet

Datagram Packet

OpenLab 22

To be continued...

top related