A collection of Java socket programming examples for various use cases, including factorials, message sending, handling accounts, and supporting multiple clients.
This repository includes four main packages demonstrating different aspects of socket programming in Java:
- Factorial Calculation: Demonstrates a simple client-server application where the client sends a number to the server, and the server returns the factorial of that number.
- Message Sending: Shows a basic example of sending a string message from the client to the server, and the server responds back.
- Handling Accounts: Illustrates sending a serializable Java object (a user account) from the server to the client.
- Multi-Clients: Demonstrates handling multiple clients in a server by using threads, allowing simultaneous processing of client requests.
To run any of these examples, you need to have Java installed on your machine. Each main class can be executed separately to start the server or the client side of the application.
- Java Development Kit (JDK) installed.
- Basic understanding of client-server architecture.
- Knowledge of Java sockets and threading for the multi-client example.
-
Clone the repository:
git clone https://github.com/yourusername/Sockets.git
-
Navigate to the desired package, for example:
cd Sockets/src/ma/fstm/ilisi/sockets/factoriel
-
Compile the server and client classes:
javac *.java
-
Run the server application first:
java Server
-
Open another terminal window and run the client application:
java Client
Follow similar steps to run other examples in the repository.
Package: ma.fstm.ilisi.sockets.factoriel
Classes: Client
, Server
The client sends an integer to the server, which calculates the factorial of the number and sends the result back to the client.
Package: ma.fstm.ilisi.sockets.message
Classes: Client
, Server
The client sends a string message to the server. The server receives this message, prints it, and responds with a confirmation message.
Package: ma.fstm.ilisi.sockets.compte
Classes: Client
, Server
, Compte
Demonstrates the serialization and deserialization of a Java object (Compte) over sockets. The server sends a serialized Compte object to the client, which deserializes and prints the account details.
Package: ma.fstm.ilisi.sockets.multiclients
Classes: Client
, Server
Illustrates a server that can handle multiple clients simultaneously by spawning a new thread for each client connection.
Feel free to fork this repository and submit pull requests to contribute to these examples.