0% found this document useful (0 votes)
8 views2 pages

N_W lab exp 5

Uploaded by

thorat_496512597
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

N_W lab exp 5

Uploaded by

thorat_496512597
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

● Program:-

1. Client side:-
import java.io.*;
import java.net.*;
import java.util.*;

public class Client {


public static void main(String args[]) throws IOException
{

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a message to send to the server: ");


String userMessage = scanner.nextLine(); // Capture user input

Socket socket = new Socket("192.168.12.142", 4000);

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

out.println(userMessage);

BufferedReader in = new BufferedReader(new


InputStreamReader(socket.getInputStream()));

String response = in.readLine();


System.out.println("Server says: " + response);

socket.close();
scanner.close();
}
}

2. Server side:-
import java.io.*;
import java.net.*;
import java.util.*;
public class Server {
public static void main(String args[]) throws IOException
{
Scanner scanner = new Scanner(System.in);

ServerSocket serverSocket = new ServerSocket(4000);


System.out.println("Server is running and waiting for client connection...");
Socket clientSocket = serverSocket.accept();
System.out.println("Client connected!");

System.out.print("Enter a message to send to the client: ");


String userMessage = scanner.nextLine(); // Capture user input

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

out.println(userMessage);

BufferedReader in = new BufferedReader(new


InputStreamReader(clientSocket.getInputStream()));

String message = in.readLine();


System.out.println("Client says: " + message);
out.println("Message received by the server.");
clientSocket.close();
serverSocket.close();
}
}

● Output:-

You might also like