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

CN P8

program

Uploaded by

Thrisha K B Babu
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)
10 views

CN P8

program

Uploaded by

Thrisha K B Babu
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/ 4

Computer Network Lab Manual

8. Develop a program on a datagram socket for client/server to display the


messages on client side, typed at the server side.

1. Create a file named Server.java with the following code:

import java.net.*;
import java.io.*;

public class Server {


public static void main(String[] args) {
DatagramSocket socket = null;
BufferedReader reader = null;
try {
// Create a DatagramSocket to send data
socket = new DatagramSocket();
reader = new BufferedReader(new InputStreamReader(System.in));

// Define the client's IP address and port


InetAddress clientAddress = InetAddress.getByName("localhost");
int clientPort = 1234;

System.out.println("Server is running. Type messages to send to the client:");

while (true) {
// Read input from the server console
String message = reader.readLine();
if (message.equalsIgnoreCase("exit")) {
System.out.println("Server is shutting down...");
break;
}

// Convert message to byte array


byte[] buffer = message.getBytes();

// Create a packet to send to the client


DatagramPacket packet = new DatagramPacket(buffer, buffer.length,
clientAddress, clientPort);

// Send the packet


socket.send(packet);
System.out.println("Message sent: " + message);
}
Computer Network Lab Manual

} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
socket.close();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2. Create a file named Client.java with the following code:

import java.net.*;

public class Client {


public static void main(String[] args) {
DatagramSocket socket = null;
try {
// Create a DatagramSocket to receive data
socket = new DatagramSocket(1234);

System.out.println("Client is running and waiting for messages...");

byte[] buffer = new byte[1024];


while (true) {
// Create a packet to receive data
DatagramPacket packet = new DatagramPacket(buffer, buffer.length);

// Receive the packet from the server


socket.receive(packet);

// Extract the message


String message = new String(packet.getData(), 0, packet.getLength());
System.out.println("Message from server: " + message);

if (message.equalsIgnoreCase("exit")) {
System.out.println("Server has shut down the connection.");
Computer Network Lab Manual

break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (socket != null && !socket.isClosed()) {
socket.close();
}
}
}
}

3. Compile both files:

javac Server.java
javac Client.java

4. Run the Client:

java Client

5. Run the Server:

java Server

6. Server Output

Server is running. Type messages to send to the client:


Hello, Client!
Message sent: Hello, Client!
How are you?
Message sent: How are you?
exit
Server is shutting down...
Computer Network Lab Manual

7. Client Output

Client is running and waiting for messages...


Message from server: Hello, Client!
Message from server: How are you?
Message from server: exit
Server has shut down the connection.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy