download
01 programs
import java.net. * ;
import java.io.*;
class Ports
{
public static void main (String [] args) throws Exception
{
InetAddress h = InetAddress.getLocalHost();
String host =h. getHostName();
String haddr = h. getHostAddress();
System.out.println("\nHost Name: :"+ host);
System.out.println("In Host Address:: "+haddr);
for (int i=0; i < 1024; i++)
{
Socket s=null;
try
{
s = new Socket ((host), i);
System.out.println(" port "+ i + "Active");
}
catch (SocketException se)
{
}
catch(Exception e)
{
System.out.println(" Error....");
}
}
}
}
Output:-
02 -program
2 a)
import java.io.*;
import java.net. *;
import java.lang. * ;
class chats extends Thread
{
public static void main (String args [])
{
try
{
ServerSocket ss = new ServerSocket (111);
System.out.println ("server ready ");
Socket s = ss.accept ();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedReader br = new BufferedReader(new InputStreamReader (s. getInputStream()));
PrintWriter pw=new PrintWriter (new OutputStreamWriter(s. getOutputStream()));
while(true)
{
String msg = br.readLine();
System.out.println(" from client: "+msg);
if (msg. equals ("quit "))
{
System.out. println("disabled");
break;
}
System.out.println("server");
msg = in.readLine();
pw.println (msg);
pw.flush();
}
}
catch (IOException e)
{
System.out.println ("error");
}
}
}
02) b)
import java .net.*;
import java.io.*;
import java.lang.*;
public class chat
{
public static void main(String[]args)
{
try
{
Socket s = new Socket("localhost", 111);
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));
while(true)
{
System.out.println("client");
String msg=in.readLine();
pw.println("msg");
pw.flush();
String from=br.readLine();
System.out.println("from server="+from);
if(from.equals("quit"))
{
System.out.println("disconnect");
System.exit(0);
}
}
}
catch(IOException e)
{
System.out.println("ending");
}
}
}
03) a)
import java.io.*;
import java.net.*;
import java.sql.*;
public class JDBCServer {
private static final String DB_URL = "jdbc:mysql://localhost:3306/soft";
private static final String USER = "root";
private static final String PASSWORD = "1234";
public static void main(String[] args) {
try (ServerSocket serverSocket = new ServerSocket(5000)) {
System.out.println("Server is listening on port 5000...");
while (true) {
Socket socket = serverSocket.accept();
System.out.println("Client connected.");
try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
Connection connection = DriverManager.getConnection(DB_URL, USER, PASSWORD)) {
String query = in.readLine(); // Read query from client
System.out.println("Received query: " + query);
try (Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery(query)) {
while (rs.next()) {
out.println(rs.getString(1)); // Send results to client (adjust as needed)
}
}
out.println("END"); // Indicate the end of results
} catch (SQLException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3) b)
import java.io.*;
import java.net.*;
public class JDBCClient {
public static void main(String[] args) {
String serverAddress = "localhost"; // Server address
int port = 5000; // Server port
try (Socket socket = new Socket(serverAddress, port);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {
String query = "SELECT * FROM your_table"; // Replace with your actual query
out.println(query); // Send query to server
System.out.println("Results from server:");
String response;
while ((response = in.readLine()) != null) {
if ("END".equals(response)) {
break; // End of results
}
System.out.println(response);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
4A)
import java.io.*;
import java.net.*;
public class FTPServer1
{
public static void main(String[] args)
{
try (ServerSocket ss = new ServerSocket(9000))
{
System.out.println("Server started on port 9000.");
while (true)
{
try (Socket s = ss.accept();
DataInputStream id = new DataInputStream(s.getInputStream());
PrintStream p = new PrintStream(s.getOutputStream()))
{
System.out.println("Client connected.");
// Read the filename from the client
String filename = id.readLine();
System.out.println("Requested file: " + filename);
// Open the requested file
File file = new File(filename);
if (!file.exists() || file.isDirectory())
{
p.println("Error: File not found.");
System.out.println("File not found: " + filename);
continue;
}
try (FileInputStream fis = new FileInputStream(file))
{
int ch;
while ((ch = fis.read()) != -1)
{
p.write(ch); // Send file content to client
}
System.out.println("File sent successfully.");
} catch (IOException e)
{
System.err.println("Error reading file: " + e.getMessage());
}
} catch (IOException e)
{
System.err.println("Connection error: " + e.getMessage());
}
}
} catch (IOException e)
{
System.err.println("Server error: " + e.getMessage());
}
}
}
4B)
import java.io. *;
import java.net. *;
public class ftpclient
{
public static void main(String args[]) throws Exception
{
Socket s = new Socket ("Localhost", 9000);
BufferedReader d1 =new BufferedReader (new InputStreamReader (s.getInputStream()));
InputStream in = s. getInputStream();
DataInputStream di = new DataInputStream (System.in);
PrintStream p = new PrintStream (s.getOutputStream());
System. out. println ("Enter absolute. Path") ;
String st = di. readLine();
p. println (st);
System.out.println("enter destination");
String file = di. readLine();
FileOutputStream fos = new FileOutputStream(file);
int ch;
while ((ch=d1.read() )!= -1)
{
System.out.println ((char)ch);
fos. write (ch);
}
}
}
5a)
import java.io.*;
import java.net.*;
// Server Thread Class
class tftpms extends Thread
{
private DatagramSocket ds;
// Constructor
tftpms(DatagramSocket ds) {
this.ds = ds;
start(); // Start the thread
}
@Override
public void run() {
try {
int POS = 0;
int buff_size = 2000;
byte[] buff = new byte[buff_size];
// Receiving packet
DatagramPacket dp = new DatagramPacket(buff, buff.length);
ds.receive(dp);
// Extracting the filename
String filename = new String(dp.getData(), 0, dp.getLength());
System.out.println("Requested file: " + filename);
// Reading the file
FileInputStream fis = new FileInputStream(filename);
int c;
while ((c = fis.read()) != -1) {
buff[POS++] = (byte) c;
// Send data when buffer is full
if (POS == buff_size) {
ds.send(new DatagramPacket(buff, POS, InetAddress.getLocalHost(), 50));
POS = 0; // Reset the buffer position
}
}
// Sending the remaining data
if (POS > 0) {
ds.send(new DatagramPacket(buff, POS, InetAddress.getLocalHost(), 50));
}
fis.close(); // Close the file input stream
System.out.println("File sent successfully.");
} catch (Exception e) {
System.err.println("Error in file transfer: " + e.getMessage());
e.printStackTrace();
}
}
}
// Main Server Class
class tftpserver {
public static void main(String args[]) throws Exception {
System.out.println("Server is ready...");
// Initialize DatagramSocket on port 69
DatagramSocket ds = new DatagramSocket(69);
// Start the server thread
new tftpms(ds);
}
}
5b)
import java.io.*;
import java.net.*;
// TFTP Client Class
class tftpclient1 {
public static void main(String[] args) {
new tftpc();
}
}
// Client Logic Class
class tftpc {
private DatagramSocket ds;
private int buff_Size = 2000;
private byte[] buff = new byte[buff_Size];
// Constructor
tftpc() {
try {
// Initialize DatagramSocket
ds = new DatagramSocket(50);
} catch (Exception e) {
System.err.println("Socket initialization error: " + e.getMessage());
System.exit(0);
}
try (DataInputStream in = new DataInputStream(System.in)) {
// Get file path from user
System.out.println("Enter the absolute path of the file: ");
String str = in.readLine();
int pos = str.length();
byte[] buf = str.getBytes();
// Send file request to the server
ds.send(new DatagramPacket(buf, pos, InetAddress.getLocalHost(), 69));
System.out.println("Enter the name to save the file as: ");
String stt = in.readLine();
// Create file output stream to save the received file
FileOutputStream fos = new FileOutputStream(stt);
while (true) {
// Receive data packets from the server
DatagramPacket dp = new DatagramPacket(buff, buff.length);
ds.receive(dp);
// Check if the server has completed sending data
if (dp.getLength() == 0) {
System.out.println("File transfer complete.");
break;
}
// Write received data to the file
fos.write(dp.getData(), 0, dp.getLength());
}
// Close the file output stream
fos.close();
System.out.println("File saved successfully.");
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
} finally {
if (ds != null && !ds.isClosed()) {
ds.close(); // Close the DatagramSocket
}
}
}
}
Comments
Post a Comment