22DH111684
22DH111684
22DH111684
Ví dụ 1:
Server:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.Buffer;
Client:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.nio.Buffer;
}
}
Kết quả:
Ví dụ 2:
import java.util.HashMap;
import java.util.Map;
//lap qua tat ca cac cap khoa gia tri trong hashmap
for(Map.Entry<String,Integer> entry: studentGrades.entrySet()){
String studentName = entry.getKey();
int grade =entry.getValue();
System.out.println(studentName + " "+grade);
}
}
}
Kết quả:
Ví dụ 3:
import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.*;
public class MySQLExample {
public static void main(String[] args) throws SQLException{
String url="jdbc:MySQL://localhost:3306/java_user_database";
String Username="root";
String Password="";
try{
// neu su dung java<9
Class.forName("com.mysql.cj.jdbc.Driver");
}
}catch (Exception e){
System.out.println("Cannot connect to sql ");
e.printStackTrace();
}
}
}
Bài tập 1:
Server:
import java.io.*;
import java.net.*;
import java.util.HashMap;
while (true) {
Socket clientSocket = serverSocket.accept();
new ClientHandler(clientSocket).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new
PrintWriter(clientSocket.getOutputStream(), true);
if (accountDatabase.containsKey(username)) {
out.println("Username already exists.");
} else if (password.length() <= 8) {
out.println("Password must be longer than 8 characters.");
} else {
accountDatabase.put(username, password);
out.println("Account registered successfully.");
}
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Client:
import java.io.*;
import java.net.*;
import java.util.Scanner;
out.println(username);
out.println(password);
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Kết quả:
Bài tập 2:
Server:
import java.io.*;
import java.net.*;
import java.util.Random;
while (true) {
Socket clientSocket = serverSocket.accept();
new ClientHandler(clientSocket, secretNumber,
guessCount).start();
guessCount++;
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void run() {
try {
BufferedReader in = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
PrintWriter out = new
PrintWriter(clientSocket.getOutputStream(), true);
while (true) {
String guessStr = in.readLine();
int guess = Integer.parseInt(guessStr);
if (guess == secretNumber) {
out.println("Congratulations! You guessed the correct
number.");
out.println("Total guesses: " + guessCount);
break;
} else if (guess < secretNumber) {
out.println("Try a higher number.");
} else {
out.println("Try a lower number.");
}
}
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Client:
import java.io.*;
import java.net.*;
import java.util.Scanner;
while (true) {
System.out.print("Your guess: ");
int guess = scanner.nextInt();
out.println(guess);
if (response.contains("Congratulations")) {
break;
}
}
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Kết quả: