Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
6 views

Unit 1 Java Networking

Uploaded by

dummyitem123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Unit 1 Java Networking

Uploaded by

dummyitem123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Unit – 1

Java Networking
1
Networking
• Networking supplements a lot of power to simple programs. With networks, a
single program can regain information stored in millions of computers
positioned anywhere in the world.

Java is the leading programming language


composed from scratch with networking in mind.

2
Network Protocols
Transmission Control Protocol (TCP) User Datagram Protocol (UDP)
• TCP or Transmission Control Protocol • UDP or User Datagram Protocol is a
allows secure communication. connection-less protocol.
• TCP is a connection-oriented protocol. • A simpler Internet protocol in which
error-checking and recovery services
• Complete protocol for transporting are not required.
information like still images, data files,
and web pages. • Data is continuously sent to the
recipient, whether they receive it or
not.

3
IP Address

Port Number

Protocol
Networking
Terminology
MAC Address

Socket

Connection oriented & connection less protocol

4
IP Address
• A unique address that distinguishes a device on the
internet or a local network.
• The range of each octet varies from 0 to 255.Range
of the IP Address – 0.0.0.0 to 255.255.255.255
• For Example – 192.168.0.1

5
Port Number
• A method to recognize a particular process
connecting internet or other network information
when it reaches a server.

• Example of port number:


• 192.168.1.100: 7

6
Protocol

•A network protocol is an organized


set of commands that define how
data is transmitted between different
devices in the same network.

7
MAC Address

• A MAC address is a unique number that is used to track a device in a


network.
• It contains a 48 bit or 64-bit address, which is combined with the
network adapter.

8
Socket

• A socket is one endpoint of a two-way


communication connection between the
two applications running on the network.

9
In a connection-oriented
service, the user must establish a
Connection- connection before starting the
communication.
oriented
and
connection- However, In connectionless
less protocol protocol, the data is transported in
one route from source to
destination without verifying that
the destination is still there or not.

10
Socket
Programming
• Used for communication
between the applications
running on different JRE.

11
Socket
Programming
• The java.net package of the J2SE APIs
contains a collection of classes and interfaces
that provide the low-level communication
details, allowing you to write java socket
program.

12
Socket Programming
• Sockets provide the communication
mechanism between two computers using
TCP/IP. A client program creates a socket on
its end of the communication and attempts to
connect that socket to a server.
• When the connection is made, the server
creates a socket object on its end of the
communication. The client and the server can
now communicate by writing to and reading
from the socket.

13
Socket Class

Method Description
1) public InputStream returns the InputStream attached
getInputStream() with this socket.

2) public OutputStream returns the OutputStream attached


getOutputStream() with this socket.

3) public synchronized void close() closes this socket

14
Server Socket Class

1) public Socket accept() returns the socket and establish a


connection between server and
client.
2) public synchronized void close() closes the server socket.

15
Creating Server

1.ServerSocket ss=new ServerSocket(6876);

2.Socket s=ss.accept();

16
Creating Client

• Socket s=new Socket("localhost",6876);

17
My Server import java.io.*;

import java.net.*;

public class MyServer {

public static void main(String[] args){

try{

ServerSocket ss=new ServerSocket(6876);

Socket s=ss.accept();//establishes connection

DataInputStream dis=new DataInputStream(s.getInputStream());

String str=(String)dis.readUTF();

System.out.println("message= "+str);

ss.close();

}catch(Exception e){System.out.println(e);}

18
My Client import java.io.*;

import java.net.*;

public class MyClient {

public static void main(String[] args) {

try{

Socket s=new Socket("localhost",6876);

DataOutputStream dout=new DataOutputStream(s.getOutputStream());

dout.writeUTF("Hello Server");

dout.flush();

dout.close();

s.close();

}catch(Exception e){System.out.println(e);}

19
Java URL
1. Protocol: In this case, http is the protocol.
2. Server name or IP Address: In this case, www.javawebapp.com is the server
name.
3. Port Number: It is
an optional attribute. If we write
http//ww.javawebapp.com:80/jaivkpanchal/ , 80 is the port number. If port
number is not mentioned in the URL, it returns -1.
4. File Name or directory name: In this case, index.jsp is the file name.

20
Java URL
• URL(String spec)

• Creates an instance of a URL from the String representation.

• URL(String protocol, String host, int port, String file)

• Creates an instance of a URL from the given protocol, host, port number, and
file.

• URL(String protocol, String host, int port, String file, URLStreamHandler


handler)

• Creates an instance of a URL from the given protocol, host, port number, file,
and handler.

• URL(String protocol, String host, String file)

• Creates an instance of a URL from the given protocol name, host name, and file
name.

• URL(URL context, String spec)

• Creates an instance of a URL by parsing the given spec within a specified

21
context.
Java URL Methods

Method Description

public String getProtocol() it returns the protocol of the URL.

public String getHost() it returns the host name of the URL.

public String getPort() it returns the Port Number of the URL.

public String getFile() it returns the file name of the URL.

public String getAuthority() it returns the authority of the URL.

public String toString() it returns the string representation of the


URL.

22
Java URL Methods

public String getQuery() it returns the query string of the URL.

public String getDefaultPort() it returns the default port of the URL.

public URLConnection openConnection() it returns the instance of URLConnection i.e.


associated with this URL.

public boolean equals(Object obj) it compares the URL with the given object.

public Object getContent() it returns the content of the URL.

public String getRef() it returns the anchor or reference of the


URL.
public URI toURI() it returns a URI of the URL.

23
URL Example import java.net.*;

public class URLDemo{

public static void main(String[] args){

try{

URL url=new URL("https://stackoverflow.com/company");

System.out.println("Protocol: "+url.getProtocol());

System.out.println("Host Name: "+url.getHost());

System.out.println("Port Number: "+url.getPort());

System.out.println("File Name: "+url.getFile());

}catch(Exception e){System.out.println(e);}

24

You might also like