package doslab2_datagram;
import java.io.*;
import java.net.*;
import java.util.*;
class client {
public static void main(String args[]) throws Exception {
Scanner scan = new Scanner(System.in);
DatagramSocket clientSocket = new DatagramSocket();
InetAddress IPAddress = InetAddress.getByName("localhost");
byte[] sendData = new byte[1024];
byte[] receiveData = new byte[1024];
System.out.println("Enter the operator");
String temp = scan.next();
System.out.println("Enter the operand");
String temp1 = scan.next();
System.out.println("Enter the operand");
String temp2 = scan.next();
String sentence = temp + " " + temp1 + " " + temp2;
sendData = sentence.getBytes();
DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, 9876);
clientSocket.send(sendPacket);
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
clientSocket.receive(receivePacket);
String modifiedSentence = new String(receivePacket.getData());
System.out.println("FROM SERVER:" + modifiedSentence);
clientSocket.close();
}
}
package doslab2_datagram;
import java.io.*;
import java.net.*;
import java.util.*;
import java.math.*;
class server {
public static void main(String args[]) throws Exception {
DatagramSocket serverSocket = new DatagramSocket(9876);
byte[] receiveData = new byte[1024];
byte[] sendData = new byte[1024];
float sum = 0;
while (true) {
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
serverSocket.receive(receivePacket);
String sentence = new String(receivePacket.getData());
System.out.println("RECEIVED: " + sentence);
StringTokenizer st = new StringTokenizer(sentence);
String str[] = new String[3];
while (st.hasMoreTokens()) {
for (int i = 0; i < 3; i++) {
str[i] = (st.nextToken());
}
}
for (int i = 0; i < 3; i++) {
System.out.println("THE VALUE IS " + str[i]);
}
String operator = str[0];
String operand1 = str[1];
String operand2 = str[2];
float f = Float.valueOf(operand1.trim()).floatValue();
float f1 = Float.valueOf(operand2.trim()).floatValue();
if (operator.equals("+")) {
try {
sum = f + f1;
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException: " + nfe.getMessage());
}
} else if (operator.equals("-")) {
try {
sum = f - f1;
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException: " + nfe.getMessage());
}
} else if (operator.equals("*")) {
try {
sum = f * f1;
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException: " + nfe.getMessage());
}
} else if (operator.equals("/")) {
try {
sum = f / f1;
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException: " + nfe.getMessage());
}
} else if (operator.equals("%")) {
try {
sum = f % f1;
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException: " + nfe.getMessage());
}
} else if (operator.equals("%")) {
try {
sum = (float) Math.pow(f, f1);
} catch (NumberFormatException nfe) {
System.out.println("NumberFormatException: " + nfe.getMessage());
}
}
InetAddress IPAddress = receivePacket.getAddress();
int port = receivePacket.getPort();
String capitalizedSentence = String.valueOf(sum);
sendData = capitalizedSentence.getBytes();
DatagramPacket sendPacket =
new DatagramPacket(sendData, sendData.length, IPAddress, port);
serverSocket.send(sendPacket);
}
}
}