0% found this document useful (0 votes)
9 views7 pages

Pract 3 Socket

The document provides Java programs for client-server communication using sockets. It includes examples for sending messages, greeting users based on server time, displaying server date and time, and handling multiple clients. Each program consists of both server and client components to demonstrate the functionality.
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)
9 views7 pages

Pract 3 Socket

The document provides Java programs for client-server communication using sockets. It includes examples for sending messages, greeting users based on server time, displaying server date and time, and handling multiple clients. Each program consists of both server and client components to demonstrate the functionality.
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

Assignment no.

3(Socket)
1. Write a java program to send “Hi” message to the Server.
File Name: [Link]
import [Link].*;
import [Link].*;

class Server {
public static void main(String args[]) throws Exception {

ServerSocket ss = new ServerSocket(5000);


[Link]("Server is waiting...");

Socket s = [Link](); // Accept client connection


[Link]("Client connected");

DataInputStream dis = new DataInputStream([Link]());


String msg = [Link](); // Read message from client

[Link]("Message received from client: " + msg);

[Link]();
[Link]();
[Link]();
}
}

File Name: [Link]


import [Link].*;
import [Link].*;

class Client {
public static void main(String args[]) throws Exception {

Socket s = new Socket("localhost", 5000);

DataOutputStream dos = new DataOutputStream([Link]());


[Link]("Hi"); // Sending "Hi" message

[Link]();
[Link]();
[Link]();
}
}

2. Write a java program to accept a user name in client application and greets to
the user according to the time on server machine.
File Name:[Link]
import [Link].*;
import [Link].*;
import [Link];

class TimeServer {
public static void main(String args[]) throws Exception {

ServerSocket ss = new ServerSocket(5000);


[Link]("Server is waiting...");

Socket s = [Link]();
[Link]("Client connected");

DataInputStream dis = new DataInputStream([Link]());


DataOutputStream dos = new DataOutputStream([Link]());

String name = [Link](); // Receive username

// Get server time


LocalTime time = [Link]();
int hour = [Link]();

String greeting;

if(hour < 12)


greeting = "Good Morning " + name;
else if(hour < 17)
greeting = "Good Afternoon " + name;
else if(hour < 20)
greeting = "Good Evening " + name;
else
greeting = "Good Night " + name;

[Link](greeting); // Send greeting to client

[Link]();
[Link]();
[Link]();
[Link]();
}
}

File Name: [Link]


import [Link].*;
import [Link].*;

class TimeClient {
public static void main(String args[]) throws Exception {

Socket s = new Socket("localhost", 5000);

DataInputStream dis = new DataInputStream([Link]());


DataOutputStream dos = new DataOutputStream([Link]());
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));

[Link]("Enter your name: ");


String name = [Link]();

[Link](name); // Send name to server


String response = [Link](); // Receive greeting
[Link]("Server says: " + response);

[Link]();
[Link]();
[Link]();
}
}

3. Write a client server program which display the server machine date and time
on the client machine.
File Name: [Link]
import [Link].*;
import [Link].*;
import [Link];

class DateTimeServer {
public static void main(String args[]) throws Exception {

ServerSocket ss = new ServerSocket(5000);


[Link]("Server is waiting...");

Socket s = [Link](); // Accept client connection


[Link]("Client connected");

DataOutputStream dos = new DataOutputStream([Link]());

Date date = new Date(); // Get server date & time


[Link]("Server Date and Time: " + [Link]());

[Link]();
[Link]();
[Link]();
}
}

File Name: [Link]


import [Link].*;
import [Link].*;
class DateTimeClient {
public static void main(String args[]) throws Exception {

Socket s = new Socket("localhost", 5000);

DataInputStream dis = new DataInputStream([Link]());

String serverDate = [Link](); // Receive date & time


[Link](serverDate);

[Link]();
[Link]();
}
}

4. Write a java program to send and receive the messages from multiple terminals.
File Name:[Link]
import [Link].*;
import [Link].*;
import [Link].*;

class MultiServer {

static Vector<ClientHandler> clients = new Vector<>();

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

ServerSocket ss = new ServerSocket(5000);


[Link]("Server started...");

while (true) {
Socket s = [Link]();
[Link]("New client connected");

ClientHandler ch = new ClientHandler(s);


[Link](ch);

Thread t = new Thread(ch);


[Link]();
}
}
}

class ClientHandler implements Runnable {

Socket socket;
DataInputStream dis;
DataOutputStream dos;
public ClientHandler(Socket s) throws Exception {
socket = s;
dis = new DataInputStream([Link]());
dos = new DataOutputStream([Link]());
}

public void run() {


try {
while (true) {

String msg = [Link]();

// Send message to all clients


for (ClientHandler mc : [Link]) {
[Link](msg);
}
}
} catch (Exception e) {
[Link]("Client disconnected");
}
}
}

File Name: [Link]


import [Link].*;
import [Link].*;

class MultiClient {

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

Socket s = new Socket("localhost", 5000);

DataInputStream dis = new DataInputStream([Link]());


DataOutputStream dos = new DataOutputStream([Link]());
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));

// Thread to receive messages


Thread receive = new Thread(() -> {
try {
while (true) {
String msg = [Link]();
[Link]("Message: " + msg);
}
} catch (Exception e) {
}
});
[Link]();

// Sending messages
while (true) {
String message = [Link]();
[Link](message);
}
}
}

You might also like