Skip to main content

JavaProgramForEchoServerAndEchoClient

 

Java:


Program:

EchoServer:


import java.io.*;

import java.net.*;


public class EServer {

public static void main(String args[]) {

try (ServerSocket s = new ServerSocket(9000);

Socket c = s.accept();

DataInputStream is = new DataInputStream(c.getInputStream());

PrintStream ps = new PrintStream(c.getOutputStream())) {


while (true) {

ps.println(is.readLine());

}

} catch (IOException e) {

System.out.println(e);

}

}

}


EchoClient:


import java.io.*;

import java.net.*;


public class EClient {

public static void main(String arg[]) {

try (Socket c = new Socket(InetAddress.getLocalHost(), 9000);

PrintStream os = new PrintStream(c.getOutputStream());

DataInputStream is = new DataInputStream(System.in);

DataInputStream is1 = new DataInputStream(c.getInputStream())) {


while (true) {

System.out.print("Client: ");

String line = is.readLine();

os.println(line);


System.out.println("Server: " + is1.readLine());

}

} catch (IOException e) {

System.out.println("Socket Closed!");

}

}

}


Output:


Server

C:\Program Files\Java\jdk1.5.0\bin>javac EServer.java

C:\Program Files\Java\jdk1.5.0\bin>java EServer

C:\Program Files\Java\jdk1.5.0\bin>

Client

C:\Program Files\Java\jdk1.5.0\bin>javac EClient.java

C:\Program Files\Java\jdk1.5.0\bin>java EClient

Client: Hai Server Server:Hai Server

Client: Hello Server:Hello

Client:end Server:end

Client:ds

Socket Closed!


Chat


Chat Server:


import java.io.*;

import java.net.*;


public class ChatServer {

public static void main(String[] args) {

try (ServerSocket serverSocket = new ServerSocket(9000)) {

System.out.println("Chat Server is running. Waiting for clients to connect...");


Socket clientSocket = serverSocket.accept();

System.out.println("Client connected: " + clientSocket.getInetAddress());


BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);


BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in));


while (true) {

// Read message from the client

String clientMessage = reader.readLine();

if (clientMessage == null || clientMessage.equalsIgnoreCase("bye")) {

break;

}

System.out.println("Client: " + clientMessage);


// Read message from the server console and send it to the client

System.out.print("Server: ");

String serverMessage = consoleReader.readLine();

writer.println(serverMessage);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}


Chat Client:


import java.io.*;

import java.net.*;


public class ChatClient {

public static void main(String[] args) {

try (Socket clientSocket = new Socket("localhost", 9000);

BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);

BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in))) {


System.out.println("Connected to Chat Server.");


while (true) {

// Read message from the server

String serverMessage = reader.readLine();

if (serverMessage == null || serverMessage.equalsIgnoreCase("bye")) {

break;

}

System.out.println("Server: " + serverMessage);


// Read message from the client console and send it to the server

System.out.print("Client: ");

String clientMessage = consoleReader.readLine();

writer.println(clientMessage);

}

} catch (IOException e) {

e.printStackTrace();

}

}

}


Output:


# Terminal 1 (ChatServer)


Chat Server is running. Waiting for clients to connect...

Client connected: /127.0.0.1

Client: Hello Server

Server: Hi there! How can I help you?

Client: What's the weather like today?

Server: I'm just a simple chat server. I don't know about the weather!

Client: bye


# Terminal 2 (ChatClient)


Connected to Chat Server.

Server: Hi there! How can I help you?

Client: How are you doing?

Server: I'm just a simple chat server. I don't have feelings!

Client: bye


File Transfer


FileTransferServer.java:



import java.io.*;

import java.net.*;



public class FileTransferServer {

private static final int PORT = 9000;

private static final String FILE_DIRECTORY = "files/";



public static void main(String[] args) {

try (ServerSocket serverSocket = new ServerSocket(PORT)) {

System.out.println("File Transfer Server is running. Waiting for clients to connect...");



while (true) {

Socket clientSocket = serverSocket.accept();

System.out.println("Client connected: " + clientSocket.getInetAddress());



try (BufferedReader reader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true)) {



String fileName = reader.readLine();

System.out.println("Client requested file: " + fileName);



try {

sendFile(fileName, writer);

} catch (FileNotFoundException e) {

writer.println("File not found: " + fileName);

}

}

}

} catch (IOException e) {

e.printStackTrace();

}

}



private static void sendFile(String fileName, PrintWriter writer) throws IOException {

String filePath = FILE_DIRECTORY + fileName;

try (BufferedReader fileReader = new BufferedReader(new FileReader(filePath))) {

String line;

while ((line = fileReader.readLine()) != null) {

writer.println(line);

}

writer.println("FileTransferComplete"); // Signal to the client that the file transfer is complete

System.out.println("File transfer successful.");

}

}

}



FileTransferClient.java:



import java.io.*;

import java.net.*;



public class FileTransferClient {

private static final int PORT = 9000;



public static void main(String[] args) {

try (Socket socket = new Socket("localhost", PORT);

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));

PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

BufferedReader consoleReader = new BufferedReader(new InputStreamReader(System.in))) {



System.out.println("Connected to File Transfer Server.");



System.out.print("Enter the file name: ");

String fileName = consoleReader.readLine();

writer.println(fileName);



String response = reader.readLine();

if (response.startsWith("File not found")) {

System.out.println(response);

} else {

System.out.println("File content received:");

while (true) {

String line = reader.readLine();

if (line.equals("FileTransferComplete")) {

break;

}

System.out.println(line);

}

}

} catch (IOException e) {

e.printStackTrace();

}

}

}


FileTransferServer Output:


File Transfer Server is running. Waiting for clients to connect...

Client connected: /127.0.0.1

Client requested file: sample.txt

File transfer successful.


FileTransferClient Output:


Connected to File Transfer Server.

Enter the file name: sample.txt

File content received:

This is a sample file.

It contains some text for testing.

FileTransferComplete

Comments

Popular posts from this blog

Install TensorFlow on Windows 11: Step-by-Step Guide for CPU & GPU

 --- Installing **TensorFlow on Windows 11** requires setting up system dependencies, configuring Python, and ensuring compatibility with CPU or GPU acceleration. This step-by-step guide provides everything needed to install **TensorFlow 2.10 or lower** on **Windows Native**, including software prerequisites, Microsoft Visual C++ Redistributable installation, Miniconda setup, GPU driver configuration, and verification steps.   ### **System Requirements:**   Before installing TensorFlow, ensure your system meets these requirements:   - **Operating System:** Windows 7 or higher (64-bit)   - **Python Version:** 3.9–3.12   - **pip Version:** 19.0 or higher for Linux and Windows, 20.3 or higher for macOS   - **Microsoft Visual C++ Redistributable:** Required for Windows Native   - **Long Paths Enabled:** Ensure long paths are enabled in Windows settings   For **GPU support**, install:   - **NVIDIA ...

Unreal Engine Product Showcase: Mesmerizing Video Sequence Render

  4k Image:

Cloudflare Is Down Worldwide: What Happened, What It Means & What You Can Do

🌍 Cloudflare Is Down Worldwide: What Happened & Why the Internet Broke Today On 18 November 2025 , the internet experienced one of the largest global outages in years as Cloudflare , the backbone of countless websites and online services, went down unexpectedly. The outage caused millions of websites to become slow, unreachable, or return 5xx internal server errors , leading to widespread disruption across businesses, apps, and essential online platforms. This blog post explains what happened, why the internet broke for many users, and what Cloudflare has officially said so far. What Exactly Happened? Around 4:30 PM IST , reports began flooding social media and outage trackers like Downdetector. Users across India, Europe, the US, and Southeast Asia noticed that: Websites were not loading Requests were timing out Services dependent on Cloudflare CDN or DNS stopped responding APIs hosted through Cloudflare were failing Even some security and protection layers were ...