Cnlab 3
Cnlab 3
Cnlab 3
Name:hariharan.s
Regno:22mis0340
server:
import java.io.*;
import java.net.*;
try {
serverSocket = new DatagramSocket(9876);
System.out.println("Server is running...");
while (true) {
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
if (isValid) {
sendData = "Success: Valid login".getBytes();
} else {
sendData = "Error: Invalid login".getBytes();
}
InetAddress clientAddress =
receivePacket.getAddress();
int clientPort = receivePacket.getPort();
Client:
import java.io.*;
import java.net.*;
public class UDPClient {
public static void main(String[] args) {
DatagramSocket clientSocket = null;
try {
clientSocket = new DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName("localhost");
int serverPort = 9876;
Output:
2. Write a UDP program to implement an image transfer from client
to server.
Code:
Server:
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
try {
serverSocket = new DatagramSocket(9876);
System.out.println("Server is running...");
while (true) {
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
Client:
import java.io.*;
import java.net.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
try {
clientSocket = new DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName("localhost");
int serverPort = 9876;
Code:
Server:
import java.io.*;
import java.net.*;
try {
serverSocket = new DatagramSocket(9876);
while (true) {
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
byte[] sendData =
LOCAL_MAC_ADDRESS.getBytes();
DatagramPacket sendPacket = new
DatagramPacket(sendData, sendData.length, clientAddress,
clientPort);
serverSocket.send(sendPacket);
Client:
import java.io.*;
import java.net.*;
try {
clientSocket = new DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName("localhost");
int serverPort = 9876;
Output:
4. Implement a DNS server and client using UDP sockets. The client
should give the URL as the input and the server should send the
respective IP address.
Code:
Server:
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
static {
// Sample DNS entries
dnsMap.put("www.vit.ac.in", "192.168.1.1");
dnsMap.put("www.test.com", "192.168.1.2");
dnsMap.put("www.example.org", "192.168.1.3");
}
try {
serverSocket = new DatagramSocket(9876);
while (true) {
DatagramPacket receivePacket = new
DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
InetAddress clientAddress =
receivePacket.getAddress();
int clientPort = receivePacket.getPort();
Client:
import java.io.*;
import java.net.*;
try {
clientSocket = new DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName("localhost");
int serverPort = 9876;
Output:
5. Implement a UDP-based socket program for the game “Guess It”.
The game is played by the client and server. The game proceeds as
follows: The server will think of a “magic number”. The magic
number is greater than zero and less than 100. Prompt for the
“magic number” from the client. The server should print out
“Higher” if the magic number is higher than the guess and “Lower”
if the magic number is less than the guess. Give 10 chances for the
client to guess the number. When the magic number has been
guessed print "Great" and then end. If the client cannot guess the
number in 10 attempts, then print the message "Better luck next
time:)" and end.
Code:
Server:
import java.io.*;
import java.net.*;
import java.util.Random;
try {
serverSocket = new DatagramSocket(9876);
if (guess == magicNumber) {
sendResponse(serverSocket,
receivePacket.getAddress(), receivePacket.getPort(), "Great! You
guessed it!");
break;
} else if (guess < magicNumber) {
sendResponse(serverSocket,
receivePacket.getAddress(), receivePacket.getPort(), "Higher");
} else {
sendResponse(serverSocket,
receivePacket.getAddress(), receivePacket.getPort(), "Lower");
}
if (attempts == 9) {
sendResponse(serverSocket,
receivePacket.getAddress(), receivePacket.getPort(), "Better luck
next time :)");
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
serverSocket.close();
}
}
}
import java.io.*;
import java.net.*;
import java.util.Scanner;
try {
clientSocket = new DatagramSocket();
InetAddress serverAddress =
InetAddress.getByName("localhost");
int serverPort = 9876;
byte[] sendData =
String.valueOf(guess).getBytes();
DatagramPacket sendPacket = new
DatagramPacket(sendData, sendData.length, serverAddress,
serverPort);
clientSocket.send(sendPacket);
if (receivedMessage.startsWith("Great")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (clientSocket != null) {
clientSocket.close();
}
}
}
}
Output: