Java NetworkingUnit3
Java NetworkingUnit3
Java NetworkingUnit3
Networking Terminologies
1. IP Address
2. Protocol
3. Port Number
4. MAC Address
5. Connection-oriented and connection-less protocol
6. Socket
1. IP Address
The IP address is a unique number assigned to a node of a network e.g.
192.168.0.1. It is composed of octets that range from 0 to 255.
2. Protocol
A protocol is a set of rules followed for communication. For example:
TCP
FTP
Telnet
SMTP
POP etc.
3. Port Number
4. MAC Address
A MAC address is basically a hardware identification number which uniquely
identifies each device on a network. For example, an Ethernet card may have
a MAC address of 00:0d:83:b1:c0:8e.
6. Socket
A socket in Java is one endpoint of a two-way communication link between two
programs running on the network. A socket is bound to a port number so that
the TCP layer can identify the application that data is destined to be sent to.
Types of Socket
1. Socket (String hostName, int port): Creates a socket connecting the local
host to the named host and port; can throw an UnknownHostException ad
host IOException.
2. Socket (InetAddress ip Address, int port): Creates a socket using a
preexisting InetAddress object and a port; can throw an IOException.
iii)public int getPort():Returns the port the socket is bound to on the remote
machine.
iv) public int getLocalPort(): Returns the port the socket is bound to on the
local machine.
v)public int getPort(): Returns the port the socket is bound to on the remote
machine.
viii) public void close() throws IOException: Closes the socket, which makes this
Socket object no longer capable of connecting again to any server.
b) int getLength(): Returns the length of the data to be sent or the length of the
data received.
c) void setData(byte[] buf): Sets the data buffer for this packet.
d) void setLength(int length): Sets the length for this packet.
Client Server
Server: Server is the one who provides requested services. A server is anything
that has some resource that can be shared. Servers are defined as fast-
processing devices that act as centralized repositories of network files, pro-
grams, databases, and policies. Following are the some example of server
Client: Clients are the ones who request services. A client is simply any other en-
tity that wants to gain access to a particular server. The server is a permanently
available resource, while the client is free to "unplug" after it has been served.
Client-Server Communication
A computer running a program that offers requested services from one or more
clients is called server machine. The media for communication can be wired or
wireless network.
Socket-Based Communication
Socket Programming:
One can create a socket that can be used to connect a server and a client. Once
the socket is created, the server can send data to the client and the client can
receive it.
Steps to create a server that sends some strings (messages) to the client:
Step 1: At server side, create a server socket with some port number. This is
done using
Step 2: Now, one should make the server wait till a client accepts connection.
This is done using accept() method.
Socket s=ss.accерt():
Step 4: Take another stream like PrintStream to send data till the socket.
Step 5: Finally, this PrintStream is used by the server to send data to the client.
To send data, one has print() or println() methods available in PrintStream.
Ps.println(str);
Step 6: Then close the connection. This can be done by closing all the streams
and socket at server side as:
Code:
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
s1=new ServerSocket(98);
}
catch(IOException ul)
{
System.err.println("could not found port 98");
System.exit(1);
}
Socket c=null;
try
{
c=s1.accept();
System.out.println("connection from"+c);
}
catch(IOException e)
{
System.out.println("accept failed");
System.exit(1);
}
PrintWriter out=new PrintWriter(c.getOutputStream(),true);
BufferedReader in=new BufferedReader(new InputStreamReader
(c.getInputStream()));
String I;
BufferedReader sin=new BufferedReader(new InputStreamReader(System.in));
System.out.println("I am ready type now");
while ((I=sin.readLine())!=null)
{
out.println(I);
}
out.close();
sin.close();
c.close();
s1.close();
}}
TCPclient.java
import java.net.*;
import java.io.*;
BufferedReader b= null;
try
{
s=new Socket(InetAddress.getLocalHost(),98);
//if you want to connect another machine in the network, specify the ip address
while(true)
{
int c=System.in.read();
switch(c)
{
case -1: System.out.println("Server quits");
return;
buffer[pos++]=(byte)c;
}}}
public static void main(String args[]) throws Exception
{
System.out.println("Server ready...n please type here");
ds=new DatagramSocket(888);
Myserver();
}
}
UDPclient.java
import java.net.*;
public class UDPclient
{
public static DatagramSocket ds;
public static byte buffer[ ]=new byte[1024];
2. Three-tier model: In this, the user’s queries are sent to middle-tier services,
from which the commands are again sent to the data source. The results are
sent back to the middle tier, and from there to the user.
This type of model is found very useful by management information system
directors.
JDBC Driver
Advantage
Native-API driver gives better performance than JDBC-ODBC bridge driver.
Disadvantages
Driver needs to be installed separately in individual client machines
The Vendor client library needs to be installed on client machine.
Type-2 driver isn’t written in java, that’s why it isn’t a portable driver
It is a database dependent driver.
3. Network Protocol driver – Type 3 driver (fully Java driver)
The Network Protocol driver uses middleware (application server) that
converts JDBC calls directly or indirectly into the vendor-specific database
protocol. Here all the database connectivity drivers are present in a single
server, hence no need of individual client-side installation.
Advantages
Type-3 drivers are fully written in Java, hence they are portable drivers.
No client side library is required because of application server that can per-
form many tasks like auditing, load balancing, logging etc.
Switch facility to switch over from one database to another database.
Disadvantages
Network support is required on client machine.
Maintenance of Network Protocol driver becomes costly because it requires
database-specific coding to be done in the middle tier.
The purpose of this layer is to translate the application's data queries into
commands that the DBMS understands. For this to work, both the application
and the DBMS must be ODBC Compliant, ie, the application must be capable of
issuing ODBC command and the DBMS must be capable of responding to them.
ODBC Process
1) ODBC client,
3) DBMS server (e.g., Microsoft Access, SQL Server, Oracle, and FoxPro).
Firstly, the ODBC client will use a command (referred to as "ODBC") to interact
(requesting and/or sending data) with the DBMS server (back-end). However,
the DBMS server will not understand the command by the ODBC client yet, as
the command has yet to be processed through the ODBC driver (front-end). So
then, the ODBC driver will decode the command that can be processed by the
ODBC server and be sent there. The ODBC server will then respond back to the
ODBC driver which will translate the final output to the ODBC client.
ODBC Architecture
3) Driver: Processes ODBC function calls, submits SQL requests to a specific data
source, and returns results to the application. If necessary, the driver modifies
an application's request so that the request conforms to syntax supported by
the associated DBMS.
4) Data Source: Consists of the data the user wants to access and its associated
operating system. DBMS, and network platform (if any) used to access the
DBMS.
ii) The Driver Manager, and between the Driver Manager and each driver.
The interface between the Driver Manager and the drivers is sometimes re-
ferred to as Service Provider Interface, or SPI. For ODBC, the Application-
Programming Inter (API) and the Service Provider Interface (SPI) are the same.
That is, the Driver Manage and each driver have the same interface to the same
functions.
ODBC vs JDBC
Connection Pooling
A connection pool is a cache of database connections maintained in memory
so that the connections can be re-used when the database receives future
requests for data Connection pools are used to enhance the performance of
executing commands on a database.
The application works with its Connection just like any other Connection until
it is finished. It then closes the connection just as it normally would. Unknown
to the application, the physical link to the database is not being closed. Its
close() method returns it to the connection pool. If you try to use that
Connection again without getting it from the connection pool, it will throw an
exception.