Unit3_Java
Unit3_Java
Example
1. ServerSocket serverSocket = new ServerSocket(port);
2. ServerSocket serverSocket = new ServerSocket(port, maxQueue);
import java.io.*;
import java.net.*;
public class ServerSocketExample {
public static void main(String[] args) throws IOException{
int port = 8080; // Specify the port number
int maxQueue = 100; // Specify the maximum queue length
// Creating the ServerSocket with the specified port
ServerSocket serverSocket = new ServerSocket(port, maxQueue);
System.out.println("Server started on port " + port + " with
maxQueue = " + maxQueue);
}
}
Server started on port 8080 with maxQueue = 100
Datagrams
• A datagram is an independent, self-contained message sent over the network.
• Datagrams are bundles of information passed between machines.
• Java implements datagrams on top of the UDP protocol by using two classes.
• The DatagramPacket object is the data container, while the DatagramSocket is
the mechanism used to send or receive the DatagramPackets.
• The send( ) method sends a packet to the port specified by packet.
• The receive( ) method waits for a packet to be received and returns the result.
Constructors
DatagramSocket( ) throws SocketException
DatagramSocket(int port) throws SocketException
DatagramPacket(byte data [ ], int size)
DatagramPacket(byte data [ ], int offset, int size)
DatagramPacket(byte data [ ], int size, InetAddress ipAddress, int port)
Send and receive UDP packets using
DatagramSocket and DatagramPacket
import java.net.*;
public class UDPClient {
public static void main(String[] args) throws Exception {
DatagramSocket socket = new DatagramSocket();
String message = "Hello Server!";
byte[] sendData = message.getBytes();
InetAddress serverAddress = InetAddress.getByName("localhost");
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length,
serverAddress, 9876);
socket.send(sendPacket);
System.out.println("Sent: " + message);
socket.close();
} Sent: Hello Server!
}
import java.net.*;
public class UDPServer {
public static void main(String[] args) throws SocketException {
DatagramSocket socket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData,
receiveData.length);
socket.receive(receivePacket);
String message = new String(receivePacket.getData(), 0,
receivePacket.getLength());
System.out.println("Received: " + message);
}
} Received: Hello from server!
}
JDBC
• JDBC stands for Java Database Connectivity, which is a
standard Java API for database-independent connectivity
between the Java programming language and a wide range of
databases.
• The JDBC library includes APIs for each of the tasks mentioned
below that are commonly associated with database usage.
• Making a connection to a database.
• Creating SQL or MySQL statements.
• Executing SQL or MySQL queries in the database.
• Viewing & Modifying the resulting records.
JDBC Driver Types
• The JDBC driver specification classifies JDBC drivers into four groups. Each group is
referred to as a JDBC driver type and addresses a specific need for communicating
with various DBMSs.
Type 1 Driver (JDBC-ODBC Bridge Driver):
• This driver translates JDBC calls into ODBC (Open Database Connectivity) calls.
• It relies on an ODBC driver to connect to the database, which means it requires the
ODBC driver to be installed on the client machine.
• It’s platform-dependent, not ideal for large-scale applications, and has been
deprecated by many vendors.
Type 2 Driver (Native-API Driver):
• This driver converts JDBC calls directly into database-specific calls, using the
database’s native API.
• It is faster than the Type 1 driver but still depends on the client having database-
specific software (like Oracle client libraries) installed.
• This makes it platform-dependent, and while faster than Type 1, it can still cause
issues in large distributed environments.
Type 3 Driver (Network Protocol Driver):
• This driver communicates with the database through a middleware server that
handles communication with the database.
• It doesn’t require any database-specific client software, making it more portable than
Types 1 and 2.
• The server-side middleware manages the communication, so it is often used for web-
based applications.