17
RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

Embed Size (px)

Citation preview

Page 1: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)1

Java Networking

RGEC Meerut

Page 2: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT2

Open System Connection

The OSI model describes the architecture for inter computer Communications.

Data moves down the layers of the source computer,across a physical medium, then back up the layers of the destination computer

Page 3: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)3

Networks/Protocols

Computer network– A set of computers using common protocols to communicate over

connecting transmission media.

Protocol – A formal description of message formats and the rules two or

more machines follow to exchange messages. – Transmission Control Protocol/Internet Protocol (TCP/IP) is a

very popular protocol in use today.

Page 4: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)4

Socket Based Communication

Programs running on separate machines can communicate with each other through designated TCP/IP sockets

Sockets are the endpoints of logical connections between two hosts and can be used to send and receive data

Socket communication in Java works similarly to I/O Operations

Page 5: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)5

Stream Sockets

Stream sockets use TCP (Transmission Control Protocol) for data transmission .

TCP is lossless and reliable because it can detect lost transmissions and resubmit them.

• All data sent is received in the same order it was sent. A stream of 8-bit bytes is exchanged across a TCP connection. Connections provided by TCP allow concurrent transfer in both

directions. Such connections are called full duplex.

A fitting analogy is a telephone connection with a dedicated link.

With the chat project, all conversations between usersare handled using stream sockets .

Page 6: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)6

Connection –Oriented Transfer

Page 7: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)7

Datagram Sockets

Datagram sockets use UDP (User Datagram Protocol) for data transmission .

UDP cannot guarantee that packets are not lost, or not received in duplicate, or received in the order they were sent.

An analogy is the sending of letters through the post office– Although you don’t get duplicate letters.

With the chat project, the broadcasting of the registered user list is handled using datagram sockets.

Page 8: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)8

Connectionless Transfer

Page 9: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)9

Client/Server Computing

Page 10: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)10

Server Socket

To establish a server, you need to create a server socket and attach it to a port where the server will listen for connections

– Port numbers range from 0 to 65536 Ports 0 to 1024 are reserved for privileged services ftp runs on port 21 sendmail runs on port 25 http runs on port 80 To create a server socket on port 8000: // running on server: rgec.edu try { ServerSocket servSocket = new ServerSocket(8000); } catch (java.net.BindException) { // port is already in use}

Page 11: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)11

Client Sockets

After a server socket is created, the server can listen for client connections using accept()// running on server: rgec.edu

Server socket = servSocket.accept();

The statement blocks until a client connects to the server socket using the server host name// running on client: ikaruga.cs.rit.edu

Socket socket = new Socket(“rgec.edu”, 8000);

alternatively, you can specify the IP address in a string:“129.21.36.165”

Page 12: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)12

Client Socket

If you provide a host name instead of an IP when creating a socket, Java will query a Domain Name Server to do the translation.

The hostname localhost refers to the machine on which a client is running

“There’s no place like 127.0.0.1”

Page 13: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)13

Data Transmission Through Sockets

Page 14: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)14

Data Transmission Through Sockets

The socket returns an InputStream and OutputStream for reading and writing bytes.

InputStream is =socket.getInputStream(); OutputStream os = socket.getOutputStream(); • For efficiency, wrap those streams to do binary

I/O: DataInputStream input = new

DataInputStream(is); DataOutputStream output = new

DataOutputStream(os);

Page 15: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)15

InetAddress

Use InetAddress to find the hostname and IP of a client connecting to a server.

InetAddress inetAddress = socket.getInetAddress();

System.out.println(“Client’s host name is “ + inetAddress.getHostName());

System.out.println(“Client’s IP address is “ +inetAddress.getHostAddress());

• You can create an instance of InetAddress using the static method getByName(String).

InetAddress address =InetAddress.getByName(“rgec.edu”);

Page 16: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)16

Client/Server Example

Write a client program which sends the radius of a circleto a server program, which computes the area and then sends the result back to the client

Page 17: RGEC MEERUT(IWT CS703) 1 Java Networking RGEC Meerut

RGEC MEERUT(IWT CS703)17

Port

Protocol Port NumberDatatime 13FTP –data 20FTP –Control 21Telnet 23SMTP 25HTTP 80POP3 110NNTP 119