0% found this document useful (0 votes)
19 views1 page

Lab 3 Socket Prohramming 1

Uploaded by

Imy
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)
19 views1 page

Lab 3 Socket Prohramming 1

Uploaded by

Imy
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
You are on page 1/ 1

Lab 3 Exercise: Understanding Socket Programming with Java

**Objective:**
This lab aims to provide you with hands-on experience in socket programming using Java. By
the end of this session, you should be able to create a basic client-server communication
system, comprehend socket concepts, and implement a simple messaging protocol.

**Requirements:**
1. Java Development Kit (JDK)
2. Code editor (e.g., IntelliJ, Eclipse)

**Instructions:**

**1. Server Implementation:**


- Open your code editor and create a new Java class named `Server`.
- Inside the `Server` class, implement the following steps:
a. Create a `ServerSocket` and bind it to a specific port (e.g., 5000).
b. Print a message indicating that the server is listening on the chosen port.
c. Accept incoming client connections using `serverSocket.accept()`.
d. Create input and output streams for communication.
e. Read and display the message sent by the client.
f. Send a response back to the client.
g. Close streams and sockets properly.

**2. Client Implementation:**


- In the same code editor, create a new Java class named `Client`.
- Inside the `Client` class, implement the following steps:
a. Connect to the server using `Socket` and specify the server's address (localhost) and
port (5000).
b. Create input and output streams for communication.
c. Send a message to the server.
d. Read and display the server's response.
e. Close streams and socket.

**3. Execution Order:**


- To observe the communication flow, execute the `Client` class first.
- Subsequently, execute the `Server` class.

**4. Expected Output:**


- After executing the `Client` class, expect a message like "Hello, Server!" to be sent to the
server.
- Upon server execution, anticipate seeing "Client says: Hello, Server!" and a response like
"Message received. Connection closed."
- The `Client` class should then display "Server response: Message received. Connection
closed.".

You might also like