Socket programming using java

NarendranThangarajan 5,723 views 23 slides Oct 06, 2011
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

Seminar on Socket Programming in Java including Hands-on Coding session.


Slide Content

SOCKET PROGRAMMING IN JAVA
NarendranThangarajan,
@naren_live,
II Year, BE, CSE,
SSN College of Engg, Chennai.

What started it all..
Internet has emerged as a global ubiquitous
media for communication
It has changed the way we live, learn, enjoy,
communicate, interact, engage, etc.
To take advantage of this, businesses are
ceaselessly trying to offer their services through
the internet.
So a huge demand for software developers good
in creating internet-enabled applications.

Client Server Applications
Network
A client, A server, and network
Client
Server
Client machine
Server machine
The Web Service is provided by the server
and the clients use these services
This is where sockets come in !!

Sockets
Introduced in BSD 4.1 UNIX 1981.
Sockets are the endpoints of any
communication over the internet.
Sockets are identified by socket addresses.
Socket Address = IP Address + Port Number

Why IP Address + Port number?
•IP Address is of the form 10.0.0.1
•Port Number can be anything from 0 to 65,535.

IP Address –Choose network
Destination Socket = 10.0.0.2 : 80
10.0.0.0
20.0.0.0
30.0.0.0
40.0.0.0

IP Address -> MAC Address –Locate the
specific system
Destination Socket = 10.0.0.2 : 80
10.0.0.1
10.0.0.2

Port Number –Process specific
Port 10000
Port 11000
Port 120000

Understanding Ports
Transport Layer
Port 1 Port 2 Port 3 Port 4
Gmail
OutLook
Express
YM
AVG
Update
Port numdata
Packet
Network layer

Thus virtually, sockets are a connection
between the two processes in different
systems.
Eg: Let the socket pairs be
10.0.0.1 : 80 and 20.0.0.1 : 2000
192.168.21.10 : 3000 and 192.168.100.1 : 6000

Networking Basics –the larger picture
Applications Layer
Standard apps
HTTP
FTP
Telnet
User apps
Transport Layer
TCP
UDP
Network Layer
IP
Link Layer
Device drivers
Application
(http,ftp,telnet,…)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)
TCP STACK

Network Basics -Where are these sockets?
Applications Layer
Standard apps
HTTP
FTP
Telnet
User apps
Programming Interface:
SOCKETS
Transport Layer
TCP
UDP
Network Layer
IP
Link Layer
Device drivers
Application
(http,ftp,telnet,…)
Transport
(TCP, UDP,..)
Network
(IP,..)
Link
(device driver,..)
TCP STACK
Sockets

Now into Socket
programming..

Socket Programming with TCP
Server starts first..
Server Process must be running first
Server must have created a socket which welcomes
client’s connection. (Welcoming socket)
Client contacts server by..
Creating Client local TCP socket
Specify IP Address and port number of server process.
When Client socket is created, the connection is
established.
When connection is established, server creates a new
socket (Connection Socket) to communicate with that
client and the Welcoming socket is once again waiting
for connections for other clients.

Client/Server Socket Interaction in
TCP
wait for incoming
connection request
connectionSocket=
welcomeSocket.accept()
create socket,
port=x, for
incoming request:
welcomeSocket=
ServerSocket()
create socket,
connect to hostid, port=x
clientSocket=
Socket()
close
connectionSocket
read reply from
connectionSocket
close
clientSocket
send request using
clientSocketread request from
connectionSocket
write reply to
connectionSocket
TCP
connection setup
Server
Client

Step 1 :
Server
Client
Connection request
port
Server
Client
Connection
port
port
port
Step 2 :

Types of Sockets in TCP
ServerSocket–the socket used by servers
Socket –Socket used by clients
Create a ServerSocketin the server and make
it to wait for connections from Socketsfrom
other clients

The concept of Streams
Client
socket
Server
socket
Client output
stream
Server input
stream
Client input
stream
Server output
stream

Socket Programming with UDP
No Connection between client and server.
No handshaking
The sender has to explicitly mention the IP
address and the port of the destination.
The server should extract the IP Address of the
datagram everytime.
Uses DatagramSocket.

Client/server socket interaction:
UDP
close
clientSocket
Server
read reply from
clientSocket
create socket,
clientSocket=
DatagramSocket()
Client
Create, address (hostid, port=x),
send datagram request
using clientSocket
create socket,
port=x, for
incoming request:
serverSocket=
DatagramSocket()
read request from
serverSocket
write reply to
serverSocket
specifying client
host address,
port number

Coding time..

Conclusion
Socket Programming is very easy in Java.
Usually each and every socket is handled by a
separate thread in real-time client/server
environments.

Queries