Socket Programming
Socket Programming
1) Construct a TCP client-server system to allow client programs to get the system date and time from
the server. When a client connects to the server, the server gets the local time on the machine and
sends it to the client. The client displays the date and time on the screen and terminates.
Server1.java
import java.io.*;
import
java.net.*;
import
java.text.SimpleDateFormat;
import java.util.Date;
while (true) {
// Wait for a client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Connection from: " +
clientSocket);
// Close the
connection
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Client1.java
import java.io.*;
import
java.net.*;
2) Construct client-server programming: The client has a list of words (at least 20 words) and the
server has a dictionary (at least 15 entries). The client picks up a random word from its list and sends it
to the server. The server responds with its meaning, if found else adds this entry to its dictionary with
its meaning.
Server2.java
import java.io.*;
import java.net.*;
import java.util.*;
static {
// Initialize dictionary with some entries
dictionary.put("apple", "a fruit");
dictionary.put("banana", "an elongated curved
fruit");
dictionary.put("carrot", "a long pointed orange vegetable");
}
public static void main(String[] args) {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(9999);
System.out.println("Server is running and waiting for
connections...");
while (true) {
// Wait for a client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Connection from: " +
clientSocket);
// Close the
connection
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
while (true) {
// Wait for a client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Connection from: " +
clientSocket);
// Close the
connection
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Calculate factorial
int factorial = calculateFactorial(number);
Client3.java
import java.io.*;
import
java.net.*;
import java.util.Scanner;
while (true) {
// Wait for a client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Connection from: " +
clientSocket);
// Close the
connection
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
// Calculate grade
String grade = calculateGrade(mark);
Client4.java
import java.io.*;
import
java.net.*;
import java.util.Scanner;
while (true) {
// Wait for a client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Connection from: " +
clientSocket);
// Close the
connection
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Client5.java
import java.io.*;
import
java.net.*;
6) Construct a Java program using client-server programming: The client sends a word and a number
to the server and the server sends the letter which occurs the given number of times in the given word,
if it exists else send the letter which occurs at maximum number of times in the given word.
Connection should not terminate till the client wants to.
Server6.java
import java.io.*;
import java.net.*;
import java.util.*;
while (true) {
// Wait for a client connection
Socket clientSocket = serverSocket.accept();
System.out.println("Connection from: " +
clientSocket);
} catch (IOException e) {
e.printStackTrace();
}
}
private static void handleClientRequest(Socket clientSocket) throws IOException {
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
while (true) {
// Receive word and number from the client
String word = in.readLine();
int number = Integer.parseInt(in.readLine());
while (true) {
// Get input from the user
System.out.print("Enter a word: ");
String word = scanner.nextLine();
System.out.print("Enter a number: ");
int number = scanner.nextInt();
UDPServer.java
import java.net.*;
public class UDPServer {
public static void main(String[] args) {
final int PORT = 9999;
try {
// Create a DatagramSocket to listen for client requests
DatagramSocket serverSocket = new
DatagramSocket(PORT);
System.out.println("Server is running and waiting for requests...");
while (true) {
UDPClient.java
import java.net.*;
try {
// Create a DatagramSocket
DatagramSocket clientSocket = new DatagramSocket();