Em có code server bên dưới, em không hiểu sao khi Client gửi dữ liệu đến Server, theo em nghĩ Server xử lý hiển thị dữ liệu xong nó phải chạy đoạn output.println("abc");
và hiển thị lại cho Client là abc chứ tại sao lại không thấy nó gửi lại Client abc ạ? Xin anh chị giải thích hộ em với ạ!
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication1.Server;
/**
*
* @author As
*/
//chuyen String sang char
//chuyen String sang char
import java.net.*;
import java.util.*;
import java.io.*;
public class Server
{
public static void main(String[] args)
{
new Server();
}
public Server()
{
//Tao try-catch xu ly loi
try {
ServerSocket sSocket = new ServerSocket(8888);
System.out.println("Server ready at: " + new Date());
//tao luong cho ket noi tu client
Socket socket = sSocket.accept();
//tao luong du lieu
PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//Thong bao client da ket noi thanh cong
output.println("You have connected at: " + new Date());
//Vong lap lam viec Server
while(true) {
//Hien thi cac du lieu tu client gui den
String chatInput = input.readLine();
System.out.println(chatInput);
output.println("abc");
}
} catch(IOException exception) {
System.out.println("Error: " + exception);
}
}
}
Bên Client:
You have connected at: Thu Nov 05 10:01:48 ICT 2015
1
Bên Server:
Server ready at: Thu Nov 05 10:01:43 ICT 2015
1