0% found this document useful (0 votes)
43 views9 pages

Câu 1: C NG 1107, UCLN, BCNN

This document contains 3 Java code examples that demonstrate using UDP client-server communication: 1. A client that sends a student code to a server, receives back calculation results, and sends the results. 2. A client that sends a string to a server to be standardized, receives the results back, and sends an acknowledgment. 3. A client that sends a Student object to a server to have the GPA converted to a letter grade, receives the updated Student back, and sends an acknowledgment.

Uploaded by

Ngọc Thuần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views9 pages

Câu 1: C NG 1107, UCLN, BCNN

This document contains 3 Java code examples that demonstrate using UDP client-server communication: 1. A client that sends a student code to a server, receives back calculation results, and sends the results. 2. A client that sends a string to a server to be standardized, receives the results back, and sends an acknowledgment. 3. A client that sends a Student object to a server to have the GPA converted to a letter grade, receives the updated Student back, and sends an acknowledgment.

Uploaded by

Ngọc Thuần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Câu 1: cổng 1107, UCLN, BCNN

package Ex1;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
*
* @author W8570
*/
public class Client {
public static void main(String[] args) {
DatagramSocket client;
DatagramPacket sendPacket, receivePacket;
byte[] sendData, receiveData;
int port = 1107;
String host = "localhost";
int qCode = 100;
String studentCode = "B16DCAT001";
String requestId = null;

try {

client = new DatagramSocket();


InetAddress add = InetAddress.getByName(host);

String s = ";" + studentCode + ";";


sendData = s.getBytes();
sendPacket = new DatagramPacket(sendData, sendData.length, add, port);
client.send(sendPacket);

receiveData = new byte[1024];


receivePacket = new DatagramPacket(receiveData, receiveData.length);
client.receive(receivePacket);

String x = new String(receivePacket.getData()).trim();


String[] result = x.split("[;,]");

requestId = result[0];
int a = Integer.parseInt(result[1]);
int b = Integer.parseInt(result[2]);
int sum = a+b;
int mul =a*b;
int ucln = ucln(a,b);
int bcnn = bcnn(a,b);

String w = requestId + ";" + ucln + "," + bcnn + "," + sum + "," + mul;

sendData = w.getBytes();
sendPacket = new DatagramPacket(sendData, sendData.length,
receivePacket.getSocketAddress());
client.send(sendPacket);

client.close();
} catch (Exception ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}

public static int ucln(int a, int b){


int temp;
while(b!=0){
temp = a%b;
a = b;
b = temp;
}
return a;
}

public static int bcnn(int a, int b){


return (a*b)/ucln(a,b);
}
}

Câu 2: Chuẩn hoá, cổng 1108, bài 101


package Ex2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author W8570
*/
public class Client {
public static void main(String[] args) {
DatagramSocket client;
DatagramPacket sp, rp;
byte[] sd, rd;
int port = 1108;
int qCode = 101;
String host = "localhost";
String sCode = "B16DCAT001";
String requestId = "";

try {
client = new DatagramSocket();
InetAddress add = InetAddress.getByName(host);
String s = ";" + sCode + ";" + qCode;
sd = s.getBytes();
sp = new DatagramPacket(sd, sd.length, add, port);
client.send(sp);

rd = new byte[1024];
rp = new DatagramPacket(rd, rd.length);
client.receive(rp);

String x = new String(rp.getData()).trim();


String[] set = x.split("[;,]");
requestId = set[0];

String y = set[1];
String a = chuan_hoa(y, 0);
String b = chuan_hoa(y, 1);

String w = requestId + ";" + a + "," + b;


sd = w.getBytes();
sp = new DatagramPacket(sd, sd.length, rp.getSocketAddress());
client.send(sp);

client.close();
} catch (Exception ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}

public static String chuan_hoa(String s, int i){


String regex1 = "[a-zA-Z0-9]";
String regex2 = "\\W|_";
if(i==1)
return s.replaceAll(regex1, "");
else
return s.replaceAll(regex2, "");
}
}

Câu 3: cổng 1109, qui đổi điểm sang A,B,C,D


package Ex3;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import udp.Student;

/**
*
* @author W8570
*/
public class Client {
public static void main(String[] args) {
DatagramSocket client;
DatagramPacket sp, rp;
byte[] sd, rd;
int port = 1109;
String host = "localhost";

try {
client = new DatagramSocket();
InetAddress add = InetAddress.getByName(host);

Student s = new Student("B16DCAT001");


sd = objectToByte(s);
sp = new DatagramPacket(sd, sd.length, add, port);
client.send(sp);

rd = new byte[1024];
rp = new DatagramPacket(rd, rd.length);
client.receive(rp);
Student x = byteToObject(rp.getData());

x.setGpaLetter(doi_Gpa(x.getGpa()));
sd = objectToByte(x);
sp = new DatagramPacket (sd, sd.length, rp.getSocketAddress());
client.send(sp);

client.close();
} catch (Exception ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
}

private static byte[] objectToByte(Student s){


byte[] b = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(s);
b = out.toByteArray();
oos.close();
} catch (IOException ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
return b;
}

private static Student byteToObject(byte[] b){


Student s = null;
try {
ByteArrayInputStream in = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(in);
s = (Student) ois.readObject();
ois.close();
} catch (Exception ex) {
Logger.getLogger(Client.class.getName()).log(Level.SEVERE, null, ex);
}
return s;
}

private static String doi_Gpa(float gpa){


if(gpa>=3.7 && gpa<=4)
return "A";
else if(gpa>=3.0 && gpa<=3.7)
return "B";
else if(gpa>=2.0 && gpa<=3.0)
return "C";
else if(gpa>=1.0 && gpa<=2.0)
return "D";
else if(gpa>=0 && gpa<=1.0)
return "F";
return null;
}
}

You might also like