0% found this document useful (0 votes)
15 views15 pages

PDC DC Record

The document contains multiple Java programs implementing TCP and UDP server and client functionalities, as well as a DNS server using RMI and database interactions. Each program includes error handling and socket management for communication between clients and servers. The DNS server programs utilize MySQL databases to resolve domain names to IP addresses, while the chat server program is incomplete.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views15 pages

PDC DC Record

The document contains multiple Java programs implementing TCP and UDP server and client functionalities, as well as a DNS server using RMI and database interactions. Each program includes error handling and socket management for communication between clients and servers. The DNS server programs utilize MySQL databases to resolve domain names to IP addresses, while the chat server program is incomplete.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

TCP Server Program

import java.net.*;
import java.io.*;

public class TCPServer {


public static void main(String[] args) {
ServerSocket listenSocket = null;
try {
int serverPort = 8117;
listenSocket = new ServerSocket(serverPort);
System.out.println("Server is listening on port " + serverPort);
while (true) {
Socket clientSocket = listenSocket.accept();
new Connection(clientSocket);
}
} catch (IOException e) {
System.out.println("Listen: " + e.getMessage());
} finally {
try {
if (listenSocket != null) {
listenSocket.close();
}
} catch (IOException e) {
System.out.println("Could not close server socket: " + e.getMessage());
}
}
}
}

class Connection extends Thread {


private DataInputStream in;
private DataOutputStream out;
private Socket clientSocket;

public Connection(Socket aClientSocket) {


try {
clientSocket = aClientSocket;
in = new DataInputStream(clientSocket.getInputStream());
out = new DataOutputStream(clientSocket.getOutputStream());
this.start();
} catch (IOException e) {
System.out.println("Connection: " + e.getMessage());
}
}

public void run() {


try {
String data = in.readUTF();
out.writeUTF(data);
System.out.println("Received: " + data);
} catch (EOFException e) {
System.out.println("EOF: " + e.getMessage());
} catch (IOException e) {
System.out.println("IO: " + e.getMessage());
} finally {
try {
clientSocket.close();
} catch (IOException e) {
System.out.println("Could not close client socket: " + e.getMessage());
}
}
}
}

TCP Client Program

import java.net.*;
import java.io.*;

public class TCPClient {


public static void main(String[] args) {
Socket s = null;
try {
if (args.length < 2) {
System.out.println("Usage: java TCPClient <message> <hostname>");
return;
}

int serverPort = 8117;


s = new Socket(args[1], serverPort);
DataInputStream in = new DataInputStream(s.getInputStream());
DataOutputStream out = new DataOutputStream(s.getOutputStream());

out.writeUTF(args[0]);
String data = in.readUTF();
System.out.println("Received: " + data);
} catch (UnknownHostException e) {
System.out.println("Unknown Host: " + e.getMessage());
} catch (EOFException e) {
System.out.println("EOF: " + e.getMessage());
} catch (IOException e) {
System.out.println("IO Error: " + e.getMessage());
} finally {
if (s != null) {
try {
s.close();
} catch (IOException e) {
System.out.println("Error closing socket: " + e.getMessage());
}
}
}
}
}

UDP Client Program

import java.net.*;
import java.io.*;

public class UDPClient {


public static void main(String[] args) {
DatagramSocket aSocket = null;
try {
if (args.length < 2) {
System.out.println("Usage: java UDPClient <message> <hostname>");
return;
}
aSocket = new DatagramSocket();
byte[] m = args[0].getBytes();
InetAddress aHost = InetAddress.getByName(args[1]);
int serverPort = 8117;

DatagramPacket request = new DatagramPacket(m, m.length, aHost, serverPort);


aSocket.send(request);

byte[] buffer = new byte[1000];


DatagramPacket reply = new DatagramPacket(buffer, buffer.length);
aSocket.receive(reply);

// Print the received reply, trimming any excess null characters


System.out.println("Reply: " + new String(reply.getData(), 0, reply.getLength()));
} catch (SocketException e) {
System.out.println("Socket: " + e.getMessage());
} catch (IOException e) {
System.out.println("IO: " + e.getMessage());
} finally {
if (aSocket != null && !aSocket.isClosed()) {
aSocket.close();
}
}
}
}

UDP Server Program

import java.net.*;
import java.io.*;

public class UDPServer {


public static void main(String[] args) {
DatagramSocket aSocket = null;
try {
aSocket = new DatagramSocket(8117);
byte[] buffer = new byte[1000];

while (true) {
DatagramPacket request = new DatagramPacket(buffer, buffer.length);
aSocket.receive(request);

DatagramPacket reply = new DatagramPacket(request.getData(), request.getLength(), request.getAddress(),


aSocket.send(reply);

System.out.println("Reply: " + new String(request.getData(), 0, request.getLength()));


}
} catch (SocketException e) {
System.out.println("Socket: " + e.getMessage());
} catch (IOException e) {
System.out.println("IO: " + e.getMessage());
} finally {
if (aSocket != null && !aSocket.isClosed()) {
aSocket.close();
}
}
}
}
DNS using RMI Server Program

import java.rmi.*;
import java.rmi.server.*;
import java.rmi.registry.*;

public class DNSServer extends UnicastRemoteObject implements DNSServerIntf {

public DNSServer() throws RemoteException {


super();
}

public String DNS(String s1) throws RemoteException {


if (s1.equals("www.osmania.ac.in"))
return "50.32.24.29";
if (s1.equals("www.mvsrec.edu.in"))
return "90.82.44.89";
if (s1.equals("www.jntu.ac.in"))
return "150.32.64.20";
if (s1.equals("www.yahoo.com"))
return "88.39.124.129";
else
return "No Info about this address";
}

public static void main(String args[]) throws Exception {


Registry r = LocateRegistry.createRegistry(1234);
r.rebind("mvsrserver", new DNSServer());
System.out.println("Server started...");
}
}

DNS using RMI Client Program

import java.rmi.*;
import java.rmi.registry.*;

public class DNSClient {


public static void main(String args[]) throws Exception {
if (args.length < 1) {
System.out.println("Usage: java DNSClient <website_name>");
return;
}

Registry r = LocateRegistry.getRegistry("localhost", 1234);


DNSServerIntf d = (DNSServerIntf) r.lookup("mvsrserver");
String str = args[0];

System.out.println("The website name is: " + str);


System.out.println("The IP Address is: " + d.DNS(str));
}
}

DNS using RMI ServerIntf

import java.rmi.*;

public interface DNSServerIntf extends Remote {


String DNS(String s1) throws RemoteException;
}

DNS using Database Server1 Program

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.StringTokenizer;

class Server1 {
public static void main(String[] args) {
ServerSocket sock = null;
Socket client = null;
DataInputStream input = null;
PrintStream ps = null;
String url, u, s = "";
Connection con = null;
Statement smt = null;
ResultSet rs = null;

try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dns", "root", "");
smt = con.createStatement();
sock = new ServerSocket(5123);

while (true) {
client = sock.accept();
input = new DataInputStream(client.getInputStream());
ps = new PrintStream(client.getOutputStream());

url = input.readLine();
System.out.println("IN SERVER1 URL IS: " + url);

StringTokenizer st = new StringTokenizer(url, ".");


while (st.countTokens() > 1) {
s = s + st.nextToken() + ".";
}

if (st.hasMoreTokens()) {
u = st.nextToken();
} else {
ps.println("Invalid URL format. Please check and try again.");
continue;
}

rs = smt.executeQuery("SELECT port, ipadd FROM root WHERE name='" + u + "'");


if (rs.next()) {
ps.println(Integer.parseInt(rs.getString(1)));
ps.println(rs.getString(2));
ps.println(s.trim());
} else {
ps.println("Illegal address, please check the spelling again.");
}

ps.close();
input.close();
client.close();
}
} catch (Exception e) {
System.err.println(e);
} finally {
try {
if (rs != null) rs.close();
if (smt != null) smt.close();
if (con != null) con.close();
if (sock != null) sock.close();
} catch (SQLException e) {
System.err.println("Error closing resources: " + e);
} catch (IOException e) {
System.err.println("Error closing server socket: " + e);
}
}
}
}

Server2 Program
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.StringTokenizer;

class Server2 {
public static void main(String[] args) {
ServerSocket sock = null;
Socket client = null;
DataInputStream input = null;
PrintStream ps = null;
String url, u, s = "";
Connection con = null;
Statement smt = null;
ResultSet rs = null;

try {
// Load MySQL JDBC driver
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dns", "root", "");
smt = con.createStatement();
sock = new ServerSocket(5124);

while (true) {
client = sock.accept();
input = new DataInputStream(client.getInputStream());
ps = new PrintStream(client.getOutputStream());

url = input.readLine();
System.out.println("IN SERVER2 URL IS: " + url);

StringTokenizer st = new StringTokenizer(url, ".");


while (st.countTokens() > 1) {
s = s + st.nextToken() + ".";
}

if (st.hasMoreTokens()) {
u = st.nextToken();
} else {
ps.println("Invalid URL format. Please check and try again.");
continue;
}
rs = smt.executeQuery("SELECT port, ipadd FROM yahoo WHERE name='" + u + "'");
if (rs.next()) {
ps.println(rs.getString(1));
ps.println(rs.getString(2));
ps.println(s.trim());
} else {
ps.println("Illegal address, please check the spelling again.");
}

ps.close();
input.close();
client.close();
}
} catch (Exception e) {
System.err.println(e);
} finally {
try {
if (rs != null) rs.close();
if (smt != null) smt.close();
if (con != null) con.close();
if (sock != null) sock.close();
} catch (SQLException e) {
System.err.println("Error closing resources: " + e);
} catch (IOException e) {
System.err.println("Error closing server socket: " + e);
}
}
}
}

Server3 Program

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.StringTokenizer;

class Server3 {
public static void main(String[] args) {
ServerSocket sock = null;
Socket client = null;
DataInputStream input = null;
PrintStream ps = null;
String url, u, s = "";
Connection con = null;
Statement smt = null;
ResultSet rs = null;

try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dns", "root", "");
smt = con.createStatement();
sock = new ServerSocket(5125);

while (true) {
client = sock.accept();
input = new DataInputStream(client.getInputStream());
ps = new PrintStream(client.getOutputStream());
url = input.readLine();
System.out.println("IN SERVER3 URL IS: " + url);

StringTokenizer st = new StringTokenizer(url, ".");


while (st.countTokens() > 1) {
s += st.nextToken() + ".";
}

if (st.hasMoreTokens()) {
u = st.nextToken();
s = s.substring(0, s.length() - 1).trim(); // Remove the trailing dot
} else {
ps.println("Invalid URL format. Please check and try again.");
continue;
}

rs = smt.executeQuery("SELECT port, ipadd FROM google WHERE name='" + u + "'");


if (rs.next()) {
ps.println(rs.getString(1));
ps.println(rs.getString(2));
ps.println(s);
} else {
ps.println("Illegal address, please check the spelling again.");
}

ps.close();
input.close();
client.close();
}
} catch (Exception e) {
System.err.println(e);
} finally {
try {
if (rs != null) rs.close();
if (smt != null) smt.close();
if (con != null) con.close();
if (sock != null) sock.close();
} catch (SQLException e) {
System.err.println("Error closing resources: " + e);
} catch (IOException e) {
System.err.println("Error closing server socket: " + e);
}
}
}
}

DNS using Database Client Program

import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.StringTokenizer;

class Client {
public static void main(String[] args) {
Socket clisock = null;
DataInputStream input = null;
PrintStream ps = null;
String url, ip = "", s = "", u = "", p = "";
int pno = 5123;
Connection con = null;
Statement smt = null;
ResultSet rs = null;
boolean status = true;

try {
System.out.println("Enter name to resolve:");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
url = br.readLine();
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dns", "root", "");
smt = con.createStatement();

while (status) {
s = "";
System.out.println("IN CLIENT URL IS: " + url);
StringTokenizer st = new StringTokenizer(url, ".");

if (st.countTokens() == 1) {
status = false;
}

while (st.countTokens() > 1) {


s += st.nextToken() + ".";
}

if (st.hasMoreTokens()) {
u = st.nextToken();
} else {
System.out.println("Invalid URL format.");
break;
}

System.out.println("u = " + u);


rs = smt.executeQuery("SELECT port, ipadd FROM client WHERE name='" + u + "'");

if (rs.next()) {
p = rs.getString(1);
pno = Integer.parseInt(p);
String str = rs.getString(2);
url = s;
ip = str + "." + ip;
} else {
System.out.println("pno = " + pno);
clisock = new Socket("127.0.0.1", pno);
input = new DataInputStream(clisock.getInputStream());
ps = new PrintStream(clisock.getOutputStream());
ps.println(url);

p = input.readLine();
pno = Integer.parseInt(p);
String str = input.readLine();
url = input.readLine();
ip = str + "." + ip;

smt.executeUpdate("INSERT INTO client (name, ipadd, port) VALUES ('" + u + "','" + str + "','" + p + "')");
}
}

if (ip.length() > 0) {
ip = ip.substring(0, ip.length() - 1).trim();
}
System.out.println("IP address is: " + ip);

} catch (Exception e) {
System.err.println(e);
} finally {
try {
if (rs != null) rs.close();
if (smt != null) smt.close();
if (con != null) con.close();
if (clisock != null) clisock.close();
if (input != null) input.close();
if (ps != null) ps.close();
} catch (SQLException e) {
System.err.println("Error closing resources: " + e);
} catch (IOException e) {
System.err.println("Error closing client socket: " + e);
}
}
}
}

Chat Server Program

import java.net.*;
import java.io.*;

public class ChatServer implements Runnable {


private ChatServerThread clients[] = new ChatServerThread[50];
private ServerSocket server = null;
private Thread thread = null;
private int clientCount = 0;

public ChatServer(int port) {


try {
System.out.println("Binding to port " + port + ", please wait ...");
server = new ServerSocket(port);
System.out.println("Server started: " + server);
thread = new Thread(this);
thread.start();
} catch (IOException e) {
System.out.println("Cannot bind to port " + port + ": " + e.getMessage());
}
}

public void run() {


while (thread != null) {
try {
System.out.println("Waiting for a client...");
addThread(server.accept());
} catch (IOException e) {
System.out.println("Server accept error: " + e);
if (thread != null) {
thread.interrupt();
thread = null;
}
}
}
}
public void stop() {
if (thread != null) {
thread.interrupt();
thread = null;
}
}

private int findClient(int ID) {


for (int i = 0; i < clientCount; i++) {
if (clients[i].getID() == ID)
return i;
}
return -1;
}

public synchronized void handle(int ID, String input) {


if (input.equals("quit")) {
clients[findClient(ID)].send("quit");
remove(ID);
} else {
System.out.println(ID + ": " + input);
for (int i = 0; i < clientCount; i++) {
clients[i].send(ID + ": " + input);
}
}
}

public synchronized void remove(int ID) {


int pos = findClient(ID);
if (pos >= 0) {
ChatServerThread closing = clients[pos];
System.out.println("Removing client thread: " + ID + " at " + pos);
if (pos < clientCount - 1) {
for (int i = pos + 1; i < clientCount; i++) {
clients[i - 1] = clients[i];
}
}
clientCount--;
try {
closing.close();
} catch (IOException e) {
System.out.println("Error closing thread: " + e);
}
closing.stop();
}
}

private void addThread(Socket socket) {


if (clientCount < clients.length) {
System.out.println("Client accepted: " + socket);
clients[clientCount] = new ChatServerThread(this, socket);
try {
clients[clientCount].open();
clients[clientCount].start();
clientCount++;
} catch (IOException e) {
System.out.println("Error opening thread: " + e);
}
} else {
System.out.println("Client refused; maximum " + clients.length + " reached.");
}
}

public static void main(String[] a) {


ChatServer server = null;
if (a.length != 1) {
System.out.println("Usage: java ChatServer Port");
} else {
server = new ChatServer(Integer.parseInt(a[0]));
}
}
}

Chat Server Thread Program

import java.net.*;
import java.io.*;

public class ChatServerThread extends Thread {


private ChatServer server = null;
private Socket socket = null;
private DataInputStream In = null;
private int ID = -1;
private PrintStream Out = null;

public ChatServerThread(ChatServer serv, Socket sock) {


super();
server = serv;
socket = sock;
ID = socket.getPort();
}

public void send(String msg) {


Out.println(msg);
Out.flush();
}

public int getID() {


return ID;
}

public void run() {


System.out.println("Server thread " + ID + " running");
while (true) {
try {
server.handle(ID, In.readLine());
} catch (IOException e) {
System.out.println(ID + " error reading: " + e.getMessage());
server.remove(ID);
break;
}
}
}

public void open() throws IOException {


In = new DataInputStream(socket.getInputStream());
Out = new PrintStream(socket.getOutputStream());
}
public void close() throws IOException {
if (socket != null)
socket.close();
if (In != null)
In.close();
if (Out != null)
Out.close();
}
}

Chat CLient Program

import java.net.*;
import java.io.*;

public class ChatClient implements Runnable {


private ChatClientThread client = null;
private Socket socket = null;
private DataInputStream console = null;
private Thread thread = null;
private PrintStream Out = null;

public ChatClient(String serverName, int serverPort) {


System.out.println("Establishing connection, please wait...");
try {
socket = new Socket(serverName, serverPort);
System.out.println("Connected: " + socket.toString());
console = new DataInputStream(System.in);
Out = new PrintStream(socket.getOutputStream());
if (thread == null) {
client = new ChatClientThread(this, socket);
thread = new Thread(this);
thread.start();
}
} catch (UnknownHostException e) {
System.out.println("Host unknown: " + e.getMessage());
} catch (IOException ioe) {
System.out.println("Unexpected exception: " + ioe.getMessage());
}
}

public void run() {


while (thread != null) {
try {
Out.println(console.readLine());
Out.flush();
} catch (IOException e) {
System.out.println("Sending error: " + e.getMessage());
stop();
}
}
}

public void handle(String msg) {


if (msg.equals("quit")) {
System.out.println("Goodbye, press RETURN to exit...");
stop();
} else {
System.out.println(msg);
}
}

public void stop() {


if (thread != null) {
thread.stop();
thread = null;
}
try {
if (console != null)
console.close();
if (Out != null)
Out.close();
if (socket != null)
socket.close();
} catch (IOException e) {
System.out.println("Error closing...");
}
if (client != null) {
client.close();
client.stop();
}
}

public static void main(String args[]) {


ChatClient client = null;
if (args.length != 2)
System.out.println("Usage: java ChatClient host port");
else
client = new ChatClient(args[0], Integer.parseInt(args[1]));
}
}

Chat Client Thread Program

import java.net.*;
import java.io.*;

public class ChatClientThread extends Thread {


private ChatClient client = null;
private Socket socket = null;
private DataInputStream In = null;

public ChatClientThread(ChatClient cli, Socket sock) {


client = cli;
socket = sock;
try {
In = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
System.out.println("Error getting input stream: " + e);
client.stop();
}
start();
}

public void close() {


try {
if (In != null)
In.close();
} catch (IOException e) {
System.out.println("Error closing input stream: " + e);
}
}

public void run() {


while (true) {
try {
client.handle(In.readLine());
} catch (IOException e) {
System.out.println("Listening error: " + e.getMessage());
client.stop();
}
}
}
}

Hadoop Installaion Steps

1.install java-Hadoop requires java to run install the penjdk package and sudo opt install openjdk-8-jdk
verify installation and java --version
2.download hadoop-goto the official apache Hadoop website and download the latest stable version and wget https
3.extract hadoop-unzip the downloaded tar file and tar -xzf hadoop-3.3.6.tar.g2
4.move to the desired directory and sudo mv hadoop-3.3.6/usr/local/Hadoop
5.configure Hadoop environment variable and nano ~/.bashrc
add he following lines at end of the file:
export hadoop_ome=usr/local/hadoop-3.3.6
export hadoop_install=$hadoop_home
export hadoop_mapred_home=$hadoop_home
export hadoop_common_home=$hadoop_home
export hadoop_hdes_home=$hadoop_home
export yarn_home=$hadoop_home
export hadoop_common_lib_natue_dir=$hadoop_home/lib/native
export path=$path:$hadoop_home/sbin:$hadoop_home/bin
export hadoop_opts="-djava.library.path=$hadoop_home/lib/native
after adding exit the bashrc
6.configure Hadoop
edit the Hadoop configuration file:
-core-site.xml
-hdfs-site.xml
-mapped-site.xml
-yarn-site.xml
7.format Hadoop filesystem hdfs
$hdfs namenode -format
8.start Hadoop
$start -dfs.sh
$start -yarn.sh
9.verify installation
$jps

You might also like