0% found this document useful (0 votes)
7 views

prac9

The document outlines the steps to implement Java client-server communication using networking concepts. It includes creating a Java project with two packages: 'server' and 'client', and provides the code for both Server.java and Client.java. The server listens for client connections and exchanges messages, while the client connects to the server and sends a message, receiving a response in return.

Uploaded by

fjrjfg015
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)
7 views

prac9

The document outlines the steps to implement Java client-server communication using networking concepts. It includes creating a Java project with two packages: 'server' and 'client', and provides the code for both Server.java and Client.java. The server listens for client connections and exchanges messages, while the client connects to the server and sends a message, receiving a response in return.

Uploaded by

fjrjfg015
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/ 3

PRACTICAL :-09

Aim : Implement java client-server communication using java networking concepts.


Step 1 : : Create a New java project name it Networkingproject.

Step 2 : Create a NewPackage name it server.

Server.java
package server;
import java.io.*;
import java.net.*;

public class Server {


public static void main(String[] args) {
int port = 5000;
try (ServerSocket serverSocket = new ServerSocket(port)) {
System.out.println("Server started. Waiting for a client...");
Socket socket = serverSocket.accept();
System.out.println("Client connected: " + socket.getInetAddress())

BufferedReader in = new BufferedReader(new


InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
String clientMessage = in.readLine();
System.out.println("Client says: " + clientMessage);
out.println("Hello from Server!");
in.close();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Step 3 : Create a NewPackage name it client.


Client.java
package client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {


public static void main(String[] args) {
String serverAddress = "localhost";
int port = 5000;
try (Socket socket = new Socket(serverAddress, port)) {
System.out.println("Connected to server.");
BufferedReader in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("Hello from Client!");
String response = in.readLine();
System.out.println("Server says: " + response);
in.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

OUTPUT:

Right click on Program9 class and RunAsJava Application.

You might also like