0% found this document useful (0 votes)
17 views10 pages

04 Exp

The document outlines a lab exercise focused on port scanning, detailing the aim, pre-lab concepts such as TCP sockets and port types, and in-lab programs for establishing client-server connections using TCP sockets. It includes algorithms and sample code for both client and server applications, as well as instructions for scanning active ports on a local host. The document concludes with a post-lab section for verifying active ports and scanning using UDP sockets.

Uploaded by

srinivaas
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)
17 views10 pages

04 Exp

The document outlines a lab exercise focused on port scanning, detailing the aim, pre-lab concepts such as TCP sockets and port types, and in-lab programs for establishing client-server connections using TCP sockets. It includes algorithms and sample code for both client and server applications, as well as instructions for scanning active ports on a local host. The document concludes with a post-lab section for verifying active ports and scanning using UDP sockets.

Uploaded by

srinivaas
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

Ex.

No:4
Port Scanning
Date:

Aim:

To establish a connection between the server and the client by means of


scanning active ports available at that time.

Pre-Lab:

1. Describe TCP socket.


▪ A TCP socket is an endpoint for communication between two machines over a
Transmission Control Protocol (TCP) connection. TCP sockets provide a
reliable, connection-oriented, byte-stream service.
▪ A server typically creates a socket, binds it to a port, and listens for
incoming client connections.
▪ A client creates a socket and connects it to the server's socket.
▪ TCP ensures data arrives in order, without duplication, and handles
retransmissions in case of lost packets.

2. Describe port scanning.


▪ Port scanning is the process of sending packets to specific ports on a host and
analyzing the responses to find open, closed, or filtered ports.
▪ Purpose:
▪ Discover open ports and services on a networked system.
▪ Identify potential security vulnerabilities.
▪ Used by both network administrators and attackers.
▪ Example tools: nmap, netcat.

3. What are the types of ports? Give their range.


There are three types of ports in TCP/IP networking:
▪ Well-Known Ports (0–1023):
Used by standard services like HTTP, FTP, SSH, etc.
(Require administrative privileges to use on most systems.)
▪ Registered Ports (1024–49151):
Used by user or vendor-specific services.
Not assigned to specific applications, but can be registered with IANA.
▪ Dynamic/Private/Ephemeral Ports (49152–65535):
Temporarily assigned by the OS to client applications for short-lived
communications.
4. Give example for well-known ports.

Service Port Number


HTTP 80
HTTPS 443
FTP 21
SSH 22
DNS 53
SMTP 25

5. Write the syntax and its explanation for the following:

a. ServerSocket()
b. Socket()
c. accept()
d. DatagramSocket()
e. getPort()
f. getInetAddress()

i. ServerSocket()
Syntax:
ServerSocket server = new ServerSocket(portNumber);
Explanation:
This statement creates a server-side socket that listens for incoming TCP
connections on the specified portNumber. It is used by the server to establish
communication with client systems.

ii. Socket()
Syntax:
Socket socket = new Socket("hostname", portNumber);
Explanation:
This creates a client-side socket and connects it to a server at the given hostname
and portNumber. It is used by the client to initiate a TCP connection to a server.

iii. accept()
Syntax:
Socket clientSocket = server.accept();
Explanation:
The accept() method waits for a client to establish a connection. Once a
connection is received, it returns a new Socket object to handle communication
with that specific client.
iv. DatagramSocket()
Syntax:
DatagramSocket socket = new DatagramSocket();
or
DatagramSocket socket = new DatagramSocket(portNumber);
Explanation:
This creates a UDP socket. The first form binds it to an automatically assigned
port, while the second form binds it to a specified port number. It is used for
connectionless communication using datagrams.

v. getPort()
Syntax:
int port = socket.getPort();
Explanation:
The getPort() method returns the remote port number to which the socket is
connected. It helps identify the port used by the remote host in the connection.

vi. getInetAddress()
Syntax:
InetAddress address = socket.getInetAddress();
Explanation:
This method returns the remote IP address to which the socket is connected. It
is useful for retrieving the address of the host on the other end of the connection.
In-Lab Programs:
1. Establish a connection between client and server using TCP socket and find the socket
addresses of client and server. Print the local port number assigned by the client.

Algorithm:

Client Side:
i. Start the client program.
ii. Accept server port number as command-line argument.
iii. Specify the server’s IP address.
iv. Create a Socket object to connect to the server.
v. Print connection success message and socket details.
vi. Close the connection.

Server Side:
i. Start the server program.
ii. Accept port number as command-line argument.
iii. Create a ServerSocket and bind it to the given port.
iv. Wait for a client to connect using accept().
v. Print success message and TCP socket information.
vi. Retrieve and print the port number assigned to the client.
vii. Close the connection..

Program:

Client side:
import java.net.*;

class Client {
public static void main(String args[]) {
try {
int serverPort = Integer.parseInt(args[0]);
String serverIP = "127.0.0.1"; // Localhost (can replace with server IP)

Socket soc = new Socket(serverIP, serverPort);


System.out.println("Connection successful with server");
System.out.println("Client socket info: " + soc);
System.out.println("Local port assigned to client: " + soc.getLocalPort());
soc.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}
Server side:
import java.net.*;

class Server {
public static void main(String args[]) {
try {
int port = Integer.parseInt(args[0]);
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Waiting for connection with client...");

Socket soc = serverSocket.accept();


System.out.println("Successful connection with client");
System.out.println("TCP socket info: " + soc);
System.out.println("Local port assigned by the client: " + soc.getPort());

soc.close();
serverSocket.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}

Output:

Connection successful with server


Client socket info: Socket[addr=/127.0.0.1,port=5000,localport=52678]
Local port assigned to client: 52678

Waiting for connection with client...


Successful connection with client
TCP socket info: Socket[addr=/127.0.0.1,port=52678,localport=5000]
Local port assigned by the client: 52678

2. Scan/trace the given range of ports of server

Algorithm:

Server Side:
i. Start the server program.
ii. Accept a port number as a command-line argument.
iii. Create a ServerSocket and wait for a client connection using accept().
iv. Create an output stream and send a message to the client.
v. Close the socket and server.

Client Side:
i. Start the client program.
ii. Accept server port number as a command-line argument.
iii. Create a Socket to connect to the server using IP and port.
iv. Create an input stream to receive the message from the server.
v. Print the message and close the socket.

Program:

Client side:
import java.net.*;
import java.io.*;

class Client2 {
public static void main(String args[]) {
try {
int port = Integer.parseInt(args[0]);
String serverIP = "127.0.0.1";

Socket socket = new Socket(serverIP, port);


System.out.println("Connected to server.");

InputStream is = socket.getInputStream();
DataInputStream dis = new DataInputStream(is);

String message = dis.readUTF();


System.out.println("Message from server: " + message);

dis.close();
socket.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}

Server side:
import java.net.*;
import java.io.*;

class Server2 {
public static void main(String args[]) {
try {
int port = Integer.parseInt(args[0]);
ServerSocket serverSocket = new ServerSocket(port);
System.out.println("Server is waiting for a client...");

Socket socket = serverSocket.accept();


System.out.println("Client connected.");
OutputStream os = socket.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);

dos.writeUTF("Hello Client! Welcome to the TCP server.");


System.out.println("Message sent to client.");

dos.close();
socket.close();
serverSocket.close();
} catch (Exception e) {
System.out.println("Error: " + e);
}
}
}

Output:
Server is waiting for a client...
Client connected.
Message sent to client.

Connected to server.
Message from server: Hello Client! Welcome to the TCP server.
Post-Lab

1. Find all active ports of your local host and verify your result using a networking tool (Windows)
Step 1 – Using Command Prompt:
Open Command Prompt and run the following command:
▪ netstat -an | find "LISTENING"
This will display all active and listening ports on your local host.
Step 2 – Verifying with a networking tool (CurrPorts):

Figure 1: CurrPorts (Networking tool) and Command Prompt window showing listening ports on my local host

2. Scan the port by UDP socket

Program:
Inference:

Criteria Marks

Pre-Lab /10

Programs with Algorithms /25

Quiz/Viva /20

Submission on Time /10

Post-Lab & Inference / 10

Total /75

Faculty Signature with Date

You might also like