Chào các bác,
Tình hình là em đang học viết 1 chương trình chat giữa Client và Server nhưng em chưa biết làm thế nào để có thể nhận và gửi tin nhắn liên tục mà chỉ có thể gửi và nhận từng tin nhắn xen kẽ nhau
Mong các bác giúp đỡ !!!
Client
Server
Tình hình là em đang học viết 1 chương trình chat giữa Client và Server nhưng em chưa biết làm thế nào để có thể nhận và gửi tin nhắn liên tục mà chỉ có thể gửi và nhận từng tin nhắn xen kẽ nhau
Mong các bác giúp đỡ !!!
Client
Java:
package baitap.cuoichuong.ClientServer;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;
import java.util.Scanner;
public class ClientExample {
private String svName;
private int port;
private Socket client;
private OutputStream outputToServer;
private DataOutputStream output;
private InputStream inputFromServer;
private DataInputStream input;
private String msg;
private boolean status = false;
public ClientExample() {
}
public void nhap() {
Scanner sc = new Scanner(System.in);
System.out.print("Nhap IP server: ");
svName = sc.nextLine();
System.out.print("Nhap Port servr: ");
port = sc.nextInt();
}
private void connectToServer() {
nhap();
try {
client = new Socket(InetAddress.getByName(svName), port);
System.out.println("Ket noi toi " + svName + " tai port " + port);
status = true;
} catch (UnknownHostException e) {
System.out.println("Khong tim thay dia chi server!!");
} catch (ConnectException e) {
System.out.println("Khong ket noi duoc voi server " + svName);
} catch (IOException e) {
e.printStackTrace();
}
}
private void getStreams() {
try {
output = new DataOutputStream(client.getOutputStream());
output.flush();
input = new DataInputStream(client.getInputStream());
System.out.println("DA THIET LAP I/O STREAMS VOI "
+ client.getInetAddress().getHostName());
} catch (ConnectException e) {
System.out.println("Qua thoi gian cho!");
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMessages() {
Scanner sc = new Scanner(System.in);
do {
System.out.print(client.getLocalAddress().getHostName() + " >>> ");
msg = sc.nextLine();
try {
output.writeUTF(msg);
output.flush();
System.out.print("");
receiveMessages();
msg = sc.nextLine();
} catch (Exception e) {
System.out.println("LOI KHONG GUI DUOC TIN NHAN!!!");
}
} while (msg.equals("EXIT!!!") == false);
}
private void receiveMessages() {
do {
try {
msg = input.readUTF();
System.out.println(msg);
sendMessages();
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("LOI KET NOI!");
}
} while (msg.equals("EXIT!!!") == false);
}
public void run() {
connectToServer();
if (status == true) {
getStreams();
sendMessages();
receiveMessages();
}
}
}
Java:
package baitap.cuoichuong.ClientServer;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.util.Scanner;
public class ServerExample {
private ServerSocket svSocket;
private Socket sv;
private DataOutputStream output;
private DataInputStream input;
private boolean status = false;
private String msg;
public ServerExample() {
}
public ServerExample(int port) throws IOException {
svSocket = new ServerSocket(port);
svSocket.setSoTimeout(30000);
}
private boolean waitForConnection() {
System.out.println("Dang cho ket noi tai port "
+ svSocket.getLocalPort() + "...");
try {
sv = svSocket.accept();
System.out.println("Da thiet lap ket noi voi "
+ sv.getInetAddress().getHostName());
status = true;
} catch (SocketTimeoutException e) {
System.out.println("Qua thoi gian cho!");
} catch (IOException e) {
e.printStackTrace();
}
return status;
}
private void getStreams() {
try {
output = new DataOutputStream(sv.getOutputStream());
output.flush();
input = new DataInputStream(sv.getInputStream());
System.out.println("DA THIET LAP I/O STREAMS VOI "
+ sv.getInetAddress().getHostName());
} catch (IOException e) {
e.printStackTrace();
}
}
private void sendMessages() {
Scanner sc = new Scanner(System.in);
do {
System.out.print(sv.getLocalAddress().getHostName() + " >>> ");
msg = sc.nextLine();
try {
output.writeUTF(sv.getLocalAddress().getHostName() + " >>> "
+ msg);
output.flush();
System.out.println("");
receiveMessages();
msg = sc.nextLine();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("LOI KHONG GUI DUOC TIN NHAN!");
}
} while (msg.equals("EXIT!!!") == false);
}
private void receiveMessages() {
do {
try {
msg = input.readUTF();
System.out.println(sv.getInetAddress().getHostName() + " >>> "
+ msg);
sendMessages();
} catch (IOException e) {
// TODO Auto-generated catch block
System.err.println("LOI KHONG DOC DUOC TIN NHAN!");
}
} while (msg.equals("EXIT!!!") == false);
}
public void run() {
waitForConnection();
if (status == true) {
getStreams();
sendMessages();
receiveMessages();
}
}
}