Created
September 28, 2020 03:46
-
-
Save DungGramer/7e89f88d3ca388eda2fe20084de22aa6 to your computer and use it in GitHub Desktop.
Send data client to Server, then print result to client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
import java.util.Scanner; | |
public class Client { | |
public static void main(String[] args) throws IOException { | |
//tạo socket kết nối đển Server | |
Socket socket = new Socket("localhost", 4567); | |
// tạo đối tượng DataInputStream để đọc dữ liệu | |
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); | |
// Tạo đối tượng DataOutputStream để truyền dữ liệu đi | |
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()); | |
Scanner scanner = new Scanner(System.in); | |
while (true) { | |
// nhập tin nhắn và gửi cho server | |
String str = scanner.nextLine(); | |
// hàm WriteUTF() của OutputStream cho phép nó gửi Data dạng String đến InputStream của Client | |
dataOutputStream.writeUTF(str); | |
dataOutputStream.flush(); | |
if (str.equals("q")) break; | |
System.out.println(dataInputStream.readUTF()); | |
} | |
dataOutputStream.close(); | |
socket.close(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.DataInputStream; | |
import java.io.DataOutputStream; | |
import java.io.IOException; | |
import java.net.ServerSocket; | |
import java.net.Socket; | |
public class Server { | |
public static String covertString(String str) { | |
str = str.trim(); | |
String str2[] = str.split(" "); | |
String result = ""; | |
for (int i = 0; i < str2.length; i++) { | |
result += str2[i].substring(0, 1).toUpperCase() + str2[i].substring(1).toLowerCase() + " "; | |
} | |
return result; | |
} | |
public static int demUpperCase(String str) { | |
int count = 0; | |
for (int i = 0; i < str.length(); i++) { | |
if (Character.isUpperCase(str.charAt(i))) { | |
count++; | |
} | |
} | |
return count; | |
} | |
public static boolean checkTamgiac(String str) { | |
String num[] = str.split(" "); | |
if (num.length > 3) return false; | |
int a = Integer.parseInt(num[0]); | |
int b = Integer.parseInt(num[1]); | |
int c = Integer.parseInt(num[2]); | |
if (a < b + c && b < a + c && c < a + b) return true; | |
return false; | |
} | |
public static void main(String[] args) throws IOException { | |
//Khởi tạo Server | |
ServerSocket serverSocket = new ServerSocket(4567); | |
//Chờ Socket phía Client kết nối | |
Socket socket = serverSocket.accept(); | |
// tạo đối tượng DataInputStream để đọc dữ liệu | |
DataInputStream dataInputStream = new DataInputStream(socket.getInputStream()); | |
// Tạo đối tượng DataOutputStream để truyền dữ liệu đi | |
DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream()); | |
while (true) { | |
// đọc tin nhắn từ client , dùng ReadUTF để đọc String | |
String str = dataInputStream.readUTF(); | |
if (str.equals("q")) break; | |
/* System.out.println("Chuoi nhan duoc: " + str); | |
System.out.println("Xu ly: " + covertString(str)); | |
System.out.println("So luong tu: " + str.length()); | |
System.out.println("So luong Uppercase: " + demUpperCase(str));*/ | |
// System.out.println("Check dieu kien tam giac: " + checkTamgiac(str)); | |
String result = "Chuoi nhan duoc: " + str + "\nXu ly: " + covertString(str) + "\nSo luong tu: " + str.length() + "\nSo luong Uppercase: " + demUpperCase(str) ; | |
dataOutputStream.writeUTF(result); | |
} | |
dataInputStream.close(); | |
socket.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment