socket programming in java (pptx)

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

Upload: narendran-thangarajan

Post on 06-Dec-2014

1.964 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Socket programming in Java (PPTX)

SOCKET PROGRAMMING IN JAVA

Narendran Thangarajan,@naren_live,II Year, BE, CSE,SSN College of Engg, Chennai.

Page 2: Socket programming in Java (PPTX)

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.

Page 3: Socket programming in Java (PPTX)

Client Server Applications

NetworkRequest

Result

A client, A server, and network

ClientServer

Client machineServer machine

The Web Service is provided by the server and the clients use these services

This is where sockets come in !!

Page 4: Socket programming in Java (PPTX)

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

Page 5: Socket programming in Java (PPTX)

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.

Page 6: Socket programming in Java (PPTX)

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

Page 7: Socket programming in Java (PPTX)

IP Address -> MAC Address – Locate the specific system

Destination Socket = 10.0.0.2 : 80

10.0.0.1

10.0.0.2

Page 8: Socket programming in Java (PPTX)

Port Number – Process specificPort 10000

Port 11000

Port 120000

Page 9: Socket programming in Java (PPTX)

Understanding Ports

Transport Layer

Port 1 Port 2 Port 3 Port 4

GmailOutLook Express

YMAVG

Update

Port num data

Packet

Network layer

Page 10: Socket programming in Java (PPTX)

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

Page 11: Socket programming in Java (PPTX)

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

Page 12: Socket programming in Java (PPTX)

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

Page 13: Socket programming in Java (PPTX)

Now into Socket programming..

Page 14: Socket programming in Java (PPTX)

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.

Page 15: Socket programming in Java (PPTX)

Client/Server Socket Interaction in TCP

wait for incomingconnection requestconnectionSocket =welcomeSocket.accept()

create socket,port=x, forincoming request:welcomeSocket =

ServerSocket()

create socket,connect to hostid, port=xclientSocket =

Socket()

closeconnectionSocket

read reply fromconnectionSocket

closeclientSocket

send request usingclientSocketread request from

connectionSocket

write reply toconnectionSocket

TCP connection setup

Server

Client

Page 16: Socket programming in Java (PPTX)

Step 1 :

ServerClient

Connection request

port

Server

Client

Connection

port

port port

Step 2 :

Page 17: Socket programming in Java (PPTX)

Types of Sockets in TCP

ServerSocket – the socket used by servers

Socket – Socket used by clients

Create a ServerSocket in the server and make it to wait for connections from Sockets from other clients

Page 18: Socket programming in Java (PPTX)

The concept of Streams

Client socket

Server socket

Client output stream

Server input stream

Client input stream

Server output stream

Page 19: Socket programming in Java (PPTX)

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.

Page 20: Socket programming in Java (PPTX)

Client/server socket interaction: UDP

closeclientSocket

Server

read reply fromclientSocket

create socket,clientSocket = DatagramSocket()

Client

Create, address (hostid, port=x),send datagram request using clientSocket

create socket,port=x, forincoming request:serverSocket = DatagramSocket()

read request fromserverSocket

write reply toserverSocketspecifying clienthost address,port number

Page 21: Socket programming in Java (PPTX)

Coding time..

Page 22: Socket programming in Java (PPTX)

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.

Page 23: Socket programming in Java (PPTX)

Queries