0% found this document useful (0 votes)
2 views5 pages

Sockets in Java

Uploaded by

Archange
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)
2 views5 pages

Sockets in Java

Uploaded by

Archange
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

Implementing Distributed Information Management System using Socket with JAVA swing

A Distributed Information Management System means Multiple clients (users) connected over a
network with a central server (or multiple servers) managing data

The Clients can

1. Send requests (add/update/query data)

2. Receive responses in real-time

Java Swing is used for GUI (forms, tables, buttons) and Sockets ([Link]) are used as
communication layer

The architecture view

1. Server (Core of the system)

• Runs continuously

• Handles multiple clients using threads

• Stores/manages shared data (e.g., database or in-memory)

Key classes:

• ServerSocket

• Socket

• Thread / ExecutorService

2. Client (Swing Application)

• GUI interface

• Connects to server via socket

• Sends/receives data

Key parts:

• Swing UI (JFrame, JTable, JTextField)

• Socket connection to server

• Background thread for listening

The Communication Flow


1. Client connects to server

2. Client sends request (e.g., "ADD_USER")

3. Server processes it

4. Server sends response

5. Client updates UI

Simple Java Example

Server side

import [Link].*;
import [Link].*;

public class Server {


public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(5000);
[Link]("Server started...");

while (true) {
Socket client = [Link]();
new ClientHandler(client).start();
}
}
}

class ClientHandler extends Thread {


private Socket socket;

public ClientHandler(Socket socket) {


[Link] = socket;
}

public void run() {


try {
BufferedReader in = new BufferedReader(
new InputStreamReader([Link]()));
PrintWriter out = new PrintWriter(
[Link](), true);
String message;
while ((message = [Link]()) != null) {
[Link]("Received: " + message);

// Simple response
[Link]("Server received: " + message);
}

} catch (IOException e) {
[Link]();
}
}
}

Swing Client side

import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;

public class ClientGUI {


private Socket socket;
private PrintWriter out;
private BufferedReader in;

public ClientGUI() {
JFrame frame = new JFrame("Client");
JTextField input = new JTextField(20);
JTextArea area = new JTextArea(10, 30);
JButton sendBtn = new JButton("Send");

[Link](new [Link]());
[Link](input);
[Link](sendBtn);
[Link](new JScrollPane(area));

try {
socket = new Socket("localhost", 5000);
out = new PrintWriter([Link](), true);
in = new BufferedReader(
new InputStreamReader([Link]()));
} catch (Exception e) {
[Link]();
}

[Link](e -> {
String msg = [Link]();
[Link](msg);
});

// Listen for server messages


new Thread(() -> {
try {
String response;
while ((response = [Link]()) != null) {
[Link](response + "\n");
}
} catch (IOException e) {
[Link]();
}
}).start();

[Link](400, 300);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
}

public static void main(String[] args) {


new ClientGUI();
}
}

Data Management
• CRUD operations (Create, Read, Update, Delete)

• You can use

o MySQL / PostgreSQL OR

o File system OR

o In-memory structures

Multi-user support

• Login system

• Role-based access

You might also like