Java Socket - 1.0
Description
This extension supports the analysis of applications that use Java core socket programming over TCP and UDP protocols.
In what situation should you install this extension?
If your Java application uses Core Socket Communication APIs.
Technology support
| Framework / Library name | Supported | Supported Technology |
|---|---|---|
| java.net | ✅ | Java |
Function Point, Quality and Sizing support
- Function Points (transactions): a green tick indicates that OMG Function Point counting and Transaction Risk Index are supported
- Quality and Sizing: a green tick indicates that CAST can measure size and that a minimum set of Quality Rules exist
| Function Points (transactions) | Quality and Sizing |
|---|---|
| ❌ | ❌ |
Dependencies
Some CAST extensions require the presence of other CAST extensions in order to function correctly. The Java Socket extension requires the following CAST extensions to be installed:
- Universal Linker
- CAST AIP Internal Extension
- The Java Analyzer is required, but it is not a dependency, this extension will be automatically installed when Java source code is delivered for analysis.
Any dependent extensions are automatically downloaded and installed. You do not need to do anything.
Download and installation instructions
The extension will not be automatically downloaded and installed. If you need to use it, you should manually install thenextension using the Application - Extensions interface:

What results can you expect?
Once the analysis/snapshot generation has completed, you can view the below objects and links created.
Objects
| Icon | Description | Comment |
|---|---|---|
![]() |
Java Server Socket | An object to represent server socket with known port |
![]() |
Java Client Socket | An object to represent client socket with known port |
![]() |
Java Unknown Server Socket | An object to represent server socket when port is not resolved |
![]() |
Java Unknown Client Socket | An object to represent client socket when port is not resolved |
Links
| Link Type | Source and Destination Link | Supported Methods |
|---|---|---|
| callLink | From Java Server Socket / Java Unknown Server Socket to Caller Java method | java.net.ServerSocket.accept java.net.DatagramSocket.receive java.net.DatagramSocket.send |
| callLink | From Caller Java method to Java Client Socket / Java Unknown Client Socket | java.net.Socket.Socket java.net.Socket.connect java.net.DatagramSocket.receive java.net.DatagramSocket.send |
Code Examples
Client.java
package com.mycompany.clientside;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Client extends JFrame {
// Global Image
static Image global_img;
/* Main Function */
public static void main(String[] args) {
// Collect inputs from the user
Scanner input = new Scanner(System.in);
/* Read the matrix */
int nodes = 5;
System.out.println("Making a graph with 5 nodes\n");
// Declare the matrix
int adjMatrix[][] = new int[nodes][nodes];
int entry;
// Read the matrix values
System.out.println("Enter the elements of the matrix");
for (int i = 0; i < nodes; i++)
for (int j = 0; j < nodes; j++) {
entry = input.nextInt();
if(entry >= 1)
entry = 1;
else
entry = 0;
adjMatrix[i][j] = entry;
}
// Display the entered matrix
System.out.println("This is the matrix that was entered\n");
StringBuilder s = new StringBuilder();
for (int i = 0; i < nodes; i++) {
s.append((char)(i+ (int)'A') + ": ");
for (int j : adjMatrix[i]) {
s.append((j) + " ");
}
s.append("\n");
}
System.out.print(s.toString());
// Input path length
System.out.println("Enter the path length");
int pathLength = input.nextInt();
// Input starting and ending nodes (convert alphabets to index values)
System.out.println("Enter the start node");
int start = (int)Character.toUpperCase(input.next().charAt(0)) - (int)'A';
System.out.println("Enter the end node");
int end = (int)Character.toUpperCase(input.next().charAt(0)) - (int)'A';
// TCP Connection and communication with the server
try {
// Make a new client side connection
Socket clientSocket = new Socket("localhost", 9000);
// Make a new inputstream object
DataInputStream dataInput = new DataInputStream(clientSocket.getInputStream());
// Create an output stream
DataOutputStream dataOutput = new DataOutputStream(clientSocket.getOutputStream());
// Send data to the server
// Send Path length
dataOutput.writeInt(pathLength);
dataOutput.flush();
// Send start and end
dataOutput.writeInt(start);
dataOutput.flush();
dataOutput.writeInt(end);
dataOutput.flush();
// Send the number of nodes and the matrix
dataOutput.writeInt(nodes);
dataOutput.flush();
for (int i = 0; i < nodes; i++)
for (int j = 0; j < nodes; j++)
dataOutput.writeInt(adjMatrix[i][j]);
dataOutput.flush();
// Read the response input from the server
char response = dataInput.readChar();
// Convert start and end node to alphabets
char startNode = (char)((int)start + (int)'A');
char endNode = (char)((int)end + (int)'A');
String statement = "";
// Check response from the server
if(response == 'Y'){
statement = "Yes, there exists a path of length " + pathLength + " from node " + startNode + " to node " + endNode;
}else if(response == 'N'){
statement = "No, there exists no path of length " + pathLength + " from node " + startNode+ " to node " + endNode;
}
// Print the statement
System.out.println(statement);
/* Load the image */
// getting img from server
byte[] sizeAr = new byte[4];
dataInput.read(sizeAr);
int size = ByteBuffer.wrap(sizeAr).asIntBuffer().get();
byte[] imageArray = new byte[size];
dataInput.read(imageArray);
BufferedImage image = ImageIO.read(new ByteArrayInputStream(imageArray));
global_img = image;
JFrame frame = new Client();
frame.setTitle("Client");
frame.setSize(1000, 1000);
frame.setVisible(true);
System.out.println("Received Bytes " + image.getHeight() + "x" + image.getWidth() + " : " + System.currentTimeMillis());
// Call the constructor to load image
dataOutput.close();
clientSocket.close(); // close the connection
} catch (IOException ex){}
}
}


Server.java
package com.mycompany.serverside;
/* Import java Packages */
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.List;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
public class Server extends JFrame{
// Global adjmatrix
static int[][] globalAdjMatrix = new int[5][5];
// Declare global image
static Image global_img;
// Global socket variable
// static ServerSocket serverSocket;
// static Socket socket;
// static DataInputStream input;
static DataOutputStream output;
/* function to convert adjacency matrix to adjacency list */
public static ArrayList<Integer>[] mat_to_list(int[][] m){
// Collect number of vertices
int vertices = m[0].length;
// Declare an Array
ArrayList<Integer>[] adjList = new ArrayList[vertices];
// Create a new list for each vertex to store the vertices
for (int i = 0; i < vertices; i++) {
adjList[i] = (new ArrayList<Integer>());
}
// Store the vertices in the adjacency list
for (int i = 0; i < m[0].length; i++) {
for (int j = 0; j < m.length; j++) {
if (m[i][j] >= 1) {
adjList[i].add(j);
}
}
}
return adjList;
}
// Set the loaction of the points in the graph
Point A = new Point(220, 80);
Point B = new Point(100, 180);
Point C = new Point(140, 305);
Point E = new Point(340, 180);
Point D = new Point(300, 305);
int drawnEdges1[] = new int[25];
int drawnEdges2[] = new int[25];
public static BufferedImage toBufferedImage(Image img)
{
if (img instanceof BufferedImage)
{
return (BufferedImage) img;
}
// Create a buffered image with transparency
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
// Return the buffered image
return bimage;
}
// Function to visualise the graph nodes and the edges
private Image createGraphImage() {
// Create a buffered image object
BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
return image;
}
/* Function to check if the required path length in the given path lengths */
public static boolean checkPathLength(ArrayList<Integer>[] list, int source, int dest, int vertices, int reqLength) {
// Create an array of visited nodes
boolean[] hasVisited = new boolean[vertices];
ArrayList<Integer> pathList = new ArrayList<>();
// add the source node to the path (subtract 1 from path length)
pathList.add(source);
ArrayList<Integer> pathLength = new ArrayList<>();
// Call recursive DFS function
pathLengthDFS(list, source, dest, pathLength, hasVisited, pathList);
// Return whether the path length exists or not
return pathLength.contains(reqLength);
}
/* Function to recursively check the path and then add to the list of path lengths */
private static void pathLengthDFS(ArrayList<Integer>[] adjList, Integer s, Integer d, List<Integer> lengths, boolean[] hasVisited, List<Integer> funcPathList) {
if (s.equals(d)) {
// Add the path length to the path lengths list (subtract 1 to remove source node)
lengths.add(funcPathList.size()-1);
// If we have found a matching node then we can directly return after adding the length to tha path lenght
return;
}
// Mark the current node as visited
hasVisited[s] = true;
// Recur to all the adjacent nodes for all the vertices
for (Integer i : adjList[s]) {
if (!hasVisited[i]) {
// store current node in the path to begin traversal
funcPathList.add(i);
pathLengthDFS(adjList, i, d, lengths, hasVisited, funcPathList);
// remove current node from the path
funcPathList.remove(i);
}
}
// Mark the current node
hasVisited[d] = false;
}
public static void main(String args[])throws Exception{
try{
// create a server socket and bind it to the port number
ServerSocket serverSocket = new ServerSocket(9000);
System.out.println("Server has been started");
while(true){
// Create a new socket to establish a virtual pipe
// with the client side (LISTEN)
Socket socket = serverSocket.accept();
// Create a datainput stream object to communicate with the client (Connect)
DataInputStream input = new DataInputStream(socket.getInputStream());
// Create a dataoutput stream object to communicate with the client (Connect)
output = new DataOutputStream(socket.getOutputStream());
// Read the data from the client (Receieve)
int pathLength = input.readInt();
int start = input.readInt();
int end = input.readInt();
// Collect the nodes and the matrix through the data
int nodes = input.readInt();
for (int i = 0; i < nodes; i++)
for (int j = 0; j < nodes; j++)
globalAdjMatrix[i][j] = input.readInt();
// Convert adjacency matrix to adjacency list
ArrayList<Integer>[] adjList = new ArrayList[nodes];
adjList = mat_to_list(globalAdjMatrix);
// Check whether path length is present in the array
boolean pathExists = checkPathLength(adjList, start, end, nodes, pathLength);
// Send the Y or N to the client
char response;
if(pathExists)
response = 'Y';
else
response = 'N';
// Send the response
output.writeChar(response);
}
} catch(IOException e){}
}
}


Limitations
- MulticastSocket is not supported.
- Linking between Java Server Socket and Java Client Socket objects is not supported.



