0% found this document useful (0 votes)
13 views4 pages

Client Server

The document provides a Java implementation of a simple client-server application using sockets. The client connects to the server, sends messages, and receives responses until 'exit' is typed, while the server listens for connections, processes client messages, and responds accordingly. It includes detailed explanations of the code structure and functionality for both the client and server classes.

Uploaded by

tesfawamare19125
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)
13 views4 pages

Client Server

The document provides a Java implementation of a simple client-server application using sockets. The client connects to the server, sends messages, and receives responses until 'exit' is typed, while the server listens for connections, processes client messages, and responds accordingly. It includes detailed explanations of the code structure and functionality for both the client and server classes.

Uploaded by

tesfawamare19125
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

LAB -- 1 client – server

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

public class client {


public static void main(String[] args) {
final String serverAddress = "[Link]"; // IP address of the server
final int port = 12345; // Port number of the server
try {
// Create a socket to connect to the server
Socket socket = new Socket(serverAddress, port);

// Get the input and output streams of the socket


OutputStream outputStream = [Link]();
InputStream inputStream = [Link]();

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));


PrintWriter writer = new PrintWriter(outputStream, true);

// Create a BufferedReader to read input from the console


BufferedReader consoleReader = new BufferedReader(new InputStreamReader([Link]));

while (true) {
// Read input from the console
[Link]("Client: ");
String clientMessage = [Link]();

// Send the message to the server


[Link](clientMessage);

// Receive a response from the server


String serverResponse = [Link]();
[Link]("Server: " + serverResponse);

// Check if the client wants to exit


if ([Link]("exit"))
break;
}
// Close the socket
[Link]();
} catch (IOException e) {
[Link]();
}
}
}

Page | 1
LAB -- 1 client – server

import [Link].*;
import [Link].*;
public class Server {
public static void main(String[] args) {
final int port = 12345; // Port number the server will listen on
try {
// Create a server socket bound to the specified port
ServerSocket serverSocket = new ServerSocket(port);

[Link]("Server is listening on port " + port);

// Wait for a client to connect


Socket clientSocket = [Link]();

[Link]("Client connected");

// Get the input and output streams of the client socket


InputStream inputStream = [Link]();
OutputStream outputStream = [Link]();

BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));


PrintWriter writer = new PrintWriter(outputStream, true);

while (true) {
// Read a message from the client
String clientMessage = [Link]();
[Link]("Client: " + clientMessage);

// Respond to the client


[Link]("Server received: " + clientMessage);

// Check if the server should exit


if ([Link]("exit"))
break;
}
// Close the client socket
[Link]();
// Close the server socket
[Link]();
} catch (IOException e) {
[Link]();
}
}
}

Page | 2
LAB -- 1 client – server

Let's break down the code step by step and explain what each part does:
Client Class:
1. **Initialization**:
- The client program initializes the server's IP address (`serverAddress`) and port
number (`port`) to establish a connection.
2. **Socket Creation**:
- The `Socket` object is created using the server's IP address and port number. This socket
represents the client's endpoint of the connection.
3. **Input/Output Streams**:
- Input and output streams (`outputStream` and `inputStream`) are obtained from the
socket. These streams are used to send data to and receive data from the server.
4. **Reading User Input**:
- A `BufferedReader` (`consoleReader`) is created to read input from the console.
5. **Sending and Receiving Messages**:
- The client enters a loop where it reads a message from the console, sends it to the server
via the output stream (`[Link](clientMessage)`), and then reads the server's
response from the input stream (`[Link]()`).
6. **Closing the Socket**:
- The client closes the socket after the conversation is over or when the user types "exit".

Server Class:
1. **Initialization**:
- The server program initializes the port number (`port`) on which it will listen for
incoming connections.
2. **Server Socket Creation**:
- A `ServerSocket` object is created, binding it to the specified port. This socket
represents the server's endpoint for accepting connections from clients.
3. **Accepting Client Connection**:
- The server enters a loop where it waits for a client to connect. When a client connects,
the `accept()` method returns a `Socket` object representing the client's connection.
4. **Input/Output Streams**:
- Input and output streams (`inputStream` and `outputStream`) are obtained from the
client's socket. These streams are used to receive data from and send data to the client.

Page | 3
LAB -- 1 client – server

5. **Receiving and Responding to Messages**:


- The server enters a loop where it reads a message from the client via the input stream
(`[Link]()`), processes the message if needed, and then sends a response back to
the client via the output stream (`[Link](...)`).

6. **Closing the Socket and Server Socket**:


- The server closes the client's socket after the conversation is over or when the client
sends "exit". Then, the server socket is closed to release the port and stop listening for new
connections.

These steps outline the basic functionality of the client and server classes in the provided
Java socket programming example. The client sends messages to the server, and the server
responds to each message received from the client. The conversation continues until either
the client or the server terminates the connection.

Page | 4

You might also like